Entity Framework Code First model separation from domain - entity-framework

Entity Framework Code First best practice question?
Hi All I am using EF codeFirst 6 on an NTier app.
I have found that poco object that I am using to map to EF are really EntityFramework specific. Let me give you an example
If I want to add a property that is not related to EF in the object ,EF does not like it.
I Read you can put the "NotMapped" attribute however it start making this object difficult to maintain .
Also there might be developers that are not familiar with EF and that will not understand the issue.
My question is it good practice to keep EF Entity Models separate and have a dto to convert to/from to a Domain Model where
a developer can do what he likes with it without interferring with EF Model which is clearly a 1 to 1 with the tables in the database
Any Suggestions?

Your problem could be resolved by using the Fluent API approach instead of the Attribute-based (Annotations) approach. See Entity Framework Fluent API.
You would configure your entity mappings in the DBContext rather than in the entity classes.
From the above linked article:
Specifying Not to Map a CLR Property to a Column in the Database
The following example shows how to specify that a property on a CLR
type is not mapped to a column in the database.
modelBuilder.Entity<Department>().Ignore(t => t.Budget);
that would mean "ignore the Bugdet property in the Department entity."

Related

How does EF codefirst know which model class to create a table for?

I was not able to find the answer to this online - please link me if I've overlooked any resources.
I understand how Entity Framework's codefirst works. The question is: how does EF know which model class to create a table for and which model class to just treat as a class?
For example, in the sample MVC4 application that comes from creating a new MVC project with VS 2012 Express Developer, there are classes (LocalPasswordModel, LoginModel, RegisterModel, etc) in the Account Model that have no tables, and EF knows not to generate tables for these classes.
How does EF know this?
Entity Framework looks at your DbContext class, and creates a table for each DbSet<T> property that you define.
EF won't even see any class which is not referenced by the DbContext.

Any Fluent API tutorials that use EF Database-First approach to explain the subject?

There are many tutorials on Fluent API, but they all explain it using Entity Framework Code-First code examples. Since I don't know Code-First, do you know of any Fluent API tutorials that would explain the subject using EF Database-First approach?
Thank you
There are no tutorials which would explain the Fluent API together with Database-First approach because Fluent API is made only for Code-First approach. You don't need the Fluent API if you want to create your model via Database-First.
Fluent API (together with Code-First data annotations and conventions) is a tool to define model details in code, such as string length, if a property is required or the type of relationship - many-to-many, one-to-many, etc. When using Database-First or Model-First the EDMX file has the same purpose - it contains all the details and mapping definitions of your model. Fluent API (+ data annotations and conventions) replaces the EDMX file only when using Code-First.
If you create the model via Database-First or Model-First, you will have an EDMX file representing your model. You can apply the T4 DbContext Generator to this EDMX file. The generated files have two characteristics being different from Code-First:
The generated connection string contains a section refering to the EDMX metadata which will be embedded into your assembly:
connectionString="metadata=res://*/Model.csdl
|res://*/Model.ssdl
|res://*/Model.msl;
..."
The generated context DbContext will have an overridden OnModelCreating method which just throws an exception:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
As long as you leave the metadata section in the connection string, EF won't even call OnModelCreating or any code in Fluent API in this method. The metadata section tells EF that your model is DB- or Model-First and that the metadata are defined in the embedded EDMX and not in Fluent API.
However, you can remove the metadata section from the connection string, remove the UnintentionalCodeFirstException and write code with Fluent API in OnModelCreating. You can follow this procedure to create an initial model via Database-First and then build on this initial model for further development with Code-First.
At this point you are not working anymore with Database-First, but Code-First instead and everything you read about Fluent API is valid for you.
There is an interesting post about accomplishing some of the “Database-First” objectives without employing the actual “Database-First” methodology per se.
http://agilenet.wordpress.com/2011/04/11/entity-framework-4-1-rc-with-an-existing-database/
The author uses “Code-First” and “Fluent API”, but disables the auto-generation and seeding of databases and tables.
He shares a sample that shows “how to create an entity model, then manually create your database and then map those entities to your database. Finally it shows using the DatabaseContext to save and retrieve entities”.
The part where he creates a “configuration class for each entity which maps between the entity and the database” is pretty cool. That step replaces the “edmx” files that would be generated when employing a formal “Database-First” approach.
This tutorial in a 6-part Database First tutorial series includes some fluent API examples:
http://www.asp.net/web-forms/tutorials/continuing-with-ef/using-the-entity-framework-and-the-objectdatasource-control-part-3-sorting-and-filtering

Can Entity Framework generate the DAL code?

I know that entity framework has a database first approach. Now the question is whether it can generate the DAL (data access layer) code (not the models) for me.
When using a Object Relational Mapper (ORM), you don't typically have CRUD code in the traditional sense. Rather, it abstracts those operations into more object oriented operations.
For example, you don't "insert", you add the model class to the table, then save changes. The ORM automatically generates the SQL needed to make the Object model match the data model.
So my point is, your question displays a basic lack of understanding of how ORM's work and how they relate to data models. You should probably do a little reading.
I'm not sure what you mean specifically by "DAL code", as that's a rather ambiguous term. I would consider your Entity types part of the DAL.
When you use a model-first or database-first approach, the Entity Framework tools can auto-generate a context class from your model .edmx, which will inherit from ObjectContext. It's easy to customize the generated context class with T4 templates by finding one online that already generates from a .edmx, and modifying to your liking.
Code-first development uses the DbContext, which is not typically auto-generated. Please see this post on Scott Gu's blog for more details on this.

ASP.NET MVC 3 and the Entity Framework with MySQL and LINQ

I have downloaded ASP.NET MVC 3 and I am now ready to use a MySQL database as my source of data.
I have understood that the Entity Framework will allow me to abstract the actual sql-business out, and I can wrap my linq queries in a repository pattern.
I have tried to follow http://pattersonc.com/blog/2009/04/01/using-mysql-with-entity-framework-and-aspnet-mvc-%E2%80%93-part-i/ but when I get to the place reading
var result = from p in mySqlEntities.ProductSet select p;
I realize that I don't have such a ProductSet.
So, is this the right way of doing things, or is there some easier or more preferable way?
Every entity in the Domain Model will have a set, you might not have a 'product' set but if you had a product table, it definitely would.

Entity Framework 4.0 Mapping POCOS with different property names from db fields names

I'm a newbie to ADO.Net Entity framework 4. I have a set of pocos which I need to map to a legacy database. The problem is that the db field names are different to the poco property names. eg. db field name = 'cusID' and poco property = 'CustomerID'.
What is the best way to map these?
This is exactly the problem EF mapping is designed to solve.
Your POCO class need to match your 'conceptual model'... Not your 'data model'.
If in EF you build your model from the database you simply need to rename your entity properties. Doing this changes the conceptual model - to match your POCO classes - but leaves the storage model unchanged, and sets up the appropriate mappings.
Entity Framework CTP4 has a new feature called Code First that allows you to map POCO property members to database table column names. This blog article may be what you are looking for,
http://theminimalistdeveloper.com/2010/07/28/how-to-map-pocos-to-existing-databases-in-entity-framework-4-0-code-first-and-asp-net-mvc-2/
Additionally, EF CTP 5 - which will be released in the next few weeks - has a better API to fluently configure your own conventions to map your POCO domain classes to existing database structures.
Hope this helps.
Update Here is the new article that discusses how to achieve this in EF4 CTP5