Why EF navigation property return null?

后端 未结 5 636
执笔经年
执笔经年 2020-12-17 16:12

I have two model 1)

public class Indicator
{
    public long ID { get; set; }
    public string Name { get; set; }
    public int MaxPoint { get; set; }
             


        
5条回答
  •  轮回少年
    2020-12-17 16:49

    1) Is Lazy Loading enabled? If not, you need to explicitly load your navigation properties with the '.Include' syntax.

    2) Are you sure EF should be able to detect that relation? Did you use Code First or Database First?

    Edit: 3) Are you sure there is data in your database and that the foreign key from Indicator to IndicatorGroup has a value for that specific record? I am saying this because the value "null" is valid if there is simply no data.

    P.S. If you do not see a foreign key on Indicator called "IndicatorGroupId", there might be an "IndicatorId" on the table "IndicatorGroup", in which case - going from the names you provided - your database is misconfigured and you will need to use fluent syntax or data attributes to instruct EF on how to make the foreign keys.

提交回复
热议问题