這一節,實現模型的建立,配置映射關係 使用Code First數據遷移。
html
這裏咱們的業務場景是:一個用戶能夠建立一個博客,在博客中能夠寫多篇文章,一篇文章只能有一個評論,
(練習配置映射關係)因爲格式的緣由我移除了註釋信息
用戶:
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
#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
}
文章:
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; }
}
文章評論:
sql
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; }
}
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);
}
}
因爲格式緣由這裏應該是:
ide
映射關係配置完成後,使用數據遷移請看: 田園的蟋蟀:http://www.cnblogs.com/xishuai/p/3632304.html Code First 數據遷移的全部使用步驟post