本篇随笔目录:

  1、外键列名默认约定

  2、一对多关系

  3、一对一关系

  4、多对多关系

  5、一对多自反关系

  6、多对多自反关系

  在关系数据库中,不同表之间往往不是全部都单独存在,而是相互存在关联的。两个不同表之间可以存在外键依赖关系,一个表自身也可以有自反关系(表中的一个字段引用主键,从而也是外键字段)。

  Entity Framework Code First默认多重关系的一些约定规则:

  一对多关系:两个类中分别包含一个引用和一个集合属性,也可以是一个类包含另一个类的引用属性,或一个类包含另一个类的集合属性。如在本篇接下来用到的例子Category类和Product类,要使得Category与Product之间具有一对多关系,Entity Framework Code First可以有3种体现方式:

  1>、在Category类中定义ICollection<Product> Products集合属性,同时在Product类中定义Category Category引用属性。

  2>、仅在Category类中定义ICollection<Product> Products集合属性。

  3>、仅在Product类中定义Category Category引用属性。

  多对多关系:两个类分别包含对方的一个集合属性。如在本篇接下来用到的例子User类和Role类,要使得User与Role之间具有多对多关系,即一个用户可以属于多个角色,一个角色可以有多个用户,则需要在User类中需要定义一个ICollection<Role> Roles集合属性,同时在Role类中需要定义一个ICollection<User> Users属性。

  一对一关系:两个类分别包含对方的一个引用属性。如在本篇接下来用到的例子User类和UserProfile类,要使得User与UserProfile之间具有一对一关系,则需要在User类中定义一个UserProfile UserProfile的引用属性,同时在UserProfile类中定义一个User User的引用属性。

  下面具体描述Entity Framework Code First生成外键的默认约定,并通过实例展示Entity Framework Code First处理一个表及多个表之间的关系。

  1、外键列名默认约定

  Entity Framework Code First在根据默认约定创建外键时,外键列的名称存在3种方式。在《Programming Entity Framework Code First》一书中,给出的3种外键列名的约定方式是:[Target Type Key Name], [Target Type Name] + [Target Type Key Name], or [Navigation Property Name] + [Target Type Key Name],对应的中文翻译为:[目标类型的键名],[目标类型名称]+[目标类型键名称],或[引用属性名称]+[目标类型键名称]。

  Entity Framework Code First外键默认约束生成的外键在分别满足3种不同的条件下,外键列名有3种不同的命名规则。且经过测试这3种不同的外键名称命名之间存在优先级:[目标类型的键名] > [引用属性名称]+[目标类型键名称] > [目标类型名称]+[目标类型键名称]。接下来以Product类及Category类为例,分别测试外键列名称的3中不同生成方式,Category与Product为一对多关系。

  1>、[目标类型的键名]

  这种方式为要求在Product表中外键列名与Category表中的主键列名相同,所以也就要求在Product类中有定义与Category类中作为主键的属性。如在Category类中主键属性为CategoryID,则需要在Product类中也定义一个CategoryID的属性。

  文件Category.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Portal.Entities
{
public class Category
{
public int CategoryID { get; set; }
public string CategoryName { get; set; } public virtual ICollection<Product> Products { get; set; }
}
}

  文件Product.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Portal.Entities
{
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public decimal UnitPrice { get; set; }
public int CategoryID { get; set; } public virtual Category Category { get; set; }
}
}

  说明:在Category类及Product类中的引用属性及集合属性前加virtual修饰,为的是Entity Framework Code First的延迟加载功能。不使用virtual修饰,在Category类的一个实例要查询包含的Product实例时,将不会启用延迟加载。当然Entity Framework Code First延迟加载并不是必须的,所以virtual修饰符也可以不加。

  在定义以上两个类之后,不再添加任何的Entity Framework Code First与数据库的映射配置,运行之后,生成的数据表结构为:

  从生成的Categories与Products表结构可以看出,在Products表中的外键CategoryID与引用的表Categories主键名称相同。跟踪Entity Framework Code First生成数据表的执行脚本可以看到具体生成外键的SQL语句。

ALTER TABLE [dbo].[Products] ADD CONSTRAINT [FK_dbo.Products_dbo.Categories_CategoryID] FOREIGN KEY ([CategoryID]) REFERENCES [dbo].[Categories] ([CategoryID]) ON DELETE CASCADE

  同时,从生成外键的脚本还能看出一点,Entity Framework Code First生成外键是启用级联删除功能的。即当删除Categories表中一条记录时,数据库会自动联带删除Products表中属于该类别的记录。

  在数据库中生成的Products表,查看外键FK_dbo.Products_dbo.Categories_CategoryID属性,其的确有启用级联删除功能。

  2>、[目标类型名称]+[目标类型键名称]

  这种方式要求在Product表中外键列名为Category类名+Category类中键名称,即在Products表中生成的外键名称为Category_CategoryID。示例:在Category类中添加ICollection<Product> Products的集合属性,而在Product类中不做任何与Category关联的代码,也不定义CategoryID属性。

  文件Category.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Portal.Entities
{
public class Category
{
public int CategoryID { get; set; }
public string CategoryName { get; set; } public virtual ICollection<Product> Products { get; set; }
}
}

  文件Product.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Portal.Entities
{
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public decimal UnitPrice { get; set; }
}
}

  在定义以上两个类之后,不再添加任何的Entity Framework Code First与数据库的映射配置,运行之后,生成的数据表结构为:

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

  这种方式为要求在Product表中外键列名为在Product类中引用Category的属性名称 + Category类的主键名称。如:在Product类中定义一个Category属性Cat,则生成的外键名称为Cat_CategoryID。

  文件Category.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Portal.Entities
{
public class Category
{
public int CategoryID { get; set; }
public string CategoryName { get; set; }
}
}

  文件Product.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Portal.Entities
{
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public decimal UnitPrice { get; set; } /// <summary>
/// 这里为演示,使用Cat作为Category的缩写。
/// </summary>
public virtual Category Cat { get; set; }
}
}

  在定义以上两个类之后,不再添加任何的Entity Framework Code First与数据库的映射配置,运行之后,生成的数据表结构为:

  关于3种不同的外键名称命名之间存在优先级:[目标类型的键名] > [引用属性名称]+[目标类型键名称] > [目标类型名称]+[目标类型键名称]的测试方法:

  [目标类型的键名]的最高优先级:只要在Product类中定义了CategoryID的属性,在Products表中生成的外键列名都只会为CategoryID。

  [引用属性名称]+[目标类型键名称] > [目标类型名称]+[目标类型键名称]:只要在Product类中定义Cat属性,不管Category类中是否定义Products属性,生成的Products表中外键都只会是Cat_CategoryID。

  2、一对多关系

  Entity Framework Code First在根据定义的类生成数据表时,数据表之间的外键关系及所生成的外键列名有默认的约定。但这种约定同样可以进行修改,如将不满足默认外键约定的属性来作为生成表的外键。示例:Category类与Product类,在Product类中定义一个属性CatID,要将CatID属性作为Product的引用Category的外键,而按照Entity Framework Code First的默认约定是不会的。要做到需要的CatID作为外键,同样可以使用Data Annotations和Fluent API两种方式实现。

  1>、Data Annotations方式

  文件类Category.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Portal.Entities
{
public class Category
{
public int CategoryID { get; set; }
public string CategoryName { get; set; } public virtual ICollection<Product> Products { get; set; }
}
}

  文件类Product.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.ComponentModel.DataAnnotations.Schema; namespace Portal.Entities
{
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public decimal UnitPrice { get; set; } public int CatID { get; set; } [ForeignKey("CatID")]
public virtual Category Category { get; set; }
}
}

  在定义以上两个类之后,不再添加任何的Entity Framework Code First与数据库的映射配置,运行之后,生成的数据表结构为:

  查看Products表的外键咧CatID引用关系:

  其中,在Product类中,为设置CatID属性为外键的代码为:

public int CatID { get; set; }
[ForeignKey("CatID")]
public virtual Category Category { get; set; }

  该段实现方式还可以改为:

[ForeignKey("Category")]
public int CatID { get; set; }
public virtual Category Category { get; set; }

  2>、Fluent API方式

  文件类Category.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Portal.Entities
{
public class Category
{
public int CategoryID { get; set; }
public string CategoryName { get; set; } public virtual ICollection<Product> Products { get; set; }
}
}

  文件类Product.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Portal.Entities
{
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public decimal UnitPrice { get; set; } public int CatID { get; set; } public virtual Category Category { get; set; }
}
}

  文件类PortalContext.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Data.Entity; using Portal.Entities; namespace Portal
{
public class PortalContext : DbContext
{
static PortalContext()
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<PortalContext>());
} public DbSet<Category> Categories { get; set; }
public DbSet<Product> Products { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Category>()
.HasMany(t => t.Products)
.WithRequired(t => t.Category)
.HasForeignKey(d => d.CatID);
modelBuilder.Entity<Product>()
.HasRequired(t => t.Category)
.WithMany(t => t.Products)
.HasForeignKey(d => d.CatID);
}
}
}

  说明:在PortalContext.cs的OnModelCreating方法中,对两个实体类Category及Product均添加了Fluent API形式的关系配置。对于Entity Framework Code First而言,两个实体类之间的关系,可以两个类中均添加关系映射配置,也可以只对其中任意一个实体类添加关系映射配置。即在PortalContext.cs的OnModelCreating方法中可以只包含Category或只包含Product类的关系映射配置。这里从Entity Framework Code First的使用经验及技巧,建议将实体类之间关系映射配置在包含外键的类中。即OnModelCreating中只添加对Product实体类的关系映射配置,这样做有一个好处,当Category有多个表引用它时,可以将外键均配置在引用它的实体类中,从而降低Category类的复杂度,同时也有益于代码的维护。

  即在PortalContext.cs的OnModelCreating方法只需下面的定义即可:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>()
.HasRequired(t => t.Category)
.WithMany(t => t.Products)
.HasForeignKey(d => d.CatID);
}

  Entity Framework Code First根据一对多关系关系生成的外键引用约束默认是有级联删除的,可以通过以下方式禁用Category与Product之间的级联删除。

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>()
.HasRequired(t => t.Category)
.WithMany(t => t.Products)
.HasForeignKey(d => d.CatID)
.WillCascadeOnDelete(false);
}

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

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
}

  虽然Entity Framework Code First是可以支持外键列名自定义的,但在实际的项目中,更多的外键列名称还是与所引用表的主键列名相同。即在Category表中主键为CategoryID,在Product表中外键列名称还是为CategoryID。

  Entity Framework Code First的Fluent API配置实体类与表的映射关系,还可以将所有的实体类与表的映射全部写在一个类中,这样可以方便代码的维护。

  文件类Category.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Portal.Entities
{
public partial class Category
{
public Category()
{
this.Products = new List<Product>();
} public int CategoryID { get; set; }
public string CategoryName { get; set; } public virtual ICollection<Product> Products { get; set; }
}
}

  文件类CategoryMap.cs,用于描述Category类与生成的表之间的映射关系:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration; using Portal.Entities; namespace Portal.Mapping
{
public class CategoryMap : EntityTypeConfiguration<Category>
{
public CategoryMap()
{
// Primary Key
this.HasKey(t => t.CategoryID); // Properties
this.Property(t => t.CategoryName)
.IsRequired()
.HasMaxLength(); // Table & Column Mappings
this.ToTable("Category");
this.Property(t => t.CategoryID).HasColumnName("CategoryID");
this.Property(t => t.CategoryName).HasColumnName("CategoryName");
}
}
}

  文件类Product.cs:

using System;
using System.Collections.Generic; namespace Portal.Entities
{
public partial class Product
{
public int ProductID { get; set; }
public int CategoryID { get; set; }
public string ProductName { get; set; }
public Nullable<decimal> UnitPrice { get; set; }
public Nullable<int> Quantity { get; set; } public virtual Category Category { get; set; }
}
}

  文件类ProductMap.cs,用于描述Product类与生成的表之间的映射关系:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration; using Portal.Entities; namespace Portal.Mapping
{
public class ProductMap : EntityTypeConfiguration<Product>
{
public ProductMap()
{
// Primary Key
this.HasKey(t => t.ProductID); // Properties
this.Property(t => t.ProductName)
.IsRequired()
.HasMaxLength(); // Table & Column Mappings
this.ToTable("Product");
this.Property(t => t.ProductID).HasColumnName("ProductID");
this.Property(t => t.CategoryID).HasColumnName("CategoryID");
this.Property(t => t.ProductName).HasColumnName("ProductName");
this.Property(t => t.UnitPrice).HasColumnName("UnitPrice");
this.Property(t => t.Quantity).HasColumnName("Quantity"); // Relationships
this.HasRequired(t => t.Category)
.WithMany(t => t.Products)
.HasForeignKey(d => d.CategoryID);
}
}
}

  PortalContext.cs的OnModelCreating方法:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new CategoryMap());
modelBuilder.Configurations.Add(new ProductMap());
}

  3、一对一关系

  在一对一关系中,两个表均有各自的主键,但要看哪个表的主键同时作为外键引用另一个表的主键。示例以User类与UserProfile类作为两个具有一对一关系的类,其中User类包含作为主键的UserID属性,UserProfile类包含作为主键的ProfileID的属性。

  1>、Data Annotations方式

  文件类User.cs:

using System;
using System.Collections.Generic; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema; namespace Portal.Entities
{
public partial class User
{
[Key]
public int UserID { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public Nullable<bool> IsValid { get; set; } public virtual UserProfile UserProfile { get; set; }
}
}

  文件类UserProfile.cs:

using System;
using System.Collections.Generic; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema; namespace Portal.Entities
{
public partial class UserProfile
{
[Key]
[ForeignKey("User")]
public int ProfileID { get; set; }
public string Name { get; set; }
public Nullable<bool> Sex { get; set; }
public Nullable<DateTime> Birthday { get; set; }
public string Email { get; set; }
public string Telephone { get; set; }
public string Mobilephone { get; set; }
public string Address { get; set; }
public Nullable<DateTime> CreateDate { get; set; } public virtual User User { get; set; }
}
}

  在定义以上两个类之后,不再添加任何的Entity Framework Code First与数据库的映射配置,运行之后,生成的数据表结构为:

  在生成的数据表中,UserProfile表中的主键ProfileID同时也作为外键引用User表中的主键UserID。

  修改文件类User.cs:

using System;
using System.Collections.Generic; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema; namespace Portal.Entities
{
public partial class User
{
[Key]
[ForeignKey("UserProfile")]
public int UserID { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public Nullable<bool> IsValid { get; set; } public virtual UserProfile UserProfile { get; set; }
}
}

  修改文件类UserProfile.cs:

using System;
using System.Collections.Generic; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema; namespace Portal.Entities
{
public partial class UserProfile
{
[Key]
public int ProfileID { get; set; }
public string Name { get; set; }
public Nullable<bool> Sex { get; set; }
public Nullable<DateTime> Birthday { get; set; }
public string Email { get; set; }
public string Telephone { get; set; }
public string Mobilephone { get; set; }
public string Address { get; set; }
public Nullable<DateTime> CreateDate { get; set; } public virtual User User { get; set; }
}
}

  则实体类执行之后生成的数据表User主键UserID将同时作为外键引用UserProfile表的主键ProfileID。

  2>、Fluent API方式

  Fluent API设置实体类生成的表引用与被引用通过WithRequiredPrincipal、WithRequiredDependent及WithOptionalPrincipal、WithOptionalDependent来设置,使用Principal属性的实体类将被另外的实体类生成的表引用,使用Dependent属性的实体类将引用另外的实体类。

  文件类User.cs:

using System;
using System.Collections.Generic; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema; namespace Portal.Entities
{
public partial class User
{
public int UserID { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public Nullable<bool> IsValid { get; set; } public virtual UserProfile UserProfile { get; set; }
}
}

  映射文件类UserMap.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration; using Portal.Entities; namespace Portal.Mapping
{
public class UserMap : EntityTypeConfiguration<User>
{
public UserMap()
{
// Primary Key
this.HasKey(t => t.UserID); // Properties
this.Property(t => t.UserName)
.HasMaxLength(); this.Property(t => t.Password)
.HasMaxLength(); // Table & Column Mappings
this.ToTable("User");
this.Property(t => t.UserID).HasColumnName("UserID");
this.Property(t => t.UserName).HasColumnName("UserName");
this.Property(t => t.Password).HasColumnName("Password");
this.Property(t => t.IsValid).HasColumnName("IsValid");
}
}
}

  文件类UserProfile.cs:

using System;
using System.Collections.Generic; namespace Portal.Entities
{
public partial class UserProfile
{
public int UserID { get; set; }
public string Name { get; set; }
public Nullable<bool> Sex { get; set; }
public Nullable<DateTime> Birthday { get; set; }
public string Email { get; set; }
public string Telephone { get; set; }
public string Mobilephone { get; set; }
public string Address { get; set; }
public Nullable<DateTime> CreateDate { get; set; } public virtual User User { get; set; }
}
}

  映射文件类UserProfileMap.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration; using Portal.Entities; namespace Portal.Mapping
{
public class UserProfileMap : EntityTypeConfiguration<UserProfile>
{
public UserProfileMap()
{
// Primary Key
this.HasKey(t => t.UserID); // Properties
this.Property(t => t.UserID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); this.Property(t => t.Name)
.IsRequired()
.HasMaxLength(); this.Property(t => t.Email)
.IsRequired()
.HasMaxLength(); this.Property(t => t.Telephone)
.HasMaxLength(); this.Property(t => t.Mobilephone)
.HasMaxLength(); this.Property(t => t.Address)
.HasMaxLength(); // Table & Column Mappings
this.ToTable("UserProfile");
this.Property(t => t.UserID).HasColumnName("UserID");
this.Property(t => t.Name).HasColumnName("Name");
this.Property(t => t.Sex).HasColumnName("Sex");
this.Property(t => t.Birthday).HasColumnName("Birthday");
this.Property(t => t.Email).HasColumnName("Email");
this.Property(t => t.Telephone).HasColumnName("Telephone");
this.Property(t => t.Mobilephone).HasColumnName("Mobilephone");
this.Property(t => t.Address).HasColumnName("Address");
this.Property(t => t.CreateDate).HasColumnName("CreateDate"); // Relationships
this.HasRequired(t => t.User)
.WithRequiredDependent(t => t.UserProfile);
}
}
}

  在以上实体类及实体映射类执行以后,生成的数据表结构如下:

  在生成的表结构中,UserProfile表中的主键UserID同时也作为外键引用User表的主键UserID。若修改UserProfileMap.cs如下,则生成的表结构User表的主键UserID将作为你外键引用UserProfile表的主键UserID。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration; using Portal.Entities; namespace Portal.Mapping
{
public class UserProfileMap : EntityTypeConfiguration<UserProfile>
{
public UserProfileMap()
{
// Primary Key
this.HasKey(t => t.UserID); // Properties
this.Property(t => t.UserID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); this.Property(t => t.Name)
.IsRequired()
.HasMaxLength(); this.Property(t => t.Email)
.IsRequired()
.HasMaxLength(); this.Property(t => t.Telephone)
.HasMaxLength(); this.Property(t => t.Mobilephone)
.HasMaxLength(); this.Property(t => t.Address)
.HasMaxLength(); // Table & Column Mappings
this.ToTable("UserProfile");
this.Property(t => t.UserID).HasColumnName("UserID");
this.Property(t => t.Name).HasColumnName("Name");
this.Property(t => t.Sex).HasColumnName("Sex");
this.Property(t => t.Birthday).HasColumnName("Birthday");
this.Property(t => t.Email).HasColumnName("Email");
this.Property(t => t.Telephone).HasColumnName("Telephone");
this.Property(t => t.Mobilephone).HasColumnName("Mobilephone");
this.Property(t => t.Address).HasColumnName("Address");
this.Property(t => t.CreateDate).HasColumnName("CreateDate"); // Relationships
this.HasRequired(t => t.User)
.WithRequiredPrincipal(t => t.UserProfile);
}
}
}

  4、多对多关系

  Entity Framework Code First在根据定义的多对多关系的类生成数据表时,除了生成实体类定义的属性表之外,还会生成一个中间表。用于体现两个实体表之间的多对多的关系。示例实体类User与Role为多对多关系,一个用户可以属于多个角色,一个角色可以包含多个用户。

  文件类User.cs:

using System;
using System.Collections.Generic; namespace Portal.Entities
{
public partial class User
{
public int UserID { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public Nullable<bool> IsValid { get; set; } public virtual ICollection<Role> Roles { get; set; }
}
}

  文件类Role.cs:

using System;
using System.Collections.Generic; namespace Portal.Entities
{
public partial class Role
{
public Role()
{
this.Users = new List<User>();
} public int RoleID { get; set; }
public string RoleName { get; set; } public virtual ICollection<User> Users { get; set; }
}
}

  在定义以上两个类之后,不再添加任何的Entity Framework Code First与数据库的映射配置,运行之后,生成的数据表结构为:

  从以上的表结构中,可以看出,实体类运行之后,除了生成Users表和Roles表之外,还生成了RoleUsers表作为中介表,体现Users表和Roles表之间的多对多关联关系。中介表RoleUsers的字段生成规则按照 [目标类型名称]+[目标类型键名称] 的约定。

  Entity Framework Code First根据默认约定生成的多对多关联关系的表时,默认启用多对多的数据级联删除,可以添加代码进行禁用。

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// 禁用多对多关系表的级联删除
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
}

  FluentAPI实现方式:

  文件类User.cs:

using System;
using System.Collections.Generic; namespace Portal.Entities
{
public partial class User
{
public int UserID { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public Nullable<bool> IsValid { get; set; } public virtual ICollection<Role> Roles { get; set; }
}
}

  映射文件类UserMap.cs:

using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration; using Portal.Entities; namespace Portal.Mapping
{
public class UserMap : EntityTypeConfiguration<User>
{
public UserMap()
{
// Primary Key
this.HasKey(t => t.UserID); // Properties
this.Property(t => t.UserName)
.HasMaxLength(); this.Property(t => t.Password)
.HasMaxLength(); // Table & Column Mappings
this.ToTable("User");
this.Property(t => t.UserID).HasColumnName("UserID");
this.Property(t => t.UserName).HasColumnName("UserName");
this.Property(t => t.Password).HasColumnName("Password");
this.Property(t => t.IsValid).HasColumnName("IsValid");
}
}
}

  文件类Role.cs:

using System;
using System.Collections.Generic; namespace Portal.Entities
{
public partial class Role
{
public int RoleID { get; set; }
public string RoleName { get; set; } public virtual ICollection<User> Users { get; set; }
}
}

  映射文件类RoleMap.cs:

using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration; using Portal.Entities; namespace Portal.Mapping
{
public class RoleMap : EntityTypeConfiguration<Role>
{
public RoleMap()
{
// Primary Key
this.HasKey(t => t.RoleID); // Properties
this.Property(t => t.RoleName)
.HasMaxLength(); // Table & Column Mappings
this.ToTable("Role");
this.Property(t => t.RoleID).HasColumnName("RoleID");
this.Property(t => t.RoleName).HasColumnName("RoleName"); // Relationships
this.HasMany(t => t.Users)
.WithMany(t => t.Roles)
.Map(m =>
{
m.ToTable("UserRole");
m.MapLeftKey("RoleID");
m.MapRightKey("UserID");
});
}
}
}

  运行之后生成的表结构:

  5、一对多自反关系

  一对多自反关系,即一个表存在一个外键列引用自身的主键。在项目中,最常见的一对多自反关系为分类表,分类表通过一个ParentID列保存引用主键,已实现无限级递归。

  Fluent API实现方式:

  文件类Category.cs:

using System;
using System.Collections.Generic; namespace Portal.Entities
{
public class Category
{
public int CategoryID { get; set; }
public int CategoryNo { get; set; }
public string CategoryName { get; set; }
public Nullable<int> ParentID { get; set; }
public virtual Category Parent { get; set; }
public virtual ICollection<Category> Children { get; set; }
}
}

  映射文件类CategoryMap.cs:

using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration; using Portal.Entities; namespace Portal.Mapping
{
public class CategoryMap : EntityTypeConfiguration<Category>
{
public CategoryMap()
{
// Primary Key
this.HasKey(t => t.CategoryID); // Properties
this.Property(t => t.CategoryName)
.IsRequired()
.HasMaxLength(); // Table & Column Mappings
this.ToTable("Category");
this.Property(t => t.CategoryID).HasColumnName("CategoryID");
this.Property(t => t.CategoryNo).HasColumnName("CategoryNo");
this.Property(t => t.CategoryName).HasColumnName("CategoryName");
this.Property(t => t.ParentID).HasColumnName("ParentID"); // Relationships
this.HasOptional(t => t.Parent)
.WithMany(t => t.Children)
.HasForeignKey(d => d.ParentID);
}
}
}

  以上代码在运行之后,生成的数据表:

  6、多对多自反关系

  多对多关系示例:Family中一条记录可能有多个Parents,也可能有多个Children。

  文件类Family.cs:

using System;
using System.Collections.Generic; namespace Portal.Entities
{
/// <summary>
/// Family表多对多自反关系
/// </summary>
public partial class Family
{
public Family()
{
this.Parents = new List<Family>();
this.Children = new List<Family>();
} public int FamilyID { get; set; }
public string Name { get; set; }
public Nullable<bool> Sex { get; set; }
public Nullable<System.DateTime> Birthday { get; set; }
public virtual ICollection<Family> Parents { get; set; }
public virtual ICollection<Family> Children { get; set; }
}
}

  映射文件类FamilyMap.cs:

using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration; using Portal.Entities; namespace Portal.Mapping
{
public class FamilyMap : EntityTypeConfiguration<Family>
{
public FamilyMap()
{
// Primary Key
this.HasKey(t => t.FamilyID); // Properties
this.Property(t => t.Name)
.HasMaxLength(); // Table & Column Mappings
this.ToTable("Family");
this.Property(t => t.FamilyID).HasColumnName("FamilyID");
this.Property(t => t.Name).HasColumnName("Name");
this.Property(t => t.Sex).HasColumnName("Sex");
this.Property(t => t.Birthday).HasColumnName("Birthday"); // Relationships
this.HasMany(t => t.Parents)
.WithMany(t => t.Children)
.Map(m =>
{
m.ToTable("FamilyRelationship");
m.MapLeftKey("ParentID");
m.MapRightKey("ChildID");
});
}
}
}

  Family类及映射配置类,在运行之后生成的数据表结构:

  在上面的表结构中,指定Family之间的中介表为FamilyRelationship,其中FamilyRelationship的两个字段ParentID及ChildID均引用Familyi表中的FamilyID作为外键。可能在实际的项目过程中,出现这种多对多自反引用关系的情况比较少见。

Entity Framework Code First关系映射约定的更多相关文章

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

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

  2. Entity Framework Code First属性映射约定 转载https://www.cnblogs.com/libingql/p/3352058.html

    Entity Framework Code First属性映射约定   Entity Framework Code First与数据表之间的映射方式有两种实现:Data Annotation和Flue ...

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

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

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

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

  5. Entity Framework Many to Many Relation Mapping(Entity Framework多对多关系映射)

    通常我们在做数据库设计时都会有两张表是多对多关系的时候,在数据库做多对多关系时候我们通常通过中间关联表来处理,那我们现在在EF中是如何处理的呢? 假设我们有如下关系,用户(User)包含多个角色(Ro ...

  6. Entity Framework Code First学习系列目录

    Entity Framework Code First学习系列说明:开发环境为Visual Studio 2010 + Entity Framework 5.0+MS SQL Server 2012, ...

  7. Entity Framework Code First学习系列

    Entity Framework Code First学习系列目录 Entity Framework Code First学习系列说明:开发环境为Visual Studio 2010 + Entity ...

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

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

  9. Entity Framework Code First 映射继承关系

    转载 http://www.th7.cn/Program/net/201301/122153.shtml Code First如何处理类之间的继承关系.Entity Framework Code Fi ...

随机推荐

  1. 代码中使用StoryBoard和DoubleAnimation的方法

    TranslateTransformを対象に.DoubleAnimation型のアニメーションを使用して.TranslateTransform.Xプロパティを ”-1 * Imageコントロールの幅” ...

  2. Python for Infomatics 第13章 网页服务二(译)

    注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 13.4 JavaScript ...

  3. MD5加密的Java实现

    在各种应用系统中,如果需要设置账户,那么就会涉及到储存用户账户信息的问题,为了保证所储存账户信息的安全,通常会采用MD5加密的方式来,进行储存.首先,简单得介绍一下,什么是MD5加密. MD5的全称是 ...

  4. 使用nose 进行Python项目的自动化测试

    一.为什么使用nose? 编写测试更容易.nose可以自动识别继承于unittest.TestCase的测试单元,并执行测试,而且,nose也可以测试非继承于unittest.TestCase的测试单 ...

  5. id选择器、类选择器、属性选择器

    在网页编辑时,通常要对样式进行各种设置.我们借助CSS样式设计中的选择器,就能很好很方便的对它们进行管理和设置了. 今天,跟大家分享一下几个常用的选择器:id选择器.类选择器.属性选择器. id选择器 ...

  6. JS中原型链继承

    当我们通过构造函数A来实现一项功能的时候,而构造函数B中需要用到构造函数A中的属性或者方法,如果我们对B中的属性或者方法进行重写就会出现冗杂的代码,同时写出来也很是麻烦.而在js中每个函数都有个原型, ...

  7. asp.net mvc添加多条数据到数据库

    mvc的视图太强大了,个人刚刚接触.(初级菜鸟,懂的不多,往大神们指点)需求是,客户点击添加按钮弹出一个框选择产品后直接添加到表单中,在表单可以自己更改产品的数量,以及一些信息.mvc表单提交的时候只 ...

  8. 简单说说.Net中的弱引用

    弱引用是什么? 要搞清楚什么是弱引用,我们需要先知道强引用是什么.强引用并不是什么深奥的概念,其实我们平时所使用的.Net引用就是强引用.例如: Cat kitty = new Cat(); 变量ki ...

  9. Cannot run gnome extension in browser

    Error Message: We cannot detect a running copy of GNOME on this system, so some parts of the interfa ...

  10. 初步认识Less

    LESS 是一个流行的样式表语言,它提供了 CSS3 也未曾实现的多种功能,让您编写 CSS 更加方便,更加直观.LESS 已经被广泛使用在多种框架中 ( 例如:BootStrap).本文将介绍 LE ...