简体   繁体   中英

Entity Framework 6 - reuse same table in multiple edmx files

I have been tasked with upgrading our software to use EF6. Previously it was using a combination of EF4 & 5.

We use the database first approach. The upgrade went smoothly as far a code changes go, but after running the application and doing a query the following error is thrown.

Schema specified is not valid. Errors:

The mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type 'tblAccountMaintenance'.

Previously found CLR type 'DALUsers.StatusDB.tblAccountMaintenance', newly found CLR type 'DALUsers.AccountsDB.tblAccountMaintenance'.

The class in questions, tblAccountMaintenance , is generated inside multiple .tt files. The classes are references to the same table, just referenced in different .edmx files.

Simply removing one of the references is not a good option in this case as we have used a similar strategy with several other tables and would require thousands of lines of rewritten code.

What do I need to do to fix this in EF6?

So it turns out the the issue (using the same tables, with the same name in multiple edmx files in the same project) is related to the fact the the .tt files contain new objects that derive from dbContext - and this is a limitation/constraint that is specific to dbContext .

I downloaded this plugin: https://visualstudiogallery.msdn.microsoft.com/66612113-549c-4a9e-a14a-f629ceb3f89a

Which allows me to create EF6 .tt files that derive from EnityObject instead which does not have this constraint. It also makes it so I don't have to update all of my code to use the newer dbContext methods which is a plus.

As a note to others viewing this - this is probably not the best answer if you are starting with a newer project or have a small number of edmx files/tables as EntityObject is not as robust as dbContext , however this is a good band-aid fix - especially if you are like me and going to have to do a complete rewrite when EF7 comes out.

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