现在,我们将学习怎么配置一对多的关系。
Visit Entity Relationship section to understand how EF manages one-to-one, one-to-many, and many-to-many relationships between the entities.
Note: You do not need to configure for one-to-many relationships either using DataAnnotations or Fluent API, if entity classes follow the conventions.
请注意;如果你是按照默认的约定来命名属性的话,不用手动配置一对多的关系,因为Code-First默认约定,会自动帮我们搞定。
一、使用数据注解特性来,配置一对多的关系
先来看看,使用默认的约定,Code-First自动帮我们配置一对多的关系的例子吧:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EF6 { public class Student { public int StudentId { get; set; } public string StudentName { get; set; } public virtual Standard Standard { get; set; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace EF6 { public class Standard { public int StandardID { get; set; } public string Description { get; set; } public virtual ICollection<Student> Students { get; set; } } }
using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EF6 { public class DbContextClass:DbContext { public DbContextClass() : base("name=ConnectionString") { } public DbSet<Student> Students { get; set; } public DbSet<Standard> Standards { get; set; } } }
然后运行程序,就自动生成数据库,我们看下默认约定,自动生成的数据库是啥样的:
上面的一对多的关系是Code-First默认帮我们搞定的。
It is recommended to include foreign key property in Student entity class. So, include StandardId as per default convention or use ForeignKey attribute to give a different name of the foreign key property. For example, the following code includes StandardRefId property for the foreign key.
现在我们来自己配置:
上面看到的外键名称,是默认生成的,我们现在自己弄个外键名字:
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EF6 { public class Student { public int StudentId { get; set; } public string StudentName { get; set; } public int StandardRefID { get; set; } [ForeignKey("StandardRefID")] public virtual Standard Standard { get; set; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace EF6 { public class Standard { public int StandardID { get; set; } public string Description { get; set; } public virtual ICollection<Student> Students { get; set; } } }
二、使用Fluent API来配置一对多的关系:
using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EF6 { public class DbContextClass:DbContext { public DbContextClass() : base("name=ConnectionString") { Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DbContextClass>()); } public DbSet<Student> Students { get; set; } public DbSet<Standard> Standards { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { //配置一对多的关系 modelBuilder.Entity<Student>().HasRequired(s => s.Standard).WithMany(s => s.Students); base.OnModelCreating(modelBuilder); } } }
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EF6 { public class Student { public int StudentId { get; set; } public string StudentName { get; set; } // public int StandardRefID { get; set; } //[ForeignKey("StandardRefID")] public virtual Standard Standard { get; set; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace EF6 { public class Standard { public int StandardID { get; set; } public string Description { get; set; } public virtual ICollection<Student> Students { get; set; } } }
可以看到生成的数据库是这样的:
看到没有,虽然生成了一对多的关系,但是外键的名字,还是默认的,我想自定义,接着我们修改一下代码:
改动一下Student实体的代码,取消注释:
public int StandardRefID { get; set; }
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EF6 { public class Student { public int StudentId { get; set; } public string StudentName { get; set; } public int StandardRefID { get; set; } //[ForeignKey("StandardRefID")] public virtual Standard Standard { get; set; } } }
然后改动上下文的代码:
using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EF6 { public class DbContextClass:DbContext { public DbContextClass() : base("name=ConnectionString") { Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DbContextClass>()); } public DbSet<Student> Students { get; set; } public DbSet<Standard> Standards { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { //配置一对多的关系 modelBuilder.Entity<Student>().HasRequired(s => s.Standard).WithMany(s => s.Students).HasForeignKey(s=>s.StandardRefID); base.OnModelCreating(modelBuilder); } } }
然后又得到想要的数据库了:
后面将学习多对多的关系》》》
附上系列目录: