c# - NavigationProperty null on added foreign key -
whenever add foreign key entity previous entity setting foreignkey-id, associated object null.
let me explain this: 
in previous step i've set addressid property 28 , have saved entity context calling context.savechanges().
now why addressid filled, address navigationproperty (which should address object of address table address.id == 28) null?
entity frameworks (ef) work design. updating foreign key never updates navigation property.
however, updating navigation property update key. note in case address entity should come same context. if not .savechanges() consider address entity new , try add in database.
as question of method better, well, depends!
- updating key straightforward , have been doing along using data transfer objects (dtos) or plain sql. easier newcomers ef grasp , use.
- updating navigation property object based data model. code looks cleaner , more readable. need careful context. in little personal experience ef, find trying update navigation property brings more complexity value, in multi-tier architecture context hidden behind data access layer.
the important benefit of ef, in opinion, in query operations using linq-to-entities. have compile-time syntax check queries , strong typing. can create object-based result set multiple levels of children, data-bind ready without additional code. write sql anymore.
Comments
Post a Comment