【问题标题】:EF4 CTP5 CodeFirst modeling problemEF4 CTP5 CodeFirst 建模问题
【发布时间】:2011-06-05 17:40:32
【问题描述】:

我是 EF4 CTP5 的新手,我想在下面创建一个模型,每个表都是相同的字段(id、文本、值),我不想把它们都放在一个表中,我可以使用基类吗?但我不知道我的域模型如何?

public class BaseSearchType
{
   public int Id {get;set;}
   public int text{get;set;}
   public int value {get;set;}
}

public class BooleanSearchTypeTable :BaseSearchType
{

}

public class JobStatusSearchTypeTable:BaseSearchType
{

}

public class PersonStatusSearchTypeTable: BaseSearchType
{

}

表格

BooleanSearchTypeTable 
-----------------
id text value
1   All  0
2   Yes  1
3   No   2

JobStatusSearchTypeTable
-----------------
id text value
1   Open  0
2   Closed  1
3   Approved   2
4   Rejected 3
5   Waiting 4

PersonStatusSearchTypeTable
id text value
1   Work 0
2   Seek 1
3   Vacation 2

【问题讨论】:

  • 您应该升级到发布版本。已经出了一个多月了。英孚 4.1

标签: c# entity-framework entity-framework-4 entity-framework-4.1 ef-code-first


【解决方案1】:

在您指示 EF 这样做之前,它们不会被映射到一个表中。简单地定义你的类:

public abstract class BaseSearchType
{
    public int Id { get; set; }
    public string text { get; set; }
    public int value { get; set; }
}

public class BooleanSearchTypeTable : BaseSearchType
{ }

public class JobStatusSearchTypeTable : BaseSearchType
{ }

public class PersonStatusSearchTypeTable : BaseSearchType
{ }

您的上下文如下:

public class Context : DbContext
{
    public DbSet<JobStatusSearchTypeTable> JobStatuses { get; set; }
    public DbSet<BooleanSearchTypeTable> BooleanStatuses { get; set; }
    public DbSet<PersonStatusSearchTypeTable> PersonStatuses { get; set; }
}

【讨论】:

  • 你能不能只对基类进行 dbset,例如 public DbSet BaseSearchType{ get;放; }
  • 一旦你这样做了,你就拥有了实体继承,并且所有实体都映射到了 OP 不想要的单个表。您可以将其修改为其他类型的继承,但只有在需要时才应该这样做。
【解决方案2】:
猜你喜欢
  • 2011-07-02
  • 1970-01-01
  • 2011-07-15
  • 2011-07-21
  • 2023-04-02
  • 1970-01-01
  • 2011-06-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多