Entity Framework Code First (五)Fluent API - 配置关系 转载 https://www.cnblogs.com/panchunting/p/entity-framework-code-first-fluent-api-configuring-relationships.html
上一篇文章我们讲解了如何用 Fluent API 来配置/映射属性和类型,本文将把重点放在其是如何配置关系的。
文中所使用代码如下

public class Student
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime EnrollmentDate { get; set; } // Navigation properties
public virtual Address Address { get; set; }
public virtual OtherInfo OtherInfo { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; }
} public class Department
{
public Department()
{
this.Courses = new HashSet<Course>();
}
// Primary key
public int DepartmentID { get; set; }
public string Name { get; set; }
public decimal Budget { get; set; }
public System.DateTime StartDate { get; set; }
public int? Administrator { get; set; } // Navigation property
public virtual ICollection<Course> Courses { get; private set; }
} public class Course
{
public int CourseID { get; set; }
public string Title { get; set; }
public int Credits { get; set; } // Foreign key
public int DepartmentID { get; set; }
public string DepartmentName { get; set; } public int SomeDepartmentID { get; set; } // Navigation properties
public virtual Department Department { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; }
public virtual ICollection<Instructor> Instructors { get; set; }
} public class Instructor
{
public int InstructorID { get; set; }
public string Name { get; set; }
public DateTime HireDate { get; set; } // Navigation properties
public virtual ICollection<Course> Courses { get; set; }
} public class Enrollment
{
public int EnrollmentID { get; set; }
public int CourseID { get; set; }
public int StudentID { get; set; } // Navigation property
public virtual Course Course { get; set; }
public virtual Student Student { get; set; }
} public class Address
{
public int AddressId { get; set; }
public string HomeAddress { get; set; }
public string LiveAddress { get; set; } // Navigation property
public virtual Student Student { get; set; }
} public class OtherInfo
{
public int Id { get; set; }
public string HomeAddress { get; set; }
public string MailAddress { get; set; }
public string PhoneNumber { get; set; }
public string StudentID { get; set; } // Navigation property
public virtual Student Student { get; set; }
}

EntityTypeConfiguration<TEntityType>

上面是泛型类的部分方法截图,一般我们通过 Code First Fluent API 来配置实体间的关系时都是从此泛型类的实例开始,此泛型类为我们提供了大部分的关系处理方法,如必须的 HasRequired ,可选的 HasOptional ,多个的 HasMany .
上面所有方法(除了 HasMany, HasOptional, HasRequired )的返回值都是泛型类 EntityTypeConfiguration<TEntityType> 的实例本身,这给链式操作提供了极大地方便.
HasRequired, HasOptional, HasMany 这三个方法的参数都是一个 lambda expression, 只不过前两个用于表示实体关系(Relationship)间的导航属性(navigation property),后一个则是导航属性的集合(Collection)
HasRequired, HasOptional, HasMany 这三个方法的返回值都是可以继续进行配置的泛型类实例,虽然名称不同,方法名也略有不同,但方法主体所体现的思想是一致的
RequiredNavigationPropertyConfiguration<TEntityType, TTargetEntityType>

OptionalNavigationPropertyConfiguration<TEntityType, TTargetEntityType>

ManyNavigationPropertyConfiguration<TEntityType, TTargetEntityType>

