Entity framework want to say love you not easy, so many error, so many limitations, how to solve?

Source: Internet
Author: User

First look at the Entity model object diagram that was designed in the form of model one:

Note: There are navigation properties in Entityone: Entitytwo

In the following code in several cases of new operations, will be error, the new will not succeed:

 static void Main (string[] args) {Model1container context = new Model1container (); First case: Entityone has the navigation property Entitytwo, the new must also specify the new Entitytwo, otherwise error: "Model1container.entityones" in the entity participation " Entityoneentitytwo "relationship. Found 0 related "Entitytwo".            There should be 1 "entitytwo". var Objsetone = context.            Entityones;            Objsetone.mergeoption = mergeoption.notracking;            Objsetone.addobject (New Entityone () {ID = 2, P1 = "Pp1", P2 = "Pp2", P3 = "pp3"}); Context.                       SaveChanges (); The second case: sometimes our entitytwo may need to be created first, but this is done with the same error: the entity in "Model1container.entitytwos" participates in the "entityoneentitytwo" relationship. Found 0 related "Entityone".            There should be 1 "entityone". var objsettwo = context.            Entitytwos;            Objsettwo.mergeoption = mergeoption.notracking;            Objsettwo.addobject (New Entitytwo () {ID = 2, T1 = "Tt1", T2 = "Tt2", T3 = "tt3"}); Context.           SaveChanges (); The third case: sometimes our entitytwo may have been created before, and this is just a query to assign a value to the associated attribute when a new entityone is needed: entitytwo, but still reportedWrong: The relationship in the "Entityoneentitytwo" AssociationSet is in the "Deleted" state.           If there are multiple constraints, then the corresponding "Entityone" must also be in the "Deleted" state. var objsettwo = context.            Entitytwos;            var entitytwo = Objsettwo.single (t=>t.id==2); var Objsetone = context.            Entityones;            Objsetone.mergeoption = mergeoption.notracking;            Objsetone.addobject (New Entityone () {ID = 3, P1 = "Pp1", P2 = "Pp2", P3 = "PP3", Entitytwo = Entitytwo}); Context.        SaveChanges (); }

There is only one situation in which new additions can be performed correctly, as follows:

Model1container context = new Model1container ();            var Objsetone = context. Entityones;            Objsetone.addobject (New Entityone () {ID = 2, P1 = "Pp1", P2 = "Pp2", P3 = "PP3", Entitytwo = new Entitytwo () {id=2, T1 = "Tt1", T2 = "Tt2", T3 = "tt3"});            Context. SaveChanges ();

Note that you can only succeed if you complete the new Entityone and associated attribute entitytwo at the same time, but is this the reality? For example: A person has written a few books, it must be someone this entity, and then there will be a book this entity, each writing a book, the author of the book Association attribute should be a person this entity, but according to the above test, add a book must add a person, this is obviously wrong, how to solve it? I do not find a better solution at the moment, the only way is not to navigate, but this is not in line with the principle of object-oriented, so please give us a lot of advice, thank you!

Entity framework want to say love you not easy, so many error, so many limitations, how to solve?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.