EFCodeFirst关系映射约定

EFCodeFirst
关系映射约定

默认多重关系的一些约定规则:

1.一对多关系

两个类中分别包含一个引用和一个集合属性。

两个类中一个类包含另一个类的引用属性。

两个类中一个类包含另一个类的集合属性。

2.多对多关系

两个类分别包含对方的一个集合属性。

3.一对一关系

两个类分别包含对方的一个引用属性。

1.外键列名默认约定

3种外键列名的约定方式是:

  • [Target Type Key Name],[目标类型的键名]
  • [Target Type Name] + [Target Type Key Name],[目标类型名称]+[目标类型键名称]
  • [Navigation Property Name] + [Target Type Key Name],[引用属性名称]+[目标类型键名称]

3种不同的外键名称命名之间存在优先级:

[目标类型的键名] > [引用属性名称]+[目标类型键名称] > [目标类型名称]+[目标类型键名称]。

1.[目标类型的键名]

  • 两个类中分别包含一个引用和一个集合属性的一对多种。
  • 启用级联删除功能。

2.[目标类型名称]+[目标类型键名称]

  • 两个类中一个类包含另一个类的集合属性。

3.[引用属性名称]+[目标类型键名称]

2.一对多关系

修改外键命名约定,自定义外键列名

Data Annotations方式

  1. public int CatID { get; set; } 

  2. [ForeignKey("CatID")] 

  3. public virtual Category Category { get; set; } 

  4. //或者 

  5. [ForeignKey("Category")] 

  6. public int CatID { get; set; } 

  7. public virtual Category Category { get; set; } 

Fluent API方式

  • 两个实体类之间的关系,可以两个类中均添加关系映射配置,也可以只对其中任意一个实体类添加关系映射配置。
  • 建议将实体类之间关系映射配置在包含外键的类中
  1. modelBuilder.Entity<Product>() 

  2. .HasRequired(t => t.Category) 

  3. .WithMany(t => t.Products) 

  4. .HasForeignKey(d => d.CatID); 

  • 一对多关系关系生成的外键引用约束默认是有级联删除的,可以通过以下方式禁用Category与Product之间的级联删除。
  1. protected override void OnModelCreating(DbModelBuilder modelBuilder) 

  2. { 

  3. modelBuilder.Entity<Product>() 

  4. .HasRequired(t => t.Category) 

  5. .WithMany(t => t.Products) 

  6. .HasForeignKey(d => d.CatID) 

  7. .WillCascadeOnDelete(false); 

  8. } 

  9. //也可以在Entity Framework Code First生成的全部表中都统一设置禁用一对多级联删除。 

  10. protected override void OnModelCreating(DbModelBuilder modelBuilder) 

  11. { 

  12. modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>(); 

  13. } 

  • 可以支持外键列名自定义的,但在实际的项目中,更多的外键列名称还是与所引用表的主键列名相同。

3.一对一关系

  • 两个表均有各自的主键,但要看哪个表的主键同时作为外键引用另一个表的主键。

Data Annotations方式

UserProfile表中的主键ProfileID同时也作为外键引用User表中的主键UserID。

User为主表

  1. public partial class User 

  2. { 

  3. [Key] 

  4. public int UserID { get; set; } 

  5. public string UserName { get; set; } 

  6. public string Password { get; set; } 

  7. public Nullable<bool> IsValid { get; set; } 


  8. public virtual UserProfile UserProfile { get; set; } 

  9. } 


  10. ------------------------------------------------------------------------------------------ 

  11. public partial class UserProfile 

  12. { 

  13. [Key] 

  14. [ForeignKey("User")] 

  15. public int ProfileID { get; set; } 

  16. public string Name { get; set; } 

  17. public Nullable<bool> Sex { get; set; } 

  18. public Nullable<DateTime> Birthday { get; set; } 

  19. public string Email { get; set; } 

  20. public string Telephone { get; set; } 

  21. public string Mobilephone { get; set; } 

  22. public string Address { get; set; } 

  23. public Nullable<DateTime> CreateDate { get; set; } 


  24. public virtual User User { get; set; } 

  25. } 

表User主键UserID将同时作为外键引用UserProfile表的主键ProfileID。

