Entity Framework : update 2 entities with related entity of same key -


"Student" has the navigation property "school" in it.

  Public class students {public long id; The name of the public string; Public school meschool; } Public class school {public long id; The name of the public string; }   

To update the database, the client program passes in two student institutions of the same school. Students. Myschool has two students in different subjects school with the same key.Id is .

How can I update two students in a transaction ?

I thought that two students should be paired with DBKNext and then be changed to modify their state. But the second was unsuccessful with the encryption error: Attaching a unit of type 'school' was unsuccessful because another unit of the same type already had primary key value.

MySchoolId add property to student and MySchoolId set the reference to MySchool faucets before adding students to the context of each student's property myschool context, and then reference.

  Public long term MySchoolId {get; Set; }   

You may have to check whether the school is already present in the context or not. If not, add it to the context, save the reference, and use the latest update id of a school unit to set the value of MySchoolId property .

Alternatively, you can ask for a match school reference and set the property of each student in the unit that came from context or you just made it.

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -