代码改变世界

EF框架step by step(9)—Code First Fluent API

2011-05-28 08:34  杨延成  阅读(12346)  评论(13编辑  收藏  举报

在上一篇中,讲述了用数据特性的方式来标识实体与数据表之间的映射关系,在Code First方法中,还可以通过Fluent API的方式来处理实体与数据表之间的映射关系。

要使用Fluent API必须在构造自定义的DbContext时,重写OnModelCreating方法,在此方法体内调用Fluent API。

如下面代码所示:

publicclass BlogDbContext : DbContext
{
public BlogDbContext()
:
base("name=BlogDB2005")
{

}
protectedoverridevoid OnModelCreating(DbModelBuilder modelBuilder)
{

//do something
base.OnModelCreating(modelBuilder);
}
}

 

下面来看一些简单的例子

主键

modelBuilder.Entity<BlogUser>().HasKey(user => user.UserId);

联合主键

//联合主键
modelBuilder.Entity<BlogUser>().HasKey(user =>new { user.UserId, user.BlogName });

字段非空

//要求属性必填
modelBuilder.Entity<BlogUser>().Property(user => user.BlogName).IsRequired();

设定字段最大长度

modelBuilder.Entity<BlogUser>().Property(user => user.BlogName).HasMaxLength(20);

设置复杂属性,相当数据特性中的ComplexType

modelBuilder.ComplexType<Address>();

属性字段不映射到数据表字段,相当于数据特性中的NotMapped

modelBuilder.Entity<BlogUser>().Ignore(user => user.MyProperty);

设置字段是否自动增长  

//设置自动增长,如不需要自动增长,则设为 DatabaseGeneratedOption.None
modelBuilder.Entity<BlogUser>().Property(user => user.UserId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

设置外键

///产生一对多的关系
modelBuilder.Entity<Post>()
.HasRequired(p
=>p.BlogUser)
.WithMany(user
=> user.Posts)
.HasForeignKey(p
=> p.UserId);

///与上面等效
//modelBuilder.Entity<BlogUser>()
// .HasMany(user => user.Posts)
// .WithRequired(p => p.BlogUser)
// .HasForeignKey(p => p.UserId);

 

设定实体映射到数据库中的表名

modelBuilder.Entity<BlogUser>().ToTable("MyUser");

设置实体属性映射到数据库中的列名

modelBuilder.Entity<BlogUser>()
.Property(user
=> user.Description)
.HasColumnName(
"userDescription")
.HasColumnType(
"ntext");

下面给出完整代码,方便测试:

完整代码
class FluentAPISample
{

staticvoid Main(string[] args)
{
using (var db =new BlogDbContext())
{
db.Database.Create();
}
}
}

publicclass BlogDbContext : DbContext
{
public BlogDbContext()
:
base("name=BlogDB2005")
{
Database.SetInitializer
<BlogDbContext>(
new DropCreateDatabaseIfModelChanges<BlogDbContext>()
);
}
protectedoverridevoid OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity
<BlogUser>().HasKey(user => user.UserId);
//联合主键
//modelBuilder.Entity<BlogUser>().HasKey(user => new { user.UserId, user.BlogName });

//设置自动增长,如不需要自动增长,则设为 DatabaseGeneratedOption.None
modelBuilder.Entity<BlogUser>().Property(user => user.UserId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

//要求属性必填
modelBuilder.Entity<BlogUser>().Property(user => user.BlogName).IsRequired();
modelBuilder.Entity
<BlogUser>().Property(user => user.BlogName).HasMaxLength(20);
modelBuilder.Entity
<BlogUser>().Ignore(user => user.MyProperty);

modelBuilder.ComplexType
<Address>();

///产生一对多的关系
modelBuilder.Entity<Post>()
.HasRequired(p
=>p.BlogUser)
.WithMany(user
=> user.Posts)
.HasForeignKey(p
=> p.UserId);

///与上面等效
//modelBuilder.Entity<BlogUser>()
// .HasMany(user => user.Posts)
// .WithRequired(p => p.BlogUser)
// .HasForeignKey(p => p.UserId);

modelBuilder.Entity
<BlogUser>().ToTable("MyUser");

modelBuilder.Entity
<BlogUser>()
.Property(user
=> user.Description)
.HasColumnName(
"userDescription")
.HasColumnType(
"ntext");

base.OnModelCreating(modelBuilder);
}
}

publicpartialclass BlogUser
{
publicint UserId { get; set; }
publicstring BlogName { get; set; }
publicint MyProperty { get; set; }
public Address Address { get; set; }
publicstring Description { get; set; }
publicvirtual ICollection<Post> Posts { get; set; }
}
publicpartialclass Post
{
publicint PostId { get; set; }
publicstring PostTitle { get; set; }
publicint UserId { get; set; }
public BlogUser BlogUser { get; set; }
}
publicpartialclass Address
{
publicstring Province { get; set; }
publicstring City { get; set; }
}

 运行结果如下图示: