实体框架 - 同一界面的多个类(表) - DbSet暴露?

时间:2015-10-12 12:51:22

标签: c# entity-framework

public interface IQuestion {
    // A couple of common properties go here, like Name, etc
}

/* Now follows multiple implementations of my interface, each of which has some custom properties */

public class TextBoxQuestion: IQuestion {
  public int MaxLength { get; set; }
}

public class DateQuestion: IQuestion {
  public DateTime MaxDate { get; set; }
  public DateTime MinDate { get; set; }
}


public class MyRepo : DbContext {
 **public DbSet<IQuestion> { get; set; } // Problem**
}

我如何实现我的DbContext?我不能公开一个接口,因为EF需要具体的实现,这当然有意义,因为我还必须生成一个数据库。

我应该在DbContext中公开我拥有的每个IQuestion实现吗?

什么是更好的设计 - 一个包含所有可能属性+鉴别器列的大表,或每个实现的单独表?

编辑:

我添加了

public class BaseQuestion : IQuestion {
    // Implements the common properties, plus:
    [Key]
    public int Id { get; set; }
}

并将Context更改为:

public class MyRepo : DbContext {
     public DbSet<BaseQuestion> { get; set; }
}

EF现在会生成一个巨大的桌子&#39; + Discriminator专栏给我。表格自动&#39;包括我的解决方案中继承BaseQuestion的所有类的属性。这是最好的做法吗?

0 个答案:

没有答案