How to determine if Navigation Property in the Entity Framework is set without loading the related records

后端 未结 2 815
庸人自扰
庸人自扰 2021-01-04 11:51

I\'m not sure about Navigational Properties in EF 4 so I would kindly ask you an explanation.

Lets imagine this scenarios:

A)

I have two Entities

相关标签:
2条回答
  • 2021-01-04 12:35

    Here is a way to check if related records for an entity is loaded or not.

    For entity where you have multiple records related to entity you can check like below.(One to many relationship)

    myDbContext.Entry(MyEntity).Collection(x => x.NavigationalProperyName).IsLoaded
    

    And If have only one record related to entity, then you can check like below.(One to one relationship)

    myDbContext.Entry(MyEntity).Reference(x => x.NavigationalProperyName).IsLoaded
    
    0 讨论(0)
  • 2021-01-04 12:51

    Do you mean:

    // -to-one relationship
    entity.RelatedItemReference.IsLoaded
    
    // -to-many relationship
    entity.RelatedItems.IsLoaded
    
    0 讨论(0)
提交回复
热议问题