UserProfile为主表

  1. public partial class User 

  2. { 

  3. [Key] 

  4. [ForeignKey("UserProfile")] 

  5. public int UserID { get; set; } 

  6. public string UserName { get; set; } 

  7. public string Password { get; set; } 

  8. public Nullable<bool> IsValid { get; set; } 


  9. public virtual UserProfile UserProfile { get; set; } 

  10. } 


  11. ------------------------------------------------------------------------------------------ 

  12. public partial class UserProfile 

  13. { 

  14. [Key] 

  15. public int ProfileID { get; set; } 

  16. public string Name { get; set; } 

  17. public Nullable<bool> Sex { get; set; } 

  18. public Nullable<DateTime> Birthday { get; set; } 

  19. public string Email { get; set; } 

  20. public string Telephone { get; set; } 

  21. public string Mobilephone { get; set; } 

  22. public string Address { get; set; } 

  23. public Nullable<DateTime> CreateDate { get; set; } 


  24. public virtual User User { get; set; } 

  25. } 

Fluent API方式

UserProfile表中的主键ProfileID同时也作为外键引用User表中的主键UserID。

User为主表

  1. public partial class User 

  2. { 

  3. public int UserID { get; set; } 

  4. public string UserName { get; set; } 

  5. public string Password { get; set; } 

  6. public Nullable<bool> IsValid { get; set; } 


  7. public virtual UserProfile UserProfile { get; set; } 

  8. } 

  9. ------------------------------------------------------------------------------------------ 

  10. public class UserMap : EntityTypeConfiguration<User> 

  11. { 

  12. public UserMap() 

  13. { 

  14. // Primary Key 

  15. this.HasKey(t => t.UserID); 


  16. // Properties 

  17. this.Property(t => t.UserName) 

  18. .HasMaxLength(50); 


  19. this.Property(t => t.Password) 

  20. .HasMaxLength(100); 


  21. // Table & Column Mappings 

  22. this.ToTable("User"); 

  23. this.Property(t => t.UserID).HasColumnName("UserID"); 

  24. this.Property(t => t.UserName).HasColumnName("UserName"); 

  25. this.Property(t => t.Password).HasColumnName("Password"); 

  26. this.Property(t => t.IsValid).HasColumnName("IsValid"); 

  27. } 

  28. } 

  29. ------------------------------------------------------------------------------------------  

  30. public partial class UserProfile 

  31. { 

  32. public int UserID { get; set; } 

  33. public string Name { get; set; } 

  34. public Nullable<bool> Sex { get; set; } 

  35. public Nullable<DateTime> Birthday { get; set; } 

  36. public string Email { get; set; } 

  37. public string Telephone { get; set; } 

  38. public string Mobilephone { get; set; } 

  39. public string Address { get; set; } 

  40. public Nullable<DateTime> CreateDate { get; set; } 


  41. public virtual User User { get; set; } 

  42. } 

  43. ------------------------------------------------------------------------------------------  

  44. public class UserProfileMap : EntityTypeConfiguration<UserProfile> 

  45. { 

  46. public UserProfileMap() 

  47. { 

  48. // Primary Key 

  49. this.HasKey(t => t.UserID); 


  50. // Properties 

  51. this.Property(t => t.UserID) 

  52. .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); 


  53. this.Property(t => t.Name) 

  54. .IsRequired() 

  55. .HasMaxLength(50); 


  56. this.Property(t => t.Email) 

  57. .IsRequired() 

  58. .HasMaxLength(100); 


  59. this.Property(t => t.Telephone) 

  60. .HasMaxLength(50); 


  61. this.Property(t => t.Mobilephone) 

  62. .HasMaxLength(20); 


  63. this.Property(t => t.Address) 

  64. .HasMaxLength(200); 


  65. // Table & Column Mappings 

  66. this.ToTable("UserProfile"); 

  67. this.Property(t => t.UserID).HasColumnName("UserID"); 

  68. this.Property(t => t.Name).HasColumnName("Name"); 

  69. this.Property(t => t.Sex).HasColumnName("Sex"); 

  70. this.Property(t => t.Birthday).HasColumnName("Birthday"); 

  71. this.Property(t => t.Email).HasColumnName("Email"); 

  72. this.Property(t => t.Telephone).HasColumnName("Telephone"); 

  73. this.Property(t => t.Mobilephone).HasColumnName("Mobilephone"); 

  74. this.Property(t => t.Address).HasColumnName("Address"); 

  75. this.Property(t => t.CreateDate).HasColumnName("CreateDate"); 


  76. // Relationships 

  77. this.HasRequired(t => t.User) 

  78. .WithRequiredDependent(t => t.UserProfile); 

  79. } 

  80. } 

表User主键UserID将同时作为外键引用UserProfile表的主键ProfileID。

