码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2016-01-13 17:44:13      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

在上一篇中,讲述了用数据特性的方式来标识实体与数据表之间的映射关系,在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; }
}

技术分享

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

标签:

原文地址:http://www.cnblogs.com/weihengblogs/p/5127609.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!