【问题标题】:Create a Class for extend Model Classes in EF code first首先在 EF 代码中为扩展模型类创建一个类
【发布时间】:2016-12-24 07:45:15
【问题描述】:

我有一个模型类,例如,我的数据库中有 3 个属性和相应的表。

public partial class Person
{
    public string prop1 {set; get;}
    public string prop2 {set; get;}
    public string prop3 {set; get;}
}

现在我想要派生属性,例如使用 prop1prop3 创建:

prop4 = prop1 + prop3;

我想将Person 扩展为prop4。我为Person 创建了一个部分类以拥有prop4

public partial class Person
{
     public string prop4 {set; get;}
}

但是当我运行应用程序时,我得到了这个错误:

列名“prop4”无效

我把上面的类改成这样:

public class PersonViewModel : Person
{
    public string prop4 { get; set; }
}

列名“鉴别器”无效。\r\n列名“鉴别器”无效。\r\n列名“鉴别器”无效。\r\n列名“prop4”无效

但是如果我创建一个这样的类,每个想法都会好的:

public class PersonViewModel
{
    public string prop1 {set; get;}
    public string prop2 {set; get;}
    public string prop3 {set; get;}
    public string prop4 {set; get;}
}

但是使用这个解决方案我应该重写 ViewModel 类中的所有属性。还有其他方法吗?

谢谢

【问题讨论】:

    标签: entity-framework model-view-controller ef-code-first entity-framework-6 code-first


    【解决方案1】:

    使用 NotMapped 属性或 fluent API Ignore 方法Ignoring a class property in Entity Framework 4.1 Code First

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多