第二节:创建模型,使用Code First,配置映射关系
这一节,实现模型的创建,配置映射关系 使用Code First数据迁移。
创建模型
一,首先创建几个接口:实体接口,聚合根接口,值对象接口1,实体接口:
![](https://images2018.cnblogs.com/blog/1297333/201806/1297333-20180630190243508-1465071293.png)2,聚合根接口:
![](https://images2018.cnblogs.com/blog/1297333/201806/1297333-20180630190447629-1515198679.png)3,值对象接口:
![](https://images2018.cnblogs.com/blog/1297333/201806/1297333-20180630190524322-569928818.png)二,模型
这里我们的业务场景是:一个用户可以创建一个博客,在博客中可以写多篇文章,一篇文章只能有一个评论,
(练习配置映射关系)由于格式的原因我移除了注释信息
用户:
public class UserInfo: IAggregationRoot
{
#region 用户实体
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]//添加时自动增长
public int Id { get; set; }
public string UserName { get;private set; }
public string UserPwd { get; private set; }
public Address UserAddr { get; private set; }
public string Phone { get; private set; }
public string CreateTime { get; private set; }
public List UserRoleInfo { get; set; }
#endregion
public UserInfo(string userName,string userPwd, Address userAddr,string phone)
{
if (!string.IsNullOrEmpty(userName))
{
throw new ArgumentException("客户姓名不能为空!");
}
this.UserName = userName;
this.UserAddr = userAddr;
this.UserPwd = userPwd;
this.Phone = phone;
this.CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
public UserInfo CreateUser(string userName, string userPwd, Address userAddr, string phone)
{
return new UserInfo(userName,userPwd,userAddr,phone);
}
}
博客:
public class BlogInfo:IAggregationRoot
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string BolgName { get;private set; }
public string BolgUrl { get; set; }
public string BolgCrateDate { get;private set; }
public string BolgStatus { get;private set; }
public int UserId { get;private set; }
public virtual List BlogPostList { get; set; }
}
文章:
public class BlogPostInfo
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string ArticleTitleName { get;private set; }
public string ArticlePostCount { get;private set; }
public string ArticleStatus { get;private set; }
public int ReadNum { get;private set; }
public string ArticleCrateDate { get;private set; }
public int BlogId { get;private set; }
//[ForeignKey("BlogId")]
public virtual BlogInfo BolgInfo { get; set; }
public virtual ArticleReviewsInfo ArticleReviewsInfo { get; set; }
}
文章评论:
public class ArticleReviewsInfo : IAggregationRoot
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
//[ForeignKey("BlogPostInfo")]
public int Id { get; set;}
public string ArticleReviewCount { get;private set; }
public string ArticleReviewTime { get;private set; }
public string ArticleCommentatorId { get;private set; }
public string ArticleCommentatorName { get;private set; }
public BlogPostInfo BlogPostInfo { get; set; }
}
三,使用 Code First,配置映射关系
1,引入Nuget EntityFramework 2,定义MyContext类继承DbContext 3,读取数据库 Web.config中 connectionStrings的配置,数据库连接字符串 4,添加 public DbSet UserInfo Name { get; set; } UserInfo要添加的实体,Name 取的名称 5,配置映射关系 ,有2种方式可以配置,Data Annotation和Fluent API 6, Code First 数据迁移 主要使用的数据迁移的命令:Enable-Migrations Add-Migration 迁移名称 Update-Database Add-Migration InitialCreate -IgnoreChanges 首先先谢谢 libingql,田园的蟋蟀的分享 写的非常的详细,非常的好 。 libingql:http://www.cnblogs.com/libingql/p/3352058.html 配置映射关系 田园的蟋蟀:http://www.cnblogs.com/xishuai/p/3632304.html Code First 数据迁移的所有使用步骤 代码
public class MyContext: DbContext
{
//读取数据库Web.config connectionStrings的配置
private static readonly string SqlConnectionString = WebConfigurationManager.ConnectionStrings["sqlConnectionString"].ToString();
public MyContext() : base(SqlConnectionString)
{
// 禁用延迟加载
//this.Configuration.LazyLoadingEnabled = false;
}
public DbSet UserInfo UserInfo { get; set; }
public DbSet RoleInfo RoleInfo { get; set; }
public DbSet UserRoleInfo UserRoleInfo { get; set; }
public DbSet BlogPostInfo BlogPost { get; set; }
public DbSet BlogInfo BlogInfo { get; set; }
public DbSet ArticleReviewsInfo ArticleReviewsInfo { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//值对象的配置
modelBuilder.ComplexType<Address>().Property(e => e.AddrProvince).HasColumnName("AddrProvince").HasMaxLength(100);
modelBuilder.ComplexType<Address>().Property(e => e.AddrCity).HasColumnName("AddrCity").HasMaxLength(100);
modelBuilder.ComplexType<Address>().Property(e => e.AddrCounty).HasColumnName("AddrCounty").HasMaxLength(100);
modelBuilder.ComplexType<Address>().Property(e => e.AddressDetails).HasColumnName("AddressDetails").HasMaxLength(1000);
// 禁用默认表名复数形式
//modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
//配置UserInfo
//modelBuilder.Configurations.Add(new UserMap());
//一对多的关系
modelBuilder.Entity<BlogInfo>().HasMany(e => e.BlogPostList).WithRequired(e => e.BolgInfo)
.HasForeignKey(e => e.BlogId);
modelBuilder.Entity<BlogPostInfo>().HasRequired(e => e.BolgInfo).WithMany(e => e.BlogPostList)
.HasForeignKey(e => e.BlogId);
//一对一的关系
modelBuilder.Entity<ArticleReviewsInfo>().HasRequired(t => t.BlogPostInfo)
.WithRequiredDependent(t => t.ArticleReviewsInfo);
//多对多的关系
modelBuilder.Entity<UserRoleInfo>().HasRequired(e => e.UserInfo).WithMany(e => e.UserRoleInfo)
.HasForeignKey(e => e.UserId);
modelBuilder.Entity<UserRoleInfo>().HasRequired(e => e.RoleInfo).WithMany(e => e.UserRoleInfo)
.HasForeignKey(e => e.RoleId);
}
}
由于格式原因这里应该是:
![](https://images2018.cnblogs.com/blog/1297333/201807/1297333-20180701142759381-870211744.png)
映射关系配置完成后,使用数据迁移请看: 田园的蟋蟀:http://www.cnblogs.com/xishuai/p/3632304.html Code First 数据迁移的所有使用步骤