Get started with EntityFramework 4
We have the following option to using EntityFramework4
Using designer
- Model First
We can design model first , then generate the sql for the model, use the sql to generate the database, and then connect the model to the database. The process of sql gereration is customizable. You change this workflow (process) and the template of the sql, these file are located at C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\DBGen.
ASO.NET team blog has an article about this a approach.
- Database First
This is a classic approach. You have your database in the first place, and generate model based on that.
Since you have use the designer, you have other customization options that are related to code generation from the model created by designer. But default designer use a Code generation strategy call "Default". If you model file name is "model.edmx", the strategy generate a cs file "model.cs" file, and the code generate the ObjectContext, and other entities, and so forth. The entities
[EdmEntityTypeAttribute(NamespaceName="CrmModel", Name="Post")] [Serializable()] [DataContractAttribute(IsReference=true)] public partial class Post : EntityObject { ... }
Since EF4 use t4 to generate code, you can customized the t4 to customized the code generation by right clicking the designer and adding code generation item. When you add code generation item, this will turn off the default code generation strategy. Your model.cs file will be empty.
There are a couple code generation item available now. When you add an item a t4 file (*.tt) is added to the project. You can customize the tt file as want.
- ADO.NET EntiyObject Generator
This is same as default template. And there is not much to talk about.
- ADO.NET Self-Tracking Entity Generator
- ADO.NET POCO Entity Generator
When this option is used, there will be two t4 file added, one for ObjectContext and one for Entities. The Entities template will generate a set of cs file each cs file contains a entity. And they are poco class, which means they does not inherit any base class. This are a couple of articles about this feature, Feature CTP Walkthrough: POCO Templates for the Entity Framework , Walkthrough: Test-Driven Development with the Entity Framework 4.0, Using Repository and Unit of Work patterns with Entity Framework 4.0, Sneak Preview: Entity Framework 4.0 Testability Improvements
- Model First
Use code only
Feature CTP Walkthrough: Code Only for the Entity Framework (Updated)