简体   繁体   English

实体框架6-在多个edmx文件中重复使用同一表

[英]Entity Framework 6 - reuse same table in multiple edmx files

I have been tasked with upgrading our software to use EF6. 我的任务是升级软件以使用EF6。 Previously it was using a combination of EF4 & 5. 以前它是结合使用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'. 由于多个CLR类型与EDM类型“ tblAccountMaintenance”匹配,因此CLR类型到EDM类型的映射是不明确的。

Previously found CLR type 'DALUsers.StatusDB.tblAccountMaintenance', newly found CLR type 'DALUsers.AccountsDB.tblAccountMaintenance'. 先前找到的CLR类型为“ DALUsers.StatusDB.tblAccountMaintenance”,新近发现的CLR类型为“ DALUsers.AccountsDB.tblAccountMaintenance”。

The class in questions, tblAccountMaintenance , is generated inside multiple .tt files. 有问题的类tblAccountMaintenance是在多个.tt文件中生成的。 The classes are references to the same table, just referenced in different .edmx files. 这些类是对同一表的引用,只是在不同的.edmx文件中引用。

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? 我需要做什么才能在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 . 因此,事实证明,问题所在(在同一项目中的多个edmx文件中使用相同的表,并且使用相同的名称)与.tt文件包含从dbContext派生的新对象有关,并且这是一个局限性/特定于dbContext约束。

I downloaded this plugin: https://visualstudiogallery.msdn.microsoft.com/66612113-549c-4a9e-a14a-f629ceb3f89a 我下载了此插件: 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. 这让我创造EF6 .tt文件派生自EnityObject代替它没有这个约束。 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. 它还使得它不必更新所有代码即可使用更新的dbContext方法,这是一个加号。

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. 作为其他查看此内容的注意事项-如果您从一个较新的项目开始或拥有少量edmx文件/表,这可能不是最好的答案,因为EntityObject不如dbContext健壮,但这是一个不错的创可贴解决-特别是如果您像我一样,并且在EF7推出时必须完全重写。

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

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