Associate tables from different databases - Entity Framework

不羁岁月 提交于 2020-02-19 20:22:40

问题


I'd like to know how to (if possible) associate tables from different databases using entity framework. I have one edmx for my "xyz" database and another for my "abc" I need to associate them using EF. I know how to fix this using FKs, but that's not what I want, I'd like to be able to do that using the visual editor, not adding, by hand, a FK to my DB.

Here's a pratical example of what I'd like to accomplish:

Table User - database: abc
Table Message - database: xyz

I'd like to associate User to Message and vice-versa.

Could anyone help me?

Thanks!


回答1:


You can query them together with Linq-to-Objects, but not with L2S or L2E.

A context is wrapped around 1 Db-connection, you cannot track/update entites from multiple db's.

A possible workaround is to 'link' to one of the Db's from the other. MS-SQL can do this. To the EF it would appear 1 database.




回答2:


There is no way to achieve this using the edmx design surface in EF.

You could encapsulate this association within your domain model and provide a distributed transaction across the two EF Contexts. We've implemented the latter with an EF Context "container" class that given an internal collection of contexts uses a TransactionScope when it contains more than one context (this relies on the IUnitOfWork abstraction to work effectively).



来源:https://stackoverflow.com/questions/5016753/associate-tables-from-different-databases-entity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!