c# - Entity Framework Lazy Loading Does Not Work -
i migrating project ef 4.0 ef 6.0. trying properties navigated entity after insert new entity. code runs without problem in ef 4.0 when try run in ef 6.0 gettin nullreference exception.
user entity:
public partial class users { public int rid { get; set; } public string name { get; set; } public nullable<int> langref { get; set; } public virtual language language { get; set; } }
language entity:
public partial class language { public language() { this.users = new hashset<users>(); } public int rid { get; set; } public string name { get; set; } public virtual icollection<users> users { get; set; } }
test code:
test1entities testent = new test1entities(); users user = new users(); user.name = "asd"; user.langref = 1;//that id refer english record in language entity testent.users.add(user); testent.savechanges(); string lang = user.language.name;//should return "english". language property seem null
model:
instead of using new users()
try testent.users.create()
. difference object returned proxy class needed lazy loading of navigation properties.
as aisde, doing opposite , setting language
navigation property , not langref
foreign key value should correctly set langref property when call savechanges()
.
see dbset.create method on msdn.
Comments
Post a Comment