Entity Framework 5 - POCO template Data Annotations - entity-framework

I downloaded EF5 which comes with T4 templates that create POCO classes. This works for me (database first) but there is no basic data annotations like [Required] or [MaxLength] that could easily come from EDMX.
My first quess is to edit the T4 template but I'm worried I'd have to update it with every new EF version update (when template changes), not to even mention that I'm sure T4 for these annotations was already coded.
Can anyone point me to the right direction, either to existing modified EF5 templates or other way to auto-generate "basic" data annotations from EDMX model? (and I don't mean Fluent API)

A little bit late, I know, but the answer is here:
T4 Metadata and Data Annotations Template
This T4 template handles generating metadata classes from an Entity
Framework 4 model and decorates properties with data annotation
attributes such as [Required] and [StringLength]. The [DataType]
attribute is also applied when appropriate. It'll also generate
ErrorMessage values based upon property names for required fields.

Related

Difference between generate an entity data model in EF4 and EF6

Before I used EF4 to generate my entity data model from an existing database. I can do CRUD operations on every generated entity, because the entities context class with all the methods is automatically generated. Now I have upgraded my project to EF6, deleted the files created by the EF4 data model wizard and generated it again using EF6 data model wizard. Now I get a T4 template file with a context class below it, a T4 template file with for every entity a class with code, a empty designer file and an edmx diagram file. But there are no methods such as AddObject, DeleteObject, SaveChanges generated. How can they be generated as it did before with EF4?
EF6 should still generate a context for you. It provides the mentioned methods. Are you sure you just missed in amongs all the .tt files?
EDIT: Also, make sure you have all the proper references to EF. There might be a compiler warning indicating a problem.

Get DbContext for Entities

Okay, I feel a bit foolish for having to ask this but I guess my understanding of the inner workings of Entity Framework is lacking.
I'd like to experiment with work with DbContext. I have an existing ASP.NET MVC application using EF 4.2. I can get my entities using:
var context = new MyEntities();
And this works just fine.
But how the heck to I get the same data represented by a DbContext?
So I guess you are using default code generator provided by EDMX designer - it will use ObjectContext and heavy weight EntityObject based entities.
If you want to use DbContext you must:
Turn off that default code generation - in property window remove Custom Tool for EDMX file
Download and install DbContext T4 generator (you can get it directly from extension manager in Visual Studio)
In EF designer select Add Code Generation Item from context menu in the designer surface (not on entity)
Now EF will add two .tt files to your project - one will be responsible for creating a new class for every entity or complex type defined in your EDMX file and the second will be responsible for creating class derived from DbContext and exposing sets for all your entity types

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

Entity Framework Recreate POCO class

This may seem like a silly question, but I thought I'd ask it anyway :)
When you use Entity Framework's Database First approach you can create an Entity Data Model to describe the structure of your business objects.
You can also use the ADO.Net DbContext Generator to create persistance ignorance POCO classes. However, when you make a change to the Data Model, ie add a new property to an existing Entity, in order for the corresponding POCO class to also reflect this change, do you have to:
Manually add the new property to the POCO class
Recreate all the POCOs again using the DbContext Generator
I guess what I am asking is there anyway the POCO can be automatically updated if a change is made to the Model?
Thanks everyone.
If you're using the T4 Templates that look at the edmx file, you can just regenerate the pocos by running the templates: Click the Transform Templates icon in the Solution Explorer?
)or have I missed something)

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