Share a model between multiple edmx files (Entity Framework 4.0) - entity-framework-4

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?

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.

Related

How to add business logic to generated entities in entity framework 6

In previous versions of EF, the use of partial classes extending entities with business logic is well documented. However in EF6, T4 templates are used to generate the entities in separate files for each class (each filename matching it's class name). This leaves no place for partial classes as they would have the same filenames.
So how do we extend the classes to add methods and meta annotations?

How to use entity framework in other modules?

I am currently developing project using C# MVC and entity framework, I want to use the entity framework in other modules ,
i.e security module , Utility module ,
i want to call the db using the entity frame work, how do i do this ?
i am new to this are please explain in detail, idea is to break the project into presentation layer, business layer and data access layer..
i don't know how to archive this.
Try this way,
There are three ways to work around entity framework, Database First, Model First & Code First.
Database First: If you already have database, then entity framework can generate a data model that consists of classes & properties that correspond to existing database objects such as tables & columns.
The information of database structure, conceptual data model & mapping between them is store in the xml in an .edmx file.
Model First: If you don't have database, you can start creating model using vs entity framework designer. This approach also use the .edmx file.
Code First: In this approach, we don't need .edmx file. Mapping between store schema & conceptual data model is represented by code, handled by code convention & special mapping API.
Here I have used the Database First approach.
In order to use the Dal class lib, add the reference in the business logic layer and initialize the entities class. For example
Find the entity framework object.
Initialize the entity framework object in other class lib.
FrameworkEntities entities = new FrameworkEntities();
Please let me know, if you want to use model first or code first approach.
Get started from the below link for the Entity Frame Work
http://msdn.microsoft.com/en-US/data/ee712907?

Error in Model classes after creating Model.Context and Model classes in ASP.Net MVC 4

I have generated xxxModel.Context and xxxModel template (in Models) from an existing database using EF 4.1 on ASP.Net MVC 4 application. After that when I build the project it gives the error for all the Model classes (POCO) saying "The type file name already contains a definition for memeber variable name". Where am I going wrong?
Thanks for help.
Clearly the file DESE.cs (and others) already contains identically named classes with properties - which are clashing with the types generated by EF. CC_Names.cs, for example, is being output by a text template - so I'm assuming you've got more than one code-generation strategy going on here from the same database.
And then you also have issues where you've re-declared the partial class CorpCostEntities again in another file with a different base to the one set by the EF code generator.
I think you might need to decide whether you want to use edmx code generation or the text-templating approach and stick to it :)

ASP.NET MVC 2 Validation: Metadatatype can't be added to standard POCO CLR classes - what's an alternative?

I am using Entity Framework and generating my POCO classes via T4 - these classes inherit from nothing and are very plain and simple (created via template in vs 2010)
I tried using the Metadatatype Attribute so I could create a buddy class but when I did this I no longer was able to see my properties... if I removed the attribute! the properties appeared.
Anyway, searching deeper I found this statement from Microsoft
The associated class must be used with EDM or LINQ-to-SQL models because CLR types cannot mark existing properties with new attributes. If you are working with CLR objects directly, sometimes referred to as Plain Old CLR Object (POCO) types, you can apply the attributes directly to the model
So it appears it doesn't work. Anyway it's difficult for me to insert my Data Annotation on the MODEL itself because it's created via T4 hence if I edit it and then re-run the tool it will remove all my changes.
There is a pretty strong consensus around SO and the MVC blogosphere that you shouldn't annotate your business/crud/domain classes with attributes. Not only is your entire MVC stack becoming dependent upon your business/database classes but you'll quickly end up with multiple context scenarios ( same Model, different validation rules ) that are impossible to validate with just a single model.
Use separate view models for your screens, annotate those.
Based on your comment: "Data Annotation on the MODEL itself because its created via T4 hence"
What I'm trying to say is put your dataannotations on your viewmodels, leave your POCO models alone.

How does MS Entity Framework map from the conceptual model to CLR types?

Given an Entity Data Model (EDMX) with "Code Generation Strategy" set to "None", how does EF determine which CLR types to map the conceptual model to?
I think I read somewhere that it just probes the assembly for types that match the conceptual model, but that was in reference to a CTP edition of EF. Is this still the case?
Can I control this process somehow?
In particular, I am in a scenario where I am moving a substantial codebase from using Linq2SQL to using POCO with EF 4.0. Thus, I have the Linq2SQL classes as well as my POCO classes, for now residing in the same assembly, but in different namespaces. I'm trying to have a smooth migration from L2S to EF so I would like to have the two frameworks run in parallel for a while. However, I get a runtime-error saying
The mapping of CLR type to EDM type is
ambiguous because multiple CLR types
match the EDM type 'SomeType'.
Previously found CLR type
'SomeNamespace.SomeType', newly found
CLR type 'SomeNamespace.POCO.SomeType'
where SomeNamespace is the namespace of the L2S entities. This error makes sense if EF is just probing for all types matching the conceptual model. Can I confine EF to only probe the SomeNamespace.POCO namespace? Or should I put my POCO objects in another assembly? Or should I take a third approach?
Thank you.
Notice this comment from the ADO.NET team blog:
Jeff 25 Feb 2010 9:10 AM #Derek
This is intentional. You can put your
POCO classes in whatever namespace
you'd like. The Entity Framework's by
convention mechanism for detecting
which properties on the entity match
the properties of entities in your
model does not use Namespace. What
matters is that the type name (without
namespace) matches the EntityType name
in your model (edmx/csdl file).
One area to watch out for is if you
have multiple types with the same name
but in different namespaces. Because
we don't account for namespace, we
detect that we've found multiple types
and we throw an exception.
Jeff
See this article:
link text

Resources