c# - EF - Foreign Key Reinserted if Entity Object is Used -
so have problem ef when create objects have fk reference on object retrieved db different db context (see getblog method). problem whenever try insert fk references along main object, fk reference recreated.
one solution can use id of fk objectwhich works, know if possible insert using entire object demonstrated in example below without duplicating fk object.
i tried db.blogs.attach(fkblog) did not help. neither did setting object state added/unchanged. ideas?
public static void main(string[] args) { using( var db = new bloggingcontext() ) { for( int = 0; < 10; ++i ) { var blog = new blog() { name = i.tostring(), description = "desc", url = string.format( "http://{0}", ) }; db.blogs.add(blog); db.savechanges(); } for( int = 0; < 10; ++i ) { var fkblog = getblog(); var post = new post() { blog = fkblog, content = string.format("blog content {0}", i), title = string.format("blog title {0}", i) }; db.posts.add(post); db.savechanges(); } } } public static blog getblog() { using( var db = new bloggingcontext() ) { return db.blogs.orderby( x=>guid.newguid() ).firstordefault(); } } if use attach method (just under entity loaded), receive following error:
attaching entity of type '...blog' failed because entity of same type has same primary key value. can happen when using 'attach' method or setting state of entity 'unchanged' or 'modified' if entities in graph have conflicting key values. may because entities new , have not yet received database-generated key values. in case use 'add' method or 'added' entity state track graph , set state of non-new entities 'unchanged' or 'modified' appropriate.
edit2: attach fails because trying attach object inside loop. if attach each blog once or if deatch object after save works.
inlining not option in case unfortunately. im reliant on external data providers.
you should either use same context object:
public static void main(string[] args) { using( var db = new bloggingcontext() ) { for( int = 0; < 10; ++i ) { var blog = new blog() { name = i.tostring(), description = "desc", url = string.format( "http://{0}", ) }; db.blogs.add(blog); db.savechanges(); } for( int = 0; < 10; ++i ) { var fkblog = db.blogs.orderby( x=>guid.newguid() ).firstordefault(); var post = new post() { blog = fkblog, content = string.format("blog content {0}", i), title = string.format("blog title {0}", i) }; db.posts.add(post); db.savechanges(); } } } or create 2 separate contexts 2 operations:
public static void main(string[] args) { using( var db = new bloggingcontext() ) { for( int = 0; < 10; ++i ) { var blog = new blog() { name = i.tostring(), description = "desc", url = string.format( "http://{0}", ) }; db.blogs.add(blog); db.savechanges(); } } using( var db = new bloggingcontext() ) { for( int = 0; < 10; ++i ) { var fkblog = getblog(); db.context.attach(fkblog); var post = new post() { blog = fkblog, content = string.format("blog content {0}", i), title = string.format("blog title {0}", i) }; db.posts.add(post); db.savechanges(); } } }
Comments
Post a Comment