Reputation: 2050
I have 3 (in future possibly more) large projects that are quite different from each other however there is around 15+ tables that they all have in common (User, Role, UserPreferences, UserLog...etc). Any changes on these common tables as well as changes on entities always apply across all projects.
As of now each project has a separate EDMX
which duplicates these entities for each project. Any change to these common entities or tables then requires replicating the same changes 3 times.
Is there a way to define these entities in separate project and then reuse
them in each project?
Keep in mind that these shared entities have relationships with other entities within each project (which is information that is specific to each project).
What I would like to do is in EDMX
to reference an object (from another EDMX) instead of just tables/views/sps
(which as far as I can tell is not possible).
Note: I found some older blogs on ADO.NET but so far I didn't find any real solution.
Upvotes: 2
Views: 1575
Reputation: 109255
You can't make references between objects of different EF models. The reason is that EF will try to resolve the associations as SQL joins and therefore it needs mapping information of all involved classes.
Dreaming away I could imagine that it should be possible to merge models and define associations between their classes, but yeah....
So the best you can do, when working with edmx, is use both models separately in your project and use in-memory (linq to objects) joins where applicable. Not the most performant solution, I'm afraid.
If you could switch to code-first you could have the common classes in a separate class library, complete with ready-made EntityTypeConfiguration
classes and use those in your projects. Then it is just a matter of referencing the common class library and adding instances of the type configurations to the DbContext
class of the local project. You could even map associations to the common classes, as long as the foreign keys will be part of the local model. (Of course you won't be able to add foreign key fields to the shared classes).
Upvotes: 1
Reputation: 4032
I would stand up an API for the shared resources and each project needing these could consume the API. The shared resources would have it's own project and 1 edmx for these entities. That way you're always making changes to these shared resources once and all three get the update. The downside is you can't introduce these changes one at time like you can now, however I would very much be in favor of the ease of maintenance and the reduction of duplicate code!
I have a similar setup with OData now and it works great! of course that was before the new Web API architecture MS put out. Here's a quick tutorial on it (http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api)
Upvotes: 1