EF+Code First 數據關系隱射及遷移筆記一
阿新 • • 發佈:2019-01-18
country get clas 姓名 type book 驗證 table 找到
1.一對一關系(one to one)
1.1DataAnnotations方式
實體:書本實體,版本信息實體,一本書只能有一個版本號,版本號在沒有書出版的情況下是無意義的
public class Book { /// <summary> /// Id,主鍵 /// </summary>
[Key] public int BookID { get; set; } /// <summary> /// 書籍名稱 /// </summary> public string Name { get; set; } /// <summary> /// 書籍類型 /// </summary> public string Category { get; set; } /// <summary> /// 出版數量 /// </summary> public int Numberofcopies { get; set; } /// <summary> /// 作者Id /// </summary> public int AuthorID { get; set; } /// <summary> /// 書籍價格 /// </summary> public decimal Price { get; set; } /// <summary> /// 出版日期 /// </summary> public DateTime PublishDate { get; set; } /// <summary> /// 評級 /// </summary> public string Rating { get; set; } /// <summary> /// 版本號Id /// </summary> public int VersionId { get; set; } /// <summary> /// 一對一版本號信息 /// </summary> public PublishInfo publish { get; set; } /// <summary> /// 作者信息 /// </summary> public Author author { get; set; } } public class PublishInfo { /// <summary> /// 版本Id /// </summary>[Key]
[ForeignKey("book")] 設置此字段為Book的外鍵 public int VersionId { get; set; } /// <summary> /// 版本號 /// </summary> public string VersionNum { get; set; } /// <summary> /// 出版社名稱 /// </summary> public string PressName { get; set; } /// <summary> /// 關聯的書本信息 /// </summary> public virtual Book book { get; set; } }
說明:
[ForeignKey("book")] 為設置外鍵,設置了版本信息的主鍵為書本實體的外鍵,在使用DataAnnotations方式的時候,記得要引用“System.ComponentModel.DataAnnotations”和“System.ComponentModel.DataAnnotations.Schema”命名空間
1.2Fluent API方式
在以上實體的基礎上添加兩個映射類代碼如下:
public PublishInfoMap() { this.HasKey(p => p.VersionId); this.Property(p => p.VersionId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); this.Property(p => p.VersionNum).HasColumnType("nvarchar").HasMaxLength(50); this.Property(p => p.PressName).HasColumnType("nvarchar").HasMaxLength(100); this.ToTable(" PublishInfo"); this.HasRequired(p => p.book).WithRequiredDependent(p => p.publish); } public BookMap() { this.HasKey(b => b.BookID).Property(b => b.BookID).HasColumnName("B_Id"); this.Property(b => b.PublishDate).HasColumnType("datetime").IsRequired(); this.Property(b => b.Price).HasColumnType("decimal").IsRequired(); this.Property(b => b.Name).HasColumnType("nvarchar").HasMaxLength(200).IsRequired(); this.Property(b => b.Rating).HasColumnType("nvarchar").HasMaxLength(5).IsRequired(); this.Property(b => b.Category).HasColumnType("nvarchar").HasMaxLength(50).IsRequired(); this.HasRequired(b => b.author).WithMany(b => b.books).HasForeignKey(b => b.AuthorID); this.ToTable("Book"); }
說明:
this.HasRequired(p => p.book).WithRequiredDependent(p => p.publish);
此段代碼設置了兩張表的一對一映射關系
2.一對多關系(one to More)
實體:一本書只能有一個作者,一個作者可以有多本數
public class Book { /// <summary> /// Id,主鍵 /// </summary> public int BookID { get; set; } /// <summary> /// 書籍名稱 /// </summary> public string Name { get; set; } /// <summary> /// 書籍類型 /// </summary> public string Category { get; set; } /// <summary> /// 出版數量 /// </summary> public int Numberofcopies { get; set; } /// <summary> /// 作者Id /// </summary> public int AuthorID { get; set; } /// <summary> /// 書籍價格 /// </summary> public decimal Price { get; set; } /// <summary> /// 出版日期 /// </summary> public DateTime PublishDate { get; set; } /// <summary> /// 評級 /// </summary> public string Rating { get; set; } /// <summary> /// 版本號Id /// </summary> public int VersionId { get; set; } /// <summary> /// 一對一版本號信息 /// </summary> public PublishInfo publish { get; set; } /// <summary> /// 作者信息 /// </summary> public Author author { get; set; } } public class Author { /// <summary> /// 主鍵Id /// </summary> public int AuthorID { get; set; } /// <summary> /// 姓名 /// </summary> public string Name { get; set; } /// <summary> /// 性別 /// </summary> public int Sex { get; set; } /// <summary> /// 國籍 /// </summary> public string Country { get; set; } public virtual ICollection<Book> books { get; set; } }
2.1DataAnnotations方式
[ForeignKey("AuthorID")]
public Author author { get; set; }
在book實體上設置以上代碼
2.2Fluent Api方式
public BookMap() { this.HasKey(b => b.BookID).Property(b => b.BookID).HasColumnName("B_Id"); this.Property(b => b.PublishDate).HasColumnType("datetime").IsRequired(); this.Property(b => b.Price).HasColumnType("decimal").IsRequired(); this.Property(b => b.Name).HasColumnType("nvarchar").HasMaxLength(200).IsRequired(); this.Property(b => b.Rating).HasColumnType("nvarchar").HasMaxLength(5).IsRequired(); this.Property(b => b.Category).HasColumnType("nvarchar").HasMaxLength(50).IsRequired(); this.HasRequired(b => b.author).WithMany(b => b.books).HasForeignKey(b => b.AuthorID); this.ToTable("Book"); } public AuthorMap() { this.ToTable("Author"); this.HasKey(a => a.AuthorID); this.Property(a => a.Name).HasColumnType("nvarchar").HasMaxLength(50); this.Property(a => a.Country).HasColumnType("nvarchar").HasMaxLength(30); this.HasMany(a => a.books).WithRequired(a => a.author).HasForeignKey(a => a.AuthorID); }
設置:
BookMap類和
AuthorMap類
this.HasRequired(b => b.author).WithMany(b => b.books).HasForeignKey(b => b.AuthorID);
this.HasMany(a => a.books).WithRequired(a => a.author).HasForeignKey(a => a.AuthorID);
數據遷移
啟動遷移:Enable-Migrations;
添加遷移文件:Add-Migration Info(文件的名稱)
遷移到數據庫:Update-Database
說明:“在與 SQL Server 建立連接時出現與網絡相關的或特定於實例的錯誤。未找到或無法訪問服務器。
請驗證實例名稱是否正確並且 SQL Server 已配置為允許遠程連接。 (provider: SQL Network Interfaces, error: 26 - 定位指定的服務器/實例時出錯)”
若出現這個錯誤,需要指定要遷移的項目名稱:Update-Database -StartUpProjectName "ContextConfig"(你要遷移的項目名稱)
EF+Code First 數據關系隱射及遷移筆記一