日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

第十四节: EF的三种模式(四) 之 原生正宗的 CodeFirst模式的默认约定

發布時間:2023/12/10 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第十四节: EF的三种模式(四) 之 原生正宗的 CodeFirst模式的默认约定 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一. 簡介

?1. 正宗的CodeFirst模式是不含有edmx模型,需要手動創建實體、創建EF上下文,然后生成通過代碼來自動映射生成數據庫。

?2. 旨在:忘記SQL、忘記數據庫。

?3. 三類配置:One To One(one-to-zero-or-one)、One To Many、Many To Many。

注意:在該模塊使用最簡單的模式配置這三種關系,暫時先不考慮DataAnnotation和Fluent API

?A. One To One(one-to-zero-or-one)

?  ①:一個學生對應一個或零個地址,一個地址只能對應一個學生

?  ②:實現方式:

    a:Student表中添加StudentAddress屬性,StudentAddress表中添加Student屬性,然后給StudentAddress中的主鍵加上[ForeignKey("stu")] ,

      ?特別注意stu代表Student屬性的名字。

    ??b:編寫配置文件

發現的現象

  ?a.已經生成了一次數據庫,修改表結構,會報錯:數據添加失敗支持“dbContext1”上下文的模型已在數據庫創建后發生更改。

    請考慮使用 Code First 遷移更新數據庫(http://go.microsoft.com/fwlink/?LinkId=238269)。

  ?b. 類名+Id結尾屬性自動生成主鍵。

  ?c.生成的數據庫默認保存在數據庫的安裝目錄下。

  ?d.在數據庫中:StudentAddress中的studentAddressId既是主鍵,又是外鍵

?B. One To Many

?①:一個年級有多個學生,一個學生只能在一個年級

?②:實現方式:

?  a:Student2表中添加Grade1屬性,Grade1表中添加ICollection<Student2>屬性

?  b:編寫配置文件

?發現的現象:

  ?a. 數據庫中:Student2表中,增加了一個新的字段grade1_grade1Id作為外鍵

?C. Many To Many

?①:一個學生有多門課,一門課有多個學生上

?②:實現方式:

?  a:Student3表中添加ICollection<Course1>屬性,Course1表中添加ICollection<Student3>屬性

?  b:編寫配置文件

發現的現象

?a. 數據庫中多了一張表:Student3Course1,里面有兩個外鍵

二. 代碼實戰

1.?One To One(one-to-zero-or-one)

1 /// <summary>2 /// 學生表(一個學生只能有一個地址或沒有地址)3 /// </summary>4 public class Student5 {6 public Student()7 {8 9 } 10 public string studentId { get; set; } 11 12 public string studentName { get; set; } 13 14 public virtual StudentAddress StudentAddress { get; set; } 15 } 16 /// <summary> 17 /// 學生地址表(一個地址只能對應一個學生) 18 /// </summary> 19 public class StudentAddress 20 { 21 public StudentAddress() 22 { 23 24 } 25 26 [ForeignKey("stu")] 27 //特別注意這個地方,stu對應下面的 Student stu; 28 //另外特別注意:studentAddressId,符合默認的Id生成規則,自動映射成主鍵,否則需要加【key】特性 29 public string studentAddressId { get; set; } 30 31 public string addressName { get; set; } 32 33 public virtual Student stu { get; set; } 34 }

1 public class dbContext1:DbContext2 {3 public dbContext1()4 : base("name=dbContext1")5 {6 7 }8 public DbSet<Student> Student { get; set; }9 10 public DbSet<StudentAddress> StudentAddress { get; set; } 11 12 13 protected override void OnModelCreating(DbModelBuilder modelBuilder) 14 { 15 base.OnModelCreating(modelBuilder); 16 } 17 }

<!--正宗的CodeFirst OneToOne--> <add name="dbContext1" connectionString="data source=localhost;initial catalog=CodeFirstDB1;persist security info=True;user id=sa;password=123456;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />

2.?One To Many

/// <summary>/// 年級表(一個年級有多個學生)/// </summary>public class Grade1{public string grade1Id { get; set; }public string gradeName { get; set; }public virtual ICollection<Student2> student2 { get; set; }}/// <summary>/// 學生表(一個學生只能在一個年級)/// </summary>public class Student2{public string student2Id { get; set; }public string studentName { get; set; }public virtual Grade1 grade1 { get; set; }}

public class dbContext2 : DbContext{public dbContext2(): base("name=dbContext2"){}public DbSet<Student2> Student2 { get; set; }public DbSet<Grade1> Grade1 { get; set; }protected override void OnModelCreating(DbModelBuilder modelBuilder){base.OnModelCreating(modelBuilder);}}

<!--正宗的CodeFirst OneToMany--><add name="dbContext2" connectionString="data source=localhost;initial catalog=CodeFirstDB2;persist security info=True;user id=sa;password=123456;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />

3.?Many To Many

/// <summary>/// 學生表(一個學生有多門課)/// </summary>public class Student3{public string student3Id { get; set; }public string studentName { get; set; }public virtual ICollection<Course1> course { get; set; }}/// <summary>/// 課程表(一門課會有多個學生上)/// </summary>public class Course1{public string course1Id { get; set; }public string courseName { get; set; }public virtual ICollection<Student3> student { get; set; }}

1 public class dbContext3 : DbContext2 {3 public dbContext3()4 : base("name=dbContext3")5 {6 7 }8 public DbSet<Student3> Student3 { get; set; }9 10 public DbSet<Course1> Course1 { get; set; } 11 12 13 protected override void OnModelCreating(DbModelBuilder modelBuilder) 14 { 15 base.OnModelCreating(modelBuilder); 16 } 17 }

<!--正宗的CodeFirst OneToMany--><add name="dbContext3" connectionString="data source=localhost;initial catalog=CodeFirstDB3;persist security info=True;user id=sa;password=123456;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />

?

三. 總結

  上面的三種數據庫表的對應關系主要都是采用的EF的默認協定,比如:

    A: 類名+ID、類名+Id、類名+id自動生成主鍵(類名忽略大小寫),

    B: string類型自動映射成nvarchar(max)

? ? ? 但在實際開發中,默認協定顯然不靈活,那么我們如何自定義配置呢(自定義主鍵、設置非空、設置類型、長度等),EF提供兩種方式:DataAnnotations和Fluent API,但大多數情況都是兩者配合使用,關于二者的詳細使用,請見后面章節。

?

總結

以上是生活随笔為你收集整理的第十四节: EF的三种模式(四) 之 原生正宗的 CodeFirst模式的默认约定的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。