Code First 实体框架 - 列返回空,因为 EF 更改了名称以包含 _ID

问题描述

实体框架更改了数据库中的列名,并没有给我它的价值。

这是我的课程:

public class Settings
{
    [Key]
    public int ID { get; set; }
    public string Setting { get; set; }
    public string MoreDetail { get; set; }
    public SettingTypes Type { get; set; }
    public SettingGroups SettingGroup { get; set; }
    public int? MinMembership { get; set; }
    public string DefaultValue { get; set; }
    public int? ParentID { get; set; }
    public int Order { get; set; }
}

public class SettingTypes
{
    [Key]
    public int ID { get; set; }
    [MaxLength(35)]
    public string TypeName { get; set; }
}

public class SettingGroups
{
    [Key]
    public int ID { get; set; }
    [MaxLength(35)]
    public string GroupName { get; set; }
}

数据库中您可以看到它更改了两列的名称

enter image description here

当我尝试遍历结果时,类型为空:

enter image description here

我如何检索这个值?我试过重命名类和数据库中的列,但这只会破坏更多的东西。处理这个问题的正确方法是什么?

谢谢!

解决方法

该死,我想通了。花了很多时间这样做。

就像在属性中添加“虚拟”一样简单:

        public virtual SettingTypes Type { get; set; }
        public virtual SettingGroups SettingGroup { get; set; }

现在我可以这样处理: setting.Type.TypeName

希望这可以为其他人节省一些时间。