When we use the Entity Framework Framework for CRUD, there are often a variety of errors, see my experimental results below.
The following is done with only one context object:
First time:
Blogdbcontext blog = new Blogdbcontext (); Post = blog. Posts.single (t = t.id = = 2); Post. Authorid = 1; Blog. SaveChanges ();
Result: Success
Second time:
Blogdbcontext blog = new Blogdbcontext (); Post = blog. Posts.single (t = t.id = = 2); Post. Author = blog. Authors.single (t = t.id = = 3); Blog. SaveChanges ();
Result: Success
Third time:
Blogdbcontext blog = new Blogdbcontext (); Post = blog. Posts.single (t = t.id = = 2); Post. Authorid = 1; Post. Author = blog. Authors.single (t = t.id = = 4); Blog. SaveChanges ();
Result: failure, error as follows:
Conflicting changes to the role ' post_author_target ' of the relationship ' ConsoleApplication1.DDD.Infrastructure.Post_ Author ' has been detected.
Fourth time:
Blogdbcontext blog = new Blogdbcontext (); Post = blog. Posts.single (t = t.id = = 2); Post. Author = new Author () {Id = 4}; Blog. Entry (post. Author). state = entitystate.unchanged; Blog. SaveChanges ();
Result: Success
Fifth time:
Blogdbcontext blog = new Blogdbcontext (); Post = blog. Posts.single (t = t.id = = 2); Author = new author () {Id = 1, Name = "ZWJ"};//direct instantiation or the following query results in author = blog. Authors.asnotracking (). Single (t = = T.id = 3); Post. Author = Author; Blog. SaveChanges ();
The result: success, but a new record is added to the authors table, and an additional ID is assigned to the posts table; Cause: Author is not in the same context as post, the context in which the post is located does not track author information, so it is added.
The following actions are performed in two different contexts:
Sixth time:
Blogdbcontext blog = new Blogdbcontext (); Post = blog. Posts.asnotracking (). Single (t = = T.id = 2); Blog. Entry (POST). state = entitystate.detached; The above asnotracking can be implemented by this sentence blogdbcontext blog2 = new Blogdbcontext (); Post. Authorid = 5; Blog2. Entry (POST). state = entitystate.modified; Blog2. SaveChanges ();
Result: Success
Seventh time:
Blogdbcontext blog = new Blogdbcontext (); Post = blog. Posts.asnotracking (). Single (t = = T.id = 2); Blogdbcontext blog2 = new Blogdbcontext (); Post. Author = blog2. Authors.single (t = t.id = = 5); Blog2. Entry (POST). state = entitystate.modified; Blog2. SaveChanges ();
Result: failure, error as follows:
A referential integrity constraint violation Occurred:the property value (s) of ' author.id ' on one end of a relationship D O Match the property value (s) of ' Post.authorid ' to the other end.
Eighth time:
Blogdbcontext blog = new Blogdbcontext (); Post = blog. Posts.asnotracking (). Single (t = = T.id = 2); Blogdbcontext blog2 = new Blogdbcontext (); Author = blog2. Authors.asnotracking (). Single (t = = T.id = 1); Post. Author = Author; Blog2. Entry (POST). state = entitystate.modified; Blog2. SaveChanges ();
Result: failure, error and seventh time
Nineth Time:
Blogdbcontext blog = new Blogdbcontext (); Post = blog. Posts.asnotracking (). Single (t = = T.id = 2); Author = blog. Authors.asnotracking (). Single (t = = T.id = 3); Blogdbcontext blog2 = new Blogdbcontext (); Post. Author = Author; Blog2. Entry (POST). state = entitystate.modified; Blog2. SaveChanges ();
Result: failure, error and seventh time
Tenth time:
Blogdbcontext blog = new Blogdbcontext (); Post = blog. Posts.asnotracking (). Single (t = = T.id = 2); Blogdbcontext blog2 = new Blogdbcontext (); Author = new author () {Id = 1, Name = "Zwj"}; Post. Author = Author; Blog2. Entry (POST). state = entitystate.modified; Blog2. SaveChanges ();
Result: failure, error and seventh time
11th Time:
Blogdbcontext blog = new Blogdbcontext (); Post = blog. Posts.asnotracking (). Single (t = = T.id = 2); Blogdbcontext blog2 = new Blogdbcontext (); Author = blog2. Authors.single (t = t.id = = 1); Post. Author = Author; Post. Authorid = author. Id; Blog2. Entry (POST). state = entitystate.modified; Blog2. SaveChanges ();
Result: success, but I think mainly through the assignment Authorid to complete, and the same as the sixth time, remove the assignment Authorid, then the same error as the seventh time
12th time:
Blogdbcontext blog = new Blogdbcontext (); Post = blog. Posts.asnotracking (). Single (t = = T.id = 2); Blogdbcontext blog2 = new Blogdbcontext (); Author = blog2. Authors.single (t = t.id = = 3); Blog2. Entry (POST). Reference (t = t.author). CurrentValue = author; Blog2. Entry (POST). state = entitystate.modified; Blog2. SaveChanges ();
Result: failure, error and seventh time
13th time:
Blogdbcontext blog = new Blogdbcontext (); Post = blog. Posts.asnotracking (). Single (t = = T.id = 2); Blogdbcontext blog2 = new Blogdbcontext (); Blog2. Posts.attach (post); Post. Author = blog2. Authors.single (t = t.id = = 1); Blog2. SaveChanges ();
Result: Success
Finally, we conclude that:
1. In the same context, either the direct assignment of the navigation property or the direct assignment of the foreign key attribute, in addition to the navigation properties and foreign key attributes can not be associated with the value of the same, it is possible to succeed;
2. Not in the same context, if you want to complete the cud, it is necessary to ensure that the entity to operate in the detached state, and then the corresponding update, involving the navigation properties, can only take the assignment foreign key properties, cannot be directly assigned by the navigation property, or will be error (this I think is a bug, I do not know what a good solution we have no), if the first in the new context attached, and then the update operation is the same as the 1th conclusion.
Share the results of the CRUD operation experiment on the entity Framework