简体   繁体   中英

EF6 mapping similar tables into one entity

I'm making an application that uses legacy database, using EF6 database first, .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.

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.

This is not exactly creating one entity out of 2 tables, because only one table is presented in the database at a time. 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.

Yes , You can do it by giving a column attribute in entity framework:

Read here

Update :

You can use the .ToTable() method:

modelBuilder.Entity().ToTable("t_Department");

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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