【问题标题】:Designing model for a field in code first entity framework that is Null for certain drieved entities?在代码优先实体框架中为某些驱动实体为空的字段设计模型?
【发布时间】:2014-10-04 08:32:38
【问题描述】:

我在程序员那里发布了我的数据模型片段,询问我的代码有什么缺点,并得到一些答案,是的,存在潜在问题。

https://softwareengineering.stackexchange.com/questions/258038/inheritance-is-a-null-property-in-the-parent-a-bad-practice

不好的做法是Amount 字段。它将Null 返回给某些实体,结论是我应该从父类中删除Amount 字段并在我需要它的类中使用它。

现在首先使用代码的方式是使用下面的模型的数据表正是我想要的方式。行字段如下所示:

Id, Title, Description, Amount, other fields

每个实体种子上的Amount字段为零或Null,这取决于实体是否实际使用该字段,并且类使用该字段的指示符是该类必须具有@的覆盖987654328@.

当我删除 Navigation 类中的虚拟 Amount 并在类中放置一个 Amount 字段时会发生什么,我实际上存储了一个金额,数据库中的表如下所示:

Id, Title, Description, Amount, Amount1, Amount2, other fields

这是一个不受欢迎的结果,原因有很多。

问题是我该怎么做才能让数据表中只有一个Amount 字段?

public class Navigation
{
    int Id { get; set; }
    string Title { get; set; }
    string Description { get; set; }
    ObservableCollection<Navigation> Children { get; set; }

    public virtual decimal? Amount
    {
        get { return null; }
        set { value = null; }
    }
}

public class C1 : Navigation { }

public class C2 : Navigation { }

public class C3 : Navigation
{
    private decimal _amount;

    public override decimal? Amount
    {
        get { return _amount; }
        set { if (value != null) _amount = (decimal)value; }
    }
}

public class C4 : Navigation
{
    private decimal _amount;

    public override decimal? Amount
    {
        get { return _amount; }
        set { if (value != null) _amount = (decimal)value; }
    }
}

【问题讨论】:

    标签: c# entity-framework inheritance code-first


    【解决方案1】:

    试试这个,但不确定它是否会起作用。您使用的是什么版本的 EF?

     protected override void OnModelCreating(ModelBuilder modelBuilder)
     {
        modelBuilder.Entity<C3>().Property(p => p.Amount).HasColumnName("Amount");
        modelBuilder.Entity<C4>().Property(p => p.Amount).HasColumnName("Amount");
     }
    

    【讨论】:

    • 6. + 英法。是的,我在想如果有解决方案,那就是它。我会试一试。
    • @Jon 告诉我进展如何。
    • 今天早上起来有个解决办法,本来想试试的,后来在fluent api里试了一下。
    【解决方案2】:

    解决方案是更改继承。我从导航中删除了金额字段。将 C3 中的 Amount 字段变为虚拟,并将 C4 更改为从 C3 继承。工作正常。

    public class Navigation
    {
        int Id { get; set; }
        string Title { get; set; }
        string Description { get; set; }
        ObservableCollection<Navigation> Children { get; set; }
    
     //  public virtual decimal? Amount
     //   {
     //       get { return null; }
     //       set { value = null; }
     //   }
    }
    
    public class C1 : Navigation { }
    
    public class C2 : Navigation { }
    
    public class C3 : Navigation
    {
        private decimal _amount;
    
        public virtual decimal? Amount
        {
            get { return _amount; }
            set { if (value != null) _amount = (decimal)value; }
        }
    }
    
    public class C4 : C3
    {
        private decimal _amount;
    
        public override decimal? Amount
        {
            get { return _amount; }
            set { if (value != null) _amount = (decimal)value; }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-12-24
      • 2013-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-03
      • 2012-05-29
      • 1970-01-01
      相关资源
      最近更新 更多