Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share a model between multiple edmx files (Entity Framework 4.0)

I am searching for the solution of the problem i am and probably many must be facing.

Curently i am working on a application containing nearly 400 tables. Application consists of seven Class library projects( StudentInfo, library, Fees etc) and each has its own .edmx file(consisting 50 tables) with code generation strategy=default And a single web application project which references the class library projects.
There are around 15 tables which are common and will be present in .edmx file in each class library project. The namespace of the classes/models is the same(Campus) in all the .edmx files.

I have created a partial class namely School(which is one of the commom table/model) which contains some methods.

However the following compile time error is thrown The type 'Campus.School' exists in both 'D:\Project\Campus\CampusStudent\' and 'D:\Project\Campus\CampusLibrary\bin\Debug\CampusLibrary.dll'

The solutions that were suggested by other members
1)Have separate namespaces for each of the .edmx files.
2)Use different names to the models namely StudentSchool, LibrarySchool etc.
Both solutions will force me to duplicate the common classes with its methods in each of the class library projects. Can anybody help me?

like image 754
Ravi Shet Avatar asked Feb 07 '12 06:02

Ravi Shet


1 Answers

There can be a way if you are using POCO T4 template for current entity generation. POCOs in EF can be any class in any namespace which have the same name as entity in your EDMX and which have all properties with the same name as entity in EDMX (including same types and accessibilities for getters and setters).

Define your 15 shared classes in another assembly (you must follow those mentioned POCO rules) and reference it by all library projects. Once you have this assembly create your own version of POCO T4 template which will not create new class files for those shared entities and instead use classes from referenced assembly.

The other option is manual creation and maintenance of all those 400 classes and EF context types. That is what you will do if you use code only mapping (aka code-first) and you will not have these problems.

like image 148
Ladislav Mrnka Avatar answered Sep 22 '22 03:09

Ladislav Mrnka