原文地址:http://www.cnblogs.com/n-pei/archive/2010/08/12/1797751.html
我使用Entity Framework是在开始学习Silverlight的时候。用了半年左右。最近ADO.NET团队发布了EF Feathure CTP 4。主要是增加了Code First这个功能。
在说明Code First之前,先和大家回顾下
EF4增加的新特性
1.外键支持(Foreign Keys)
可以通过直接设置外键属性来设置实体之间的关系。
2.延迟加载支持
这个功能默认情况下是开启的,也就是说一个查询操作返回的实体只有在被用到时才从服务器加载。
3. POCOs支持
这样你的实体对象就可以独立于EF而存在。不懂POCOs?没关系,这里有几篇很好的文章帮助你理解:
4.T4代码生成的支持
T4不但简化了代码生成的个性化配置,而且让开发人员对代码有了更灵活和强大的控制。
5.更好的N-Tier设计支持
6.改进SQL语句的生成
为了提供EF生成的SQL语句的可读性和性能,在EF4中,查询生成SQL的部分做了很大的改进。
7.增强对存储过程的支持
这篇文章里我将会和大家探讨下Database First模式,Model First模式和Code First模式在EF4和VS2010中如何使用以及它们的优缺点。
Database First模式的介绍
这个是最早也是最简单的模式。当然新手可以看看下面的内容,如果是EF使用很久了请跳过这里。
首先在创建好的VS2010的工程中,右键添加新项,添加ADO.NET Entity Data Model:
点击下一步,选择从数据库生成:
点击下一步,你就可以选择你需要使用的数据库,我这里使用NorthWind:
选择好数据库后点击下一步,选择你要用到的表,试图或者是存储过程:然后点击完成,VS2010自动为你生成好了Data Model:
当然这个功能限制了开发人员对整个生成的Data Model的修改,于是出来了POCOs的使用。使用POCOs让开发人员对数据库的操作更加灵活。
Model First模式的介绍
之前我有一篇文章使用MVVM+MEF+Silverlight实现的Sellthrough文章里面介绍了使用Model First的模式。好像这个模式大家用的并不多,大部分人现在使用的是POCOs。如果建立一个小的数据模型,在设计实体关系模型时我觉得完全可以使用Model First来进行。
熟悉Model First的你完全可以跳过下面的例子继续看Code First部分了。
在Database First中我们创建的Model是
那么Model First中我们需要创建的就只是一个空Model。如上图:
在创建好一个空的Model后,在这个Model的设计模式下右键选择新增,新增一个实体:
我们给它命名为Products:
点击确定,为这个Entity添加属性:比如:name,Supplier,UnitPrice。
同样的方法我们为这个Data Model添加一个Customers实体,它的属性有Company,Name,Title。
创建好的Model如下:
这么简单的模型对于我们来说没有意义,所以接下来我们要给实体之间加上关系对应。
在这个Data Model的设计界面的空白处,右键选择添加,添加关联(Association):
Customer和product是n-n的关系,所以我们给他们每个人增加一个Navigation Properties:
都是Many-Many。创建好Model后,我们下一步就是用这个Model来生成数据库脚本了。
在空白处点击右键:
选择从Model生成数据库,VS2010会给你向导,在向导页面选择新连接,然后输入一个数据库名(目前不存在的)。系统会提示你,数据库不存在,是否要创建,选择是。
完成后你会发现你的项目中多了个sql脚本:
现在你需要做的只是打开SQL Server Management Studio并运行下上面这个SQl脚本。
Code First模式
这个模式是我要重点说明的。使用Code first这个模式后,你的项目中可以说就不再需要.edmx这种系统自动生成的Data Model了。
这部分我还不是很有把握,很多东西还没学。但是我会用两个例子来告诉大家如何使用ObjectContext和DBContext来直接创建数据库。
既然是Code First那么最重要的就是我们创建实体的部分了。创建好了实体,我们再去创建ObjectContext或者是DBContext,最终借助ObjectContext或者是DBContext来完成数据库的自动创建。
实体的创建:
很简单的几个实体。书籍,作者出版商。
接下来我们创建一个BookCatalog,它继承了ObjectContext类,这里需要注意的是在BookCatalog的构造函数中有个SqlConnection的参数。
我们就是要靠这个连接字符串来创建数据库所在的位置和名称。
除此之外就是要注意对象集ObjectSet的使用:
其它的出版商和作者是BookCatolog的ObjectSet。
完成这个类后,和上面的Model First遇到的问题相同,这么简单的东西不可能满足我们的业务需求的。所以引出来了EntityConfiguration类。如何使用呢?
首先我们创建一个BookConfiguration类,继承了EntityConfiguration,代码如下:
HasKey:设置主键,
IsRequired()不为空字段,
WithMany是一对多。
上面的这些属性就是为了在生成数据库时Book的表有主键,有不为空字段和外键关系。
我们来使用它创建个数据库试试。
我这里使用一个控制台应用程序来执行测试的。
在Main方法入口中,我们首先需要创建一个ModelBuilder,使用它我们才能使用Configuration类和创建一个Model,接着创建Object。
Ok,有个model后,就可以借助上面的BookCatalog来创建数据库,被就行数据操作:
从代码中可以看到model首先是创建了一个ObjectContext,然后这个context去判断给数据库是否存在(CodeFirstWalkthrough),不存在就创建这个数据库。
最后是在数据库中添加一条记录。
运行后看结果:
下面说明如何使用DbContext来创建数据库:
首先还是创建一个类继承了DbContext这个类:
它的属性类型是DbSet,在ObjectContext中式ObjectSet,很相似。
使用它来创建数据库:
这个比用ObjectContext创建数据库要简单多了,但是你会发现我们的SimpleBookCatalog的构造函数需要的参数只是个DbModel,那么运行它后创建的数据库呢???????默认的你的机器必须是安装了SQL express,数据库已经被创建好在那里。
那么数据库的名称呢?因该是Projectname.SimpleBookCatalog,和你的DbContext名字相同。
如果你想创建在其它地方可以吗?当然可以。很多方式,首先是构造函数中给出来sqlconnection,像上面的ObjectContext创建数据库。
第二个方式就是在App.config文件在connectionStrings节点处配置好你的数据库连接串。
你可以使用sql server ,sql express或者是SQL CE。链接字符串的名字必须是SimpleBookCatalog。
分析这三种方式的优缺点,
Database-First模式明显性能会差点,但是它很适合初学者,或者是比较急的小型项目。
Model-First模式优点是开发人员能够在设计模型时完全了解数据库的结构,但是缺点是在模型设计完后,还是需要去手动创建数据库,并且生成的脚本有点不简洁。
Code-First模式有点不用说了,就是上面两个模式的缺点。缺点应该也是有很多的,比如更新数据库。
大家可以说说自己的意见。
推荐EF学习资源:
Documentation
- Updated Entity Framework Documentation for Beta1: MSDN Documentation (far from complete)
- Announcing: Entity Framework Feature CTP 1: After VS2010 Beta 1 shipped, the EF team added 3 new features in a CTP1 just for Entity Framework. The documentation comes as walkthroughs
- Better N-Tier Support with Self Tracking Entities
- POCO (Plain Old CLR Objects) entity code generation via the POCO Template
- Writing only code and having it work with the Entity Framework via Code Only.
- NB: These will ship shortly after VS2010 RTM
Overview blog posts
Team Blogs
- Entity Framework Transparent Design Blog http://blogs.msdn.com/efdesign
- ADO.NET Team blog http://blogs.msdn.com/adonet
- Daniel Simmons http://blogs.msdn.com/dsimmons
- Diego Vega’s blog http://blogs.msdn.com/diego
- Alex’s blog http://blogs.msdn.com/alexj
Community Bloggers
- Julie Lerman, author of Programming Entity Framework http://thedatafarm.com/blog
- Check out EF4 posts http://thedatafarm.com/blog/tags/ef4/
- Roger Jennings, author of ADO.NET 3.5 with LINQ and the Entity Framework
- Entity Framework post aggregation
- Dan Rigsby http://www.danrigsby.com/blog
- Gunnar http://weblogs.asp.net/gunnarpeipman/archive/tags/Entity+Framework/default.aspx
- Tony http://blog.tonysneed.com/
Screencasts and Podcasts
- Julie Lerman Tips and Tricks – includes some EF4 insights http://www.oreillynet.com/pub/e/1412 (60mins)
- 10-4 Episode 15: Model-First Development with the Entity Framework 4.0
- .NET Rocks with Daniel Simmons May 22nd 2009.
Misc
- A series of Entity Framework Tips. Some are EF 4.0 specific
Improvements to the Tools/Designer
- Now used the T4 template engine for code generation
- You can configure the ADO.NET Entity Data Model Designer to use text templates to generate customized object layer code. For more information, see How to: Customize Object Layer Code Generation.
- Customizing T4 Templates
- Feature CTP Walkthrough: POCO Templates for Entity Framework
- Sneak Peek – Using Code Generation Templates with the Entity Framework 4.0
- Customizing EDM Code Gen in EF4
- Model-First Development is now possible
- The Create Database Wizard enables you to do conceptual modelling first, and then create a database that supports the model. For more information, see How to: Generate a Database from a Conceptual Model.
- Sneak Preview: Model First in the Entity Framework 4.0
- Model First (Sept 2008)
- Entity Framework 4.0: Generating SQL script from model
- Automatic Pluralization/Singularization is now added including customisation
- The Entity Data Model Wizard and the Update Model Wizard provide the option of using singular or plural forms of Entity, EntitySet, and NavigationProperty names to make application code more readable. For more information, see Choose Your Database Objects Dialog Box (Entity Data Model Wizard) and Choose Your Database Objects Dialog Box (Update Model Wizard).
- Sneak Preview - Pluralization in Entity Framework 4.0
- Entity Framework 4.0: Pluralization – deep look at customising
- The much improved EDM Wizard Pluralization in VS2010
- Pluralization (Dec 2008)
- The designer now surfaces Complex Type support
- The ADO.NET Entity Data Model Designer now supports complex types. For more information, see the following topics:
- Complex Types in the EDM Designer in EF4 and a look at updating complex types in code
- Stored procedure mapping is improved significantly
- Improved control over Entity deletion and search
- The Model Browser window of the ADO.NET Entity Data Model Designer enables you to delete objects from the storage model and to search the conceptual and storage models for a specified string. For more information, see Model Browser Window and How to: Delete Objects from the Storage Model.
- More Designer Improvements - Deleting Entities from the model and from the store schema
- Misc:
- EF4 – New Properties for Entity Properties?
- Making it easier in the designer to do EF Table Splitting – The Opposite of Entity Splitting
Persistence Ignorance
You can use your own custom data classes together with your data model without making any modifications to the data classes themselves. This means that you can use "plain old" CLR objects (POCO), such as existing domain objects, with your Entity Framework application. For more information, see Persistence Ignorant Objects (Entity Framework).
- No need to inherit from EntityObject
- No need to implement any EF interface
- No metadata attributes
- By-convention mapping between model and POCOs
- Any ICollection<T> for collections and object references for relationships
- Snapshot change detection
- POCO in Entity Framework : Part 1 – The Experience
- POCO in Entity Framework : Part 2 – Complex Types, Deferred Loading and Explicit Loading
- POCO in Entity Framework : Part 3 – Change Tracking with POCO
- Using Repository and Unit of Work patterns with Entity Framework 4.0
- Why not use POCO always?
- Sneak Preview: Persistence Ignorance and POCO in Entity Framework 4.0
And from the community:
- First look http://mosesofegypt.net/post/Entity-Framework-4-Persistence-Ignorance-First-Look.aspx
- Entity Framework 4.0: POCOs and table-per-type inheritance mapping
- Entity Framework 4.0: How to use POCOs
- Entity Framework 4.0 – How To POCO
Code-only
In NET 4.0 Beta 1 we introduced Model-First which allowed you to begin by creating an entity data model, then use it to create an empty database and classes. The subsequent CTP introduced Code Only.
- Begin by writing your classes, use them to create the database. There are no metadata artifacts at runtime
- Code Only introduction before CTP 1.
- Code Only Enhancements details what to expect in the release after CTP 1.
Deferred Loading of Related Objects (aka Lazy Loading)
With deferred loading, also known as lazy loading, related objects are automatically loaded from the data source when you access a navigation property. For more information, see Shaping Query Results (Entity Framework).
Foreign Key associations
Foreign Keys are now surfaced.
- An FK property can define an association and be part of the entity at the same time
- Concurrency is now shared with the entity
- FKs, EntityReferences, EntityCollections are all kept in sync
- Overlapping composite keys are supported
- Foreign Keys in the Entity Framework
- Foreign Keys in the Conceptual and Object Models (Oct 2008)
Query Improvements
- Enhanced support of LINQ query operators
- Contains
- DefaultIfEmpty
- Single, SingleOrDefault (in top projection)
- Function Mapping
- EntityFunctions and SqlFunctions
- The EntityFunctions and SqlFunctions classes provide access to canonical and database functions from LINQ to Entities queries. The EdmFunctionAttribute allows a CLR method to serve as a proxy for a function defined in the conceptual model or storage model. For more information, see Calling Functions in LINQ to Entities Queries.
- Model Defined Functions (Jan 2009)
- EF4: Model-Defined Functions Level 1 & 2
- EDM and Store functions exposed in LINQ (Oct 2008)
- ExecuteStoreQuery and Translate
- Not forgetting better support for using Stored Procedures (see Tools section above)
- Important Entity Framework Query Improvements for .NET 4.0
- Improvements to the Generated SQL in .NET 4.0 Beta1
N-Tier
In Beta 1 we added an improved API for reporting changes: ChangeObjectState, ChangeRelationshipState, ApplyOriginalValues. In the CTP we added Self-tracking entities: serialize changes alongside current state in the object graph
- Feature CTP Walkthrough: Self Tracking Entities for Entity Framework
- Sneak Preview: N-Tier development with Entity Framework 4.0
- Self-Tracking Entities in the Entity Framework
- Entity Framework: Anti-Patterns To Avoid In N-Tier Applications
Danny Simmons explores some anti-patterns you should look out for when building n-tier applications with the Entity Framework. - Entity Framework: N-Tier Application Patterns
This article examines n-tier patterns for success and some of the key APIs and issues specific to the Entity Framework. It also provides a sneak peak at features coming in the Microsoft .NET Framework 4 that should make n-tier development significantly easier. - N-Tier Improvements for Entity Framework (Nov 2008)
Improving Testability of the Entity Framework
Unit testing EF v1 is tricky (Check out Julies post on this). Things get a lot better with EF 4.0 thanks to a combination of features:
- POCO - classes that do not depend on the EF can be easier to test
- Data Access Guidance and the Repository pattern – work in progress http://dataguidance.codeplex.com/
- New IObjectSet<T> interface makes it easier to fake an object
- Template based code generation – control the code gen
- LINQ to Entities improvements make LINQ to Entities and LINQ to Objects closer – which can simplify testing
- Sneak Preview: Entity Framework 4.0 Testability Improvements
Cheers
Nic