UserProfile为主表

  1. public class UserProfileMap : EntityTypeConfiguration<UserProfile> 

  2. { 

  3. public UserProfileMap() 

  4. { 

  5. // Primary Key 

  6. this.HasKey(t => t.UserID); 


  7. // Properties 

  8. this.Property(t => t.UserID) 

  9. .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); 


  10. this.Property(t => t.Name) 

  11. .IsRequired() 

  12. .HasMaxLength(50); 


  13. this.Property(t => t.Email) 

  14. .IsRequired() 

  15. .HasMaxLength(100); 


  16. this.Property(t => t.Telephone) 

  17. .HasMaxLength(50); 


  18. this.Property(t => t.Mobilephone) 

  19. .HasMaxLength(20); 


  20. this.Property(t => t.Address) 

  21. .HasMaxLength(200); 


  22. // Table & Column Mappings 

  23. this.ToTable("UserProfile"); 

  24. this.Property(t => t.UserID).HasColumnName("UserID"); 

  25. this.Property(t => t.Name).HasColumnName("Name"); 

  26. this.Property(t => t.Sex).HasColumnName("Sex"); 

  27. this.Property(t => t.Birthday).HasColumnName("Birthday"); 

  28. this.Property(t => t.Email).HasColumnName("Email"); 

  29. this.Property(t => t.Telephone).HasColumnName("Telephone"); 

  30. this.Property(t => t.Mobilephone).HasColumnName("Mobilephone"); 

  31. this.Property(t => t.Address).HasColumnName("Address"); 

  32. this.Property(t => t.CreateDate).HasColumnName("CreateDate"); 


  33. // Relationships 

  34. this.HasRequired(t => t.User) 

  35. .WithRequiredPrincipal(t => t.UserProfile); 

  36. } 

  37. } 

4.多对多关系

  • 除了生成实体类定义的属性表之外,还会生成一个中间表。用于体现两个实体表之间的多对多的关系。
  1. public partial class User 

  2. { 

  3. public int UserID { get; set; } 

  4. public string UserName { get; set; } 

  5. public string Password { get; set; } 

  6. public Nullable<bool> IsValid { get; set; } 


  7. public virtual ICollection<Role> Roles { get; set; } 

  8. } 


  9. public partial class Role 

  10. { 

  11. public Role() 

  12. { 

  13. this.Users = new List<User>(); 

  14. } 


  15. public int RoleID { get; set; } 

  16. public string RoleName { get; set; } 


  17. public virtual ICollection<User> Users { get; set; } 

  18. } 

  • 默认启用多对多的数据级联删除

  1. protected override void OnModelCreating(DbModelBuilder modelBuilder) 

  2. { 

  3. // 禁用多对多关系表的级联删除 

  4. modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>(); 

  5. } 


Fluent API方式

  1. public partial class User 

  2. { 

  3. public int UserID { get; set; } 

  4. public string UserName { get; set; } 

  5. public string Password { get; set; } 

  6. public Nullable<bool> IsValid { get; set; } 


  7. public virtual ICollection<Role> Roles { get; set; } 

  8. } 

  9. --------------------------------------------------------------------- 

  10. public class UserMap : EntityTypeConfiguration<User> 

  11. { 

  12. public UserMap() 

  13. { 

  14. // Primary Key 

  15. this.HasKey(t => t.UserID); 


  16. // Properties 

  17. this.Property(t => t.UserName) 

  18. .HasMaxLength(50); 


  19. this.Property(t => t.Password) 

  20. .HasMaxLength(100); 


  21. // Table & Column Mappings 

  22. this.ToTable("User"); 

  23. this.Property(t => t.UserID).HasColumnName("UserID"); 

  24. this.Property(t => t.UserName).HasColumnName("UserName"); 

  25. this.Property(t => t.Password).HasColumnName("Password"); 

  26. this.Property(t => t.IsValid).HasColumnName("IsValid"); 

  27. } 

  28. } 

  29. ---------------------------------------------------------------------------- 

  30. public partial class Role 

  31. { 

  32. public int RoleID { get; set; } 

  33. public string RoleName { get; set; } 


  34. public virtual ICollection<User> Users { get; set; } 

  35. } 

  36. ----------------------------------------------------------------------------- 

  37. public class RoleMap : EntityTypeConfiguration<Role> 

  38. { 

  39. public RoleMap() 

  40. { 

  41. // Primary Key 

  42. this.HasKey(t => t.RoleID); 


  43. // Properties 

  44. this.Property(t => t.RoleName) 

  45. .HasMaxLength(50); 


  46. // Table & Column Mappings 

  47. this.ToTable("Role"); 

  48. this.Property(t => t.RoleID).HasColumnName("RoleID"); 

  49. this.Property(t => t.RoleName).HasColumnName("RoleName"); 


  50. // Relationships 

  51. this.HasMany(t => t.Users) 

  52. .WithMany(t => t.Roles) 

  53. .Map(m => 

  54. { 

  55. m.ToTable("UserRole"); 

  56. m.MapLeftKey("RoleID"); 

  57. m.MapRightKey("UserID"); 

  58. }); 

  59. } 

  60. } 


5.一对多自反关系

Fluent API方式

  1. public class Category 

  2. { 

  3. public int CategoryID { get; set; } 

  4. public int CategoryNo { get; set; } 

  5. public string CategoryName { get; set; } 

  6. public Nullable<int> ParentID { get; set; } 

  7. public virtual Category Parent { get; set; } 

  8. public virtual ICollection<Category> Children { get; set; } 

  9. } 

  10. --------------------------------------------------------------------- 

  11. public class CategoryMap : EntityTypeConfiguration<Category> 

  12. { 

  13. public CategoryMap() 

  14. { 

  15. // Primary Key 

  16. this.HasKey(t => t.CategoryID); 


  17. // Properties 

  18. this.Property(t => t.CategoryName) 

  19. .IsRequired() 

  20. .HasMaxLength(50); 


  21. // Table & Column Mappings 

  22. this.ToTable("Category"); 

  23. this.Property(t => t.CategoryID).HasColumnName("CategoryID"); 

  24. this.Property(t => t.CategoryNo).HasColumnName("CategoryNo"); 

  25. this.Property(t => t.CategoryName).HasColumnName("CategoryName"); 

  26. this.Property(t => t.ParentID).HasColumnName("ParentID"); 


  27. // Relationships 

  28. this.HasOptional(t => t.Parent) 

  29. .WithMany(t => t.Children) 

  30. .HasForeignKey(d => d.ParentID); 

  31. } 

  32. } 

6.多对多自反关系

Fluent API方式

  1. /// <summary> 

  2. /// Family表多对多自反关系 

  3. /// </summary> 

  4. public partial class Family 

  5. { 

  6. public Family() 

  7. { 

  8. this.Parents = new List<Family>(); 

  9. this.Children = new List<Family>(); 

  10. } 


  11. public int FamilyID { get; set; } 

  12. public string Name { get; set; } 

  13. public Nullable<bool> Sex { get; set; } 

  14. public Nullable<System.DateTime> Birthday { get; set; } 

  15. public virtual ICollection<Family> Parents { get; set; } 

  16. public virtual ICollection<Family> Children { get; set; } 

  17. } 

  18. --------------------------------------------------------------------- 

  19. public class FamilyMap : EntityTypeConfiguration<Family> 

  20. { 

  21. public FamilyMap() 

  22. { 

  23. // Primary Key 

  24. this.HasKey(t => t.FamilyID); 


  25. // Properties 

  26. this.Property(t => t.Name) 

  27. .HasMaxLength(50); 


  28. // Table & Column Mappings 

  29. this.ToTable("Family"); 

  30. this.Property(t => t.FamilyID).HasColumnName("FamilyID"); 

  31. this.Property(t => t.Name).HasColumnName("Name"); 

  32. this.Property(t => t.Sex).HasColumnName("Sex"); 

  33. this.Property(t => t.Birthday).HasColumnName("Birthday"); 


  34. // Relationships 

  35. this.HasMany(t => t.Parents) 

  36. .WithMany(t => t.Children) 

  37. .Map(m => 

  38. { 

  39. m.ToTable("FamilyRelationship"); 

  40. m.MapLeftKey("ParentID"); 

  41. m.MapRightKey("ChildID"); 

  42. }); 

  43. } 

  44. } 

