Entity Framework Returns NULL linked object C# -


i use entity framework in program, , have problem when record being removable table, table linked objects come null.

instead of doing

waittravel = db.waittravels                 .where(w => w.suggesttravelid == suggesttravelid &&                            w.wantedtravelid == wantedtravelid)                .first();  if (waittravel.wantedtravels.statustravelid != 1) 

i should that:

 if (db.wantedtravels.where(w => w.id == waittravel.wantedtravelid).first().statustravelid != 1) 

know me?

i believe asking why waittravel.wantedtravels null in if statement. because missing include statement , not have lazy loading enabled.

see ef documentation on loading related entities additional options on how can accomplish this. easiest, , imo best way, explicitly use include when know want retrieve related property/collection.

waittravel = db.waittravels                 .where(w => w.suggesttravelid == suggesttravelid &&                            w.wantedtravelid == wantedtravelid)                .include(w => w.wantedtravels) // added                .first(); 

if not asking please clarify question.


Comments

Popular posts from this blog

ios - Is 'init' forbidden as *part* of a variable name? -

file - Python: AttributeError: 'str' object has no attribute 'readlines' -

c# - Get the Class name in a class with atribute inside a attribute method -