0

I have read this question and i understand that.

Let's say i have tow entities and they have One to Zero or One relatinship. Let's say I have such code:

 myFirstEntity.PrimaryKey = 5;
 myContext.Entry(myFirstEntity).State = System.Data.EntityState.Unchanged;

After the second line, myFirstEntity.MySecondEntity.PrimaryKey value will also be 5.

So, it is OK.

But, it is not wokring if make changes reversely:

 mySecondEntity.PrimaryKey = 5;
 myContext.Entry(mySecondEntity).State = System.Data.EntityState.Unchanged;

After the second line, mySecondEntity.MyFirstEntity.Pk value is still 0.

Community
  • 1
  • 1

1 Answers1

0

Entity Framework will retrieve your entities from the database but will not automatically load all the properties for exemple if lazy loading is "off" (if the property is not virtual).

Also, if you change the Id of a property, you have to save the changes. With the given code, we don't know how you retrieved the entities, are they tracked ?

If the entities are not tracked, you have to manually assign the value of your FK.

AMS
  • 439
  • 2
  • 12
  • State if the ain entity is Added. Entities are not tracked. – İhsan İlicali Jun 15 '15 at 10:23
  • My understanding with the given info is that mySecondEntity has a fk to myFirstEntity. So assigning an Id to myFirstEntity "propagate" to the mySecondEntity property. The reverse cannot be deduce by EF so it is not changed. I'm just trying to guess here. Maybe show us the model and the fluent annotation. But if an entity is not tracked EF wont load for you the MyFirstEntity property and FK. – AMS Jun 15 '15 at 12:42