具有相同结构实体框架的多个表

具有相同结构实体框架的多个表

问题描述:

我们有一个包含多个具有相同结构的表的数据库

We have a database with multiple tables with Same structure

键ID ... .....

Key ID ........

密钥ID .......

Key ID .......

表的数量可以根据配置动态变化。

The number of tables can be dynamic based on configuration.

我正在尝试将数据访问层升级为实体框架。我创建了一个代表表结构的类。我的计划是对具有相同结构的所有表使用相同的类。抱歉,我找不到有关如何执行此操作的足够信息。我了解的是,我只能将一个类映射到一个表。

I am trying to upgrade the data access Layer to Entity framework. I have created one class representing the structure of the table. My Plan is to use the same class for all the tables with same structure. Bubt I could not find enough information on how to do this. What I understood is that I can map one class to one table only.

有什么方法可以使用实体框架来实现?

Is there any way to achieve this using entity framework?

简单的方法:拥有一个具有所有属性的抽象基类,并映射具体类型:

The easy way: have an abstract base class with all the properties, and map concrete types:

public abstract class BaseClass
{
   public int Id { get; set; }
   public string StringField { get; set; }
   /* Other fields */ 
}

[Table("Table1")]
public class Table1 : BaseClass
{
}

[Table("Table2")]
public class Table2 : BaseClass
{
}

我不是在回答这个设计的好坏(我不会说你喜欢它,我只是喜欢回答这个问题,

I'm not answering whether that design is good or bad (I wouldn't say I like it as you explained it), I'm just answering the question