微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。