简体   繁体   中英

ADO.Net: Sharing entities (.edmx) between multiple projects within a solution

I've got three C# ASP.NET MVC2 projects within a single solution. Names have been changed for simplicity's sake.

The solution is MY_COMPANY_SITES.

It's got three projects in it: CUSTOMER_SITE, INTRANET_SITE, CENTRAL_REPOSITORY.

...Both the CUSTOMER_SITE and INTRANET_SITE projects have their own ADO.Net entity models, generated via EDMX, and connect to their own databases. The CENTRAL_REPOSITORY project contains a third ADO.Net entity model, linked up with its own database.

It is required for my situation that these three databases be separate; would take too long to explain here.

What I need to do: Give both the CUSTOMER_SITE and INTRANET_SITE access to the CENTRAL_REPOSITORY ADO.Net entities, in addition to their own.

ie, the following code should work:

CUSTOMER_Entities custDb = new CUSTOMER_Entities();
CENTRAL_Entities ctrDb = new CENTRAL_Entities();

StoreLocation firstLoc = ctrDB.StoreLocations.First();

List<Order> orders = (from n in custDb.Orders
                        where n.LocationID == firstLoc.LocationID
                        select n).ToList();

...I'd also ideally prefer that if I modify the entity model within the CENTRAL_REPOSITORY, it's just linked to by the other applications so I don't have to copy all my changes among all three.

I figure there must be a "right" way to do this. Could very much use some help understanding how.

Thanks!

UPDATE:

Blindy (below) directed me to find that the answer is just to right-click on the "References" within the CUSTOMER_SITE in the Solution Explorer, and click "Add Reference...", then select the CENTRAL_REPOSITORY project, and click OK. And voila, everything shoud just work.

However, it doesn't actually seem to just work. Specifically, I'm getting this error when I try to access the CENTRAL_REPOSITORY's data entities from within the CUSTOMER_SITE or INTRANET_SITE:

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

...so there's some problem with trying to use the entities from that project, and I'm not understanding what. Any thoughts?

To answer your question, you need to have the appropriate connection string in your configuration of the other two sites, to enable your CENTRAL_REPOSITORY to use it to connect. Have a look in the original site config and copy that connection to the other two sites.

您需要在app.config文件中为每个使用linq到实体模型解决此错误的项目创建一个连接字符串。

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