EFCodeFirst关系映射约定的更多相关文章

  1. EFCodeFirst属性映射约定

    EFCodeFirst属性映射约定 EFCodeFirst 属性映射约定 CodeFirst与数据表之间得映射方式又两种:Data Annotation和Fluent API 默认约定: 表名为类名的 ...

  2. Entity Framework Code First关系映射约定

    本篇随笔目录: 1.外键列名默认约定 2.一对多关系 3.一对一关系 4.多对多关系 5.一对多自反关系 6.多对多自反关系 在关系数据库中,不同表之间往往不是全部都单独存在,而是相互存在关联的.两个 ...

  3. Entity Framework Code First主外键关系映射约定

    本篇随笔目录: 1.外键列名默认约定 2.一对多关系 3.一对一关系 4.多对多关系 5.一对多自反关系 6.多对多自反关系 在关系数据库中,不同表之间往往不是全部都单独存在,而是相互存在关联的.两个 ...

  4. Entity Framework Code First关系映射约定【l转发】

    本篇随笔目录: 1.外键列名默认约定 2.一对多关系 3.一对一关系 4.多对多关系 5.一对多自反关系 6.多对多自反关系 在关系数据库中,不同表之间往往不是全部都单独存在,而是相互存在关联的.两个 ...

  5. Entity Framework Code First属性映射约定

    Entity Framework Code First与数据表之间的映射方式有两种实现:Data Annotation和Fluent API.本文中采用创建Product类为例来说明tity Fram ...

  6. EntityFramework 6.x和EntityFramework Core关系映射中导航属性必须是public?

    前言 不知我们是否思考过一个问题,在关系映射中对于导航属性的访问修饰符是否一定必须为public呢?如果从未想过这个问题,那么我们接下来来探讨这个问题. EF 6.x和EF Core 何种情况下必须配 ...

  7. 【EF】Entity Framework实现属性映射约定

    Entity Framework Code First属性映射约定中“约定”一词,在原文版中为“Convention”,翻译成约定或许有些不好理解,这也是网上比较大多数的翻译,我们就当这是Entity ...

  8. 补习知识:Entity Framework Code First属性映射约定

    Entity Framework Code First与数据表之间的映射方式有两种实现:Data Annotation和Fluent API.本文中采用创建Product类为例来说明tity Fram ...

  9. 你所不知道的库存超限做法 服务器一般达到多少qps比较好[转] JAVA格物致知基础篇:你所不知道的返回码 深入了解EntityFramework Core 2.1延迟加载(Lazy Loading) EntityFramework 6.x和EntityFramework Core关系映射中导航属性必须是public? 藏在正则表达式里的陷阱 两道面试题,带你解析Java类加载机制

    你所不知道的库存超限做法 在互联网企业中,限购的做法,多种多样,有的别出心裁,有的因循守旧,但是种种做法皆想达到的目的,无外乎几种,商品卖的完,系统抗的住,库存不超限.虽然短短数语,却有着说不完,道不 ...

随机推荐

  1. Snapshot查询所有快照

    今天使用snapshot list这个命令时查询出了所有的表,没注意下面报错: NoMethodError:undefined method '-@' for #<Array:0x54326e9 ...

  2. 删括号(dp)

    题目链接:https://ac.nowcoder.com/acm/problem/21303 思路:删括号的时候一定要时刻保证左括号数量比右括号多,我们可以定义dp[i][j][k]表示考虑AA前i个 ...

  3. Alternating Strings Gym - 100712D 简单dp && Alternating Strings II Gym - 100712L 数据结构优化dp

    比赛链接:https://vjudge.net/contest/405905#problem/D 题意: 给你一个长度为n的由0或1构成的串s,你需要切割这个串,要求切割之后的每一个子串长度要小于等于 ...

  4. Network UVA - 315 无向图找割点

    题意: 给你一个无向图,你需要找出来其中有几个割点 割点/割项: 1.u不为搜索起点,low[v]>=dfn[u] 2.u为搜索起点,size[ch]>=2 3.一般情况下,不建议在tar ...

  5. hdu5360 Hiking

    Problem Description There are n soda conveniently labeled by 1,2,-,n. beta, their best friends, want ...

  6. 一张图解决ThreadLocal

    一张图解决ThreadLocal 一.前言 年底梳理知识体系时,研究了一下ThreadLocal的源码,整理了一张核心图. 想着,都走到这一步了,那就写一篇深度解读的文章吧.看过我之前文章的小伙伴都知 ...

  7. WPF 中的逻辑树(Logical Tree)与可视化元素树(Visual Tree)

    一.前言 ​ WPF 中有两种"树":逻辑树(Logical Tree)和可视化元素树(Visual Tree). Logical Tree 最显著的特点就是它完全由布局组件和控件 ...

  8. [Golang]-6 超时处理、非阻塞通道操作、通道的关闭和遍历

    目录 超时处理 非阻塞通道操作 通道的关闭 通道遍历 超时处理 超时 对于一个连接外部资源,或者其它一些需要花费执行时间的操作的程序而言是很重要的. 得益于通道和 select,在 Go中实现超时操作 ...

  9. Leetcode(94)-二叉树的中序遍历

    给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 思路:和上篇的前序遍历一样,同样有递归和非递归的做法 (1)递归 vecto ...

  10. 2019南昌网络赛H The Nth Item(二阶线性数列递推 + 广义斐波那契循环节 + 分段打表)题解

    题意: 传送门 已知\(F(n)=3F(n-1)+2F(n-2) \mod 998244353,F(0)=0,F(1)=1\),给出初始的\(n_1\)和询问次数\(q\),设每一次的答案\(a_i= ...