三个泛型类都有类似于如下三个方法 WithRequired, WithOptional, WithMany, 这三个方法都提供了重载的无参版本,有参方法的参数也都是一个 lambda expression, 前两个用于表示实体关系(Relationship)间的导航属性(navigation property),后一个则是导航属性的集合(Collection)
接下来,你可以使用方法 HasForeignKey 继续配置外键属性,方法的参数仍然是一个 lambda expression, 只不过代表一个属性
DependentNavigationPropertyConfiguration<TDependentEntityType>
public CascadableNavigationPropertyConfiguration HasForeignKey<TKey>(Expression<Func<TDependentEntityType, TKey>> foreignKeyExpression);
其它两个类主要包含方法 Map ,如下
ForeignKeyNavigationPropertyConfiguration
public CascadableNavigationPropertyConfiguration Map(Action<ForeignKeyAssociationMappingConfiguration> configurationAction);
ManyToManyNavigationPropertyConfiguration<TEntityType, TTargetEntityType>
public ManyToManyNavigationPropertyConfiguration<TEntityType, TTargetEntityType> Map(Action<ManyToManyAssociationMappingConfiguration> configurationAction);
public ManyToManyNavigationPropertyConfiguration<TEntityType, TTargetEntityType> MapToStoredProcedures();
public ManyToManyNavigationPropertyConfiguration<TEntityType, TTargetEntityType> MapToStoredProcedures(Action<ManyToManyModificationStoredProceduresConfiguration<TEntityType, TTargetEntityType>> modificationStoredProcedureMappingConfigurationAction);
我们可以继续在返回的实例上进行配置
CascadableNavigationPropertyConfiguration
public void WillCascadeOnDelete();
public void WillCascadeOnDelete(bool value);
我们看到了级联删除
Configuring Relationships
1:1,0 - Configuring a Required-to-Optional Relationship (One-to–Zero-or-One)
一个学生可以有一个或没有其它信息(包括邮件地址、手机号等)
// Map one-to-zero or one relationship
modelBuilder.Entity<OtherInfo>()
.HasRequired(t => t.Student)
.WithOptional(t => t.OtherInfo);
1:1 - Configuring a Relationship Where Both Ends Are Required (One-to-One)
一个学生肯定有一个地址信息(包含家庭住址、居住地址)
// Map one-to-one relationship
modelBuilder.Entity<Address>()
.HasRequired(t => t.Student)
.WithRequiredPrincipal(t => t.Address);
1:N - Configuring a Required-to-Many Relationship (One-to-Many)
一个学生可以参加多门课程
// Map one-to-many relationship
modelBuilder.Entity<Student>()
.HasMany(t => t.Enrollments)
.WithRequired(t => t.Student);
N:N - Configuring a Many-to-Many Relationship (Many-to-Many)
一个老师可以教授多门课程,一门课程也可以由多名老师教授
// Map one-to-many relationship
modelBuilder.Entity<Course>()
.HasMany(t => t.Instructors)
.WithMany(t => t.Courses);
你还可以进一步指定中间连接表(数据库将会创建中间连接表)

// Map one-to-many relationship
modelBuilder.Entity<Course>()
.HasMany(t => t.Instructors)
.WithMany(t => t.Courses)
.Map(m =>
{
m.ToTable("CourseInstructor");
m.MapLeftKey("CourseID");
m.MapRightKey("InstructorID");
});

Configuring a Relationship with One Navigation Property - 基于导航属性配置关系
如果关系是单向的,即两个实体间只在一个实体上定义导航属性。Code First Conventions 能够自动推断这种关系为 one-to-many .
例如你想在 Student 和 Address 两个实体间建立 one-to-one 关系,而且只在 Address 实体上包含导航属性,此时你就需要用 Code First Fluent API 配置这种关系
// Map one-to-one relationship
modelBuilder.Entity<Address>()
.HasRequired(t => t.Student)
.WithRequiredPrincipal();
WillCascadeOnDelete - Enabling Cascade Delete (级联删除)
你可以使用 WillCascadeOnDelete 来级联删除关系,如果从属主体上的外键是 not nullable, 那么 Code First 将设置级联删除,否则将不会设置级联删除,而只是仅仅把外键设置成 null
在 Code First Conventions 下是这样移除级联删除的
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>()
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>()
Code First Fluent API 的方式如下
// Cascade Delete
modelBuilder.Entity<Course>()
.HasRequired(t => t.Department)
.WithMany(t => t.Courses)
.HasForeignKey(d => d.DepartmentID)
.WillCascadeOnDelete(false);
Configuring a Composite Foreign Key - 配置组合外键
如果设置 Department 的主键为组合主键 DepartmentID, Name,则可以通过 Fluent API 为 Course 指定组合外键

// Composite primary key
modelBuilder.Entity<Department>()
.HasKey(d => new { d.DepartmentID, d.Name }); // Composite foreign key
modelBuilder.Entity<Course>()
.HasRequired(c => c.Department)
.WithMany(d => d.Courses)
.HasForeignKey(d => new { d.DepartmentID, d.DepartmentName });

