简体   繁体   English

EF6将相似的表格映射到一个实体中

[英]EF6 mapping similar tables into one entity

I'm making an application that uses legacy database, using EF6 database first, .Net C#. 我正在制作一个使用旧数据库的应用程序,首先使用EF6数据库.Net C#。

The database has two versions: the old and the new one. 该数据库有两个版本:旧版本和新版本。 In the new one some tables were modified and renamed. 在新表中,对某些表进行了修改和重命名。 Eg old one has tables like: work, order, item etc. and new one work_t, order_t and item_t. 例如,旧的表格有:工作,订单,物料等,而新的表格有work_t,order_t和item_t。

The content of corresponding tables is very similar, in the new ones some new columns were added and some were removed. 对应表的内容非常相似,在新表中添加了一些新列,并删除了一些列。 So my application is supposed to work with both kind of databases as I use only the columns that are presented in both versions. 因此,我的应用程序应该可以同时使用两种数据库,因为我只使用两种版本中都提供的列。

I was wondering if there is any decent way to hide those table pairs behind some interface or something to avoid doing 2 implementations of LINQ coding. 我想知道是否有任何合适的方法将那些表对隐藏在某个接口或某些东西后面,以免进行LINQ编码的2种实现。

This is not exactly creating one entity out of 2 tables, because only one table is presented in the database at a time. 这并不是完全从2个表中创建一个实体,因为一次在数据库中仅显示一个表。 I want to have single piece of code to address either one of the similar tables. 我想用一段代码来解决其中一个相似的表。

Here's some pseudo code for what I'm after: 这是我想要的一些伪代码:

    public workDTO GetWork(int workId)
    {
        MyEntities db = new MyEntities();

        // for old version it will go like
        var work = db.work.Where(a => a.id == workId); 

        // for new version it will go like
        var work = db.work_t.Where(a => a.id == workId); 

        return Mapper.Map(work, workDTO);
    }

So the idea is to have just one method and one LINQ implementation for both tables. 因此,想法是两个表只有一种方法和一种LINQ实现。

Yes , You can do it by giving a column attribute in entity framework: 是的,您可以通过在实体框架中提供列属性来实现:

Read here 在这里阅读

Update : 更新:

You can use the .ToTable() method: 您可以使用.ToTable()方法:

modelBuilder.Entity().ToTable("t_Department"); modelBuilder.Entity()。ToTable(“ t_Department”);

Source: MSDN: http://msdn.microsoft.com/en-us/data/jj591617.aspx 来源:MSDN: http//msdn.microsoft.com/en-us/data/jj591617.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM