Fluent API :

http://social.msdn.microsoft.com/Search/zh-CN?query=Fluent%20API&Refinement=95&ac=4

http://msdn.microsoft.com/zh-cn/data/hh134698.aspx
http://msdn.microsoft.com/zh-cn/data/jj591617.aspx
http://msdn.microsoft.com/zh-cn/data/jj591620.aspx

CodeFirst:

http://www.codeproject.com/Articles/504720/EntityplusFrameworkplusCodeplusFirstplusMigrations

Understanding Database Initializers in Entity Framework Code First:

http://www.codeguru.com/csharp/article.php/c19999/Understanding-Database-Initializers-in-Entity-Framework-Code-First.htm

http://www.codeguru.com/csharp/article.php/c19233/Introduction-to-Entity-Framework-Code-First.htm

 
 
EF的codeFirst有两种方式:
第一种:.EF自动创建数据库。
第二种:EF使用已经存在的数据库,EF的codeFirst使用已经存在的数据库,需要在配置文件中添加:
<configuration>
  <connectionStrings>
    <add
      name="MyDB"
      providerName="System.Data.SqlClient"
      connectionString="Server=.\SQLEXPRESS;Database=TestDb;Trusted_Connection=true;"/>
  </connectionStrings>
</configuration>

public partial class BlogContext : DbContext
{
  public BlogContext():base("MyDB")
  {
  }
 
  。。。。
 

codeFirst的实现机制是先去配置文件中找,是否连接字符串,有则用已经存在的数据库,无则自动创建数据库

【1】如果BlogContext():base(“”)构造函数没有传参,先去配置文件中找是否连接字符串与"命名空间.BlogContext"同名的连接字符串。如果有,用这个数据库连接,若无,创建新的名为--”命名空间.BlogContext“的数据库

【2】如果BlogContext():base(“MyDB”)构造函数传参“MyDB”,先去配置文件中找是否连接字符串与"MyDB"同名的连接字符串。如果有,用这个数据库连接,若无,创建新的名为--”MyDB“的数据库

若需要在修改一些配置:

(什么时候要添加额外的配置?

答:比如:

1.CodeFirst自动生成的表名、表字段名不满足你的命名习惯

2.用已经存在的数据库表名称,表的列名等和实体类的字段名不一样,EF就无法建立表和实体类字段的映射关系,这时需要添加配置

添加配置的方法有两种:

第一种是使用特性:例如:[key]。但是使用特性这种方法有时无法满足所有的需求,

这时就要用第二种方法FluentAPI,

还有就是想我这种有代码点洁癖的,不喜欢在POCO类添加这些特性,若以后换了ORM框架,

EF的这些特性,跟其他ORM特性就没关系了,不删代码(实体类上的EF特性),要编译通过就得引用EF程序集。而EF程序集又不是现在需要的。所以

尽量保持POCO类的洁净是件好事

第二种是使用FluentAPI

重写BlogContext()的方法:

protected override void OnModelCreating(DbModelBuilder modelBuilder)

例如:

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder
.Configurations
.Add(new UserTypeConfiguration())
.Add(new ProductTypeConfiguration())
.Add(new CategoryTypeConfiguration())
.Add(new CategorizationTypeConfiguration())
.Add(new SalesLineTypeConfiguration())
.Add(new SalesOrderTypeConfiguration())
.Add(new ShoppingCartItemTypeConfiguration())
.Add(new ShoppingCartTypeConfiguration())
.Add(new RoleTypeConfiguration())
.Add(new UserRoleTypeConfiguration());
base.OnModelCreating(modelBuilder);
}
    public class UserTypeConfiguration : EntityTypeConfiguration<User>
{
#region Ctor
/// <summary>
/// Initializes a new instance of <c>CustomerTypeConfiguration</c> class.
/// </summary>
public UserTypeConfiguration()
{
HasKey(c => c.ID);
Property(c => c.ID)
.IsRequired()
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(c => c.UserName)
.IsRequired()
.HasMaxLength();
Property(c => c.Password)
.IsRequired()
.HasMaxLength();
Property(c => c.Email)
.IsRequired()
.HasMaxLength(); ToTable("Users");
}
#endregion
}

使用Fluent API进行了额外的配置。

【工欲善其事必先利其器】—Entity Framework实例详解

http://www.cnblogs.com/nianming/archive/2012/11/04/2753183.html

DDD & EntiyFramework

Shrink EF Models with DDD Bounded Contexts:
http://www.nmtree.net/2014/05/09/shrink-ef-models-with-ddd-bounded-contexts.html

【Entity Framework】 Entity Framework资料汇总的更多相关文章

  1. Types of Entity in Entity Framework:

    http://www.entityframeworktutorial.net/Types-of-Entities.aspx We created EDM for existing database i ...

  2. .NET Core Entity使用Entity Framework Core链接数据库

    首先安装Nuget包 Install-package Microsoft.EntityFrameworkCore Install-package Microsoft.EntityFrameworkCo ...

  3. Entity Framework Tutorial Basics(8):Types of Entity in Entity Framework

    Types of Entity in Entity Framework: We created EDM for existing database in the previous section. A ...

  4. EntityFramework 学习 一 Colored Entity in Entity Framework 5.0

    You can change the color of an entity in the designer so that it would be easy to see related groups ...

  5. 【Robot Framework 】项目实战汇总

    写在前面 RF自动化的文章记录基本完成,建一个汇总目录,方便查看. [Robot Framework 项目实战]汇总 ∮[RF 项目实战 00]环境搭建 ∮[RF 项目实战 01]使用 Request ...

  6. d3可视化实战00:d3的使用心得和学习资料汇总

    最近以来,我使用d3进行我的可视化工具的开发已经3个月了,同时也兼用其他一些图表类库,自我感觉稍微有点心得.之前我也写过相关文章,我涉及的数据可视化的实现技术和工具,但是那篇文章对于项目开发而言太浅了 ...

  7. NUnit单元测试资料汇总

    NUnit单元测试资料汇总 从安装到配置 首先到官网http://www.nunit.org/下载如下图的资料,安装NUnit-2.6.1.msi包. 然后挂在VS2010外部工具这个地方来使用,工具 ...

  8. iOS超全开源框架、项目和学习资料汇总--数据库、缓存处理、图像浏览、摄像照相视频音频篇

    iOS超全开源框架.项目和学习资料汇总--数据库.缓存处理.图像浏览.摄像照相视频音频篇 感谢:Ming_en_long 的分享 大神超赞的集合,http://www.jianshu.com/p/f3 ...

  9. Java进阶资料汇总

    Java经过将近20年的发展壮大,框架体系已经丰满俱全:从前端到后台到数据库,从智能终端到大数据都能看到Java的身影,个人感觉做后台进要求越来越高,越来越难. 为什么现在Java程序员越来越难做,一 ...

  10. 机器学习(Machine Learning)与深度学习(Deep Learning)资料汇总

    <Brief History of Machine Learning> 介绍:这是一篇介绍机器学习历史的文章,介绍很全面,从感知机.神经网络.决策树.SVM.Adaboost到随机森林.D ...

随机推荐

  1. 数据库设计(字段)中的char、varchar、text和nchar、nvarchar、ntext的区别

    char.varchar.text和nchar.nvarchar.ntext的区别 1.CHAR.CHAR存储定长数据很方便,CHAR字段上的索引效率级高,比如定义char(10),那么不论你存储的数 ...

  2. ASP.NET MVC强制返回XML

    GlobalConfiguration.Configuration.Formatters.Remove(config.Formatters.JsonFormatter);

  3. ARC小知识

    ARC是iOS 5推出的新功能,全称叫 ARC(Automatic Reference Counting).简单地说,就是代码中自动加入了retain/release,原先需要手动添加的用来处理内存管 ...

  4. Windows phone(1)-ApplicationBar(应用栏)

    在手机APP应用当中,我们往往会想某些操作能够进行快速访问或者能够持续显示用户信息的情况,比如像做泡泡堂游戏右边区域玩家信息 在wp7,wp8中为我们提供了ApplicationBar这样的控件来创建 ...

  5. linux 编译内核[scripts/kconfig/dochecklxdialog] 错误

    administrator@ubuntu:~/linux-2.6.28-omap$ make menuconfig *** Unable to find the ncurses libraries o ...

  6. 更改windows服务的配置文件app.config

    /// <summary> /// 获取每次处理记录数 /// </summary> /// <returns></returns> private s ...

  7. PHP获取上个月、下个月、本月的日期(strtotime(),date())

    今天写程序的时候,突然发现了很早以前写的获取月份天数的函数,经典的switch版,但是获得上月天数的时候,我只是把月份-1了,估计当时太困了吧,再看到有种毛骨悚然的感觉,本来是想再处理一下的,但是一想 ...

  8. http 错误编号大全(转)

      状态行包含HTTP版本.状态代码.与状态代码对应的简短说明信息.在大多数情况下,除了Content-Type之外的所有应答头都是可选的.但Content-Type是必需的,它描述的是后面文档的MI ...

  9. Waring:This LinearLayout layout or its FrameLayout parent is useless; transfer the background attribute to the other view

    解决方法请参考: You have a single component (row) vertical linear layout, containing another linear layout. ...

  10. Python标准库之urllib,urllib2自定义Opener

    urllib2.urlopen()函数不支持验证.cookie或者其它HTTP高级功能.要支持这些功能,必须使用build_opener()函数创建自定义Opener对象. 1. build_open ...