Renaming a Foreign Key That Is Not Defined in the Model - 重命名外键
可以重命名外键名
// Renaming a Foreign Key That Is Not Defined in the Model
modelBuilder.Entity<Course>()
.HasRequired(c => c.Department)
.WithMany(t => t.Courses)
.Map(m => m.MapKey("ChangedDepartmentID"));
Configuring a Foreign Key Name That Does Not Follow the Code First Convention
如果从属实体上的外键属性名不符合 Code First Conventions 的规范,意即从属实体上没有外键,你可以通过如下方式来指定
// Configuring a Foreign Key Name That Does Not Follow the Code First Convention
modelBuilder.Entity<Course>()
.HasRequired(c => c.Department)
.WithMany(d => d.Courses)
.HasForeignKey(c => c.SomeDepartmentID);
原文链接:http://msdn.microsoft.com/en-us/data/jj591620
Entity Framework Code First (五)Fluent API - 配置关系 转载 https://www.cnblogs.com/panchunting/p/entity-framework-code-first-fluent-api-configuring-relationships.html的更多相关文章
- Entity Framework Code First (五)Fluent API - 配置关系
上一篇文章我们讲解了如何用 Fluent API 来配置/映射属性和类型,本文将把重点放在其是如何配置关系的. 文中所使用代码如下 public class Student { public int ...
- Entity Framework Code First属性映射约定 转载https://www.cnblogs.com/libingql/p/3352058.html
Entity Framework Code First属性映射约定 Entity Framework Code First与数据表之间的映射方式有两种实现:Data Annotation和Flue ...
- Entity Framework Code First数据库连接 转载 https://www.cnblogs.com/libingql/p/3351275.html
Entity Framework Code First数据库连接 1. 安装Entity Framework 使用NuGet安装Entity Framework程序包:工具->库程序包管理器 ...
- WebApi系列~基于RESTful标准的Web Api 转载 https://www.cnblogs.com/lori/p/3555737.html
微软的web api是在vs2012上的mvc4项目绑定发行的,它提出的web api是完全基于RESTful标准的,完全不同于之前的(同是SOAP协议的)wcf和webService,它是简单,代码 ...
- Activity详解一 配置、启动和关闭activity转载 https://www.cnblogs.com/androidWuYou/p/5887726.html
先看效果图: Android为我们提供了四种应组件,分别为Activity.Service.Broadcast receivers和Content providers,这些组建也就是我们开发一个And ...
- Entity Framework 实体框架的形成之旅--Code First模式中使用 Fluent API 配置(6)
在前面的随笔<Entity Framework 实体框架的形成之旅--Code First的框架设计(5)>里介绍了基于Code First模式的实体框架的经验,这种方式自动处理出来的模式 ...
- Entity Framework Code First (四)Fluent API - 配置属性/类型
上篇博文说过当我们定义的类不能遵循约定(Conventions)的时候,Code First 提供了两种方式来配置你的类:DataAnnotations 和 Fluent API, 本文将关注 Flu ...
- Code First约定-Fluent API配置
转自:http://blog.163.com/m13864039250_1/blog/static/2138652482015283397609/ 用Fluent API 配置/映射属性和类型 简介 ...
- Entity FrameWork Code First 配置关系
Has方法与With方法 A.HasRequired(a => a.B).WithOptional(b => b.A);上面一句配置意思就是A类包含B类一个不为null的实例,B类包含A类 ...
随机推荐
- C++多态下的访问修饰符
C++多态下的访问修饰符 先上代码: class Parent { public: virtual void showMsg() { cout << "Parent showMs ...
- Viewer.js – 强大的JS/jQuery图片查看器
简介 Viewer.js 是一款强大的图片查看器,像门户网站一般都会有各自的图片查看器,如果您正需要一款强大的图片查看器,也许 Viewer.js 是一个很好的选择.Viewer.js 有以下特点: ...
- 第一个chrome extension
如今,chrome浏览器的使用如越来越流行,chrome extension往往能提供更多很丰富的功能.以前一直想了解这方面的东西,可是又担心很复杂.前段时间,在斗鱼看一个直播,想刷弹幕,但是每次自己 ...
- JSP2的自定义标签和方法
Jsp2的自定义标签 Jsp2 开发标签库的几个步骤: 开发自定义标签处理类. 建立一个*.tld文件,每个tld文件对应一个标签库,每个标签库可对应多个标签. 在jsp文件中使用自定义标签 空标签 ...
- this与super的区别
调用super()的语句必须要写在子类构造方法的第一行. super()是在子类中调用父类的构造方法:this()是在同一类中调用其它方法. super()和this()都需要放在构造函数的第一行. ...
- 安装kafka多节点
安装多节点的kafka只需要创建多份配置文件(server.properties),然后指定他们启动kafka服务即可,本例中采用一台服务器来模拟3个节点的kafka集群搭建.同理,使用一台服务器搭建 ...
- 【容器化】容器技术实践.pdf_视频学习笔记
容器运行时 docker rkt gvisor containerd 容器编排系统:kubernetes (简称k8s)
- 理解Promise (3)
在promise 的then 中我们不仅有 成功状态 失败状态,可能还有等待状态,所以我们要对等待状态进行处理 function Promise(executor) { let self = th ...
- MySQL重复数据中限定操作n条
对于一个表,有时可能里面有很多重复的条,比如: +-----------+---------+| coupon_id | user_id |+-----------+---------+| 8 | 1 ...
- git & gerrit & shell
g公司使用Gerrit改善评审流程. 比较麻烦.gerrit提交后会触发vertifyCI, 实施代码扫描. 这一堆过程, 打印出一堆信息, 都在log中, 所以处理log就需要自己写shell了. ...