简体   繁体   English

Code First Entity Framework - 列返回 null 因为 EF 将名称更改为包含 _ID

[英]Code First Entity Framework - Column returns null becuase EF changed the name to include _ID

Entity framework changed the column name in the DB, and isn't giving me it's value.实体框架更改了数据库中的列名,并没有给我它的价值。

Here are my classes:这是我的课程:

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; }
}

In the DB you can see that it changed the name of the two columns:在数据库中,您可以看到它更改了两列的名称:

在此处输入图像描述

When I try to loop through the results, type is null:当我尝试遍历结果时,类型为 null:

在此处输入图像描述

How do I retrieve this value?如何检索此值? I've tried renaming the columns in the class and in the DB but that just breaks more things.我尝试重命名 class 和数据库中的列,但这只会破坏更多内容。 What's the proper way to handle this?处理这个问题的正确方法是什么?

Thanks!谢谢!

Dangit, I figured it out.丹吉特,我想通了。 Spent way to much time doing so.花了很多时间这样做。

It was as simple as adding "virtual" to the properties:就像在属性中添加“虚拟”一样简单:

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

Now I can address it like: setting.Type.TypeName现在我可以像这样解决它: setting.Type.TypeName

Hope this saves someone else some time.希望这可以节省其他人一些时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM