我们初始化数据库一节已经知道:EF为每一个具体的类生成了数据库的表.现在有了一个问题:我们在设计领域类时经常用到继承,这能让我们的代码更简洁且容易管理,在面向对象中有“has a”和“is a”关系(如student has a name,student is a person--继承),然而数据库中只有“has a”关系.数据库管理系统并不支持继承,所以我们怎么去映射具有继承关系的领域类呢? EF CodeFirst中有三种方式表示继承体系: 1.TPH(table per hierarch…
现在假想,我们想要为讴歌学校创建一个应用程序,这个程序需要能够来添加或者更新学生,分数,教师还有课程信息. 代替之前我们的做法:先是创建数据库,现在我们不这么做,我们先来创建领域类,首先我来创建两个简单的类,一个是Student类,一个是Standard类. 每个学生都有一个分数,下面看代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Thr…
FluentApi总结 1.FluentApi简介 EF中的FluentApi作用是通过配置领域类来覆盖默认的约定.在EF中,我们通过DbModelBuilder类来使用FluentApi,它的功能比数据注释属性更强大. 使用FluentApi时,我们在context类的OnModelCreating()方法中重写配置项,一个栗子: public class SchoolContext: DbContext { public DbSet<Student> Students { get; set…
我们已经知道了在OnModelCreating()方法中可以通过FluentApi对所有的实体类进行配置,然而当实体类很多时,我们把所有的配置都放在OnModelCreating()方法中很难维护.EF6允许我们给每一个实体添加一个单独的配置类,通过这个配置类来对相应的实体进行配置. 以配置Student实体类为例,我们在OnModelCreating()方法中配置Student实体,代码如下: public class SchoolDBContext: DbContext { public S…
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/relationship-in-entity-framework-using-code-first-approach-w/ In this article, you will learn about relationships in Entity Framework using the Code First Approach with Fluent API. 在这篇文章中,你将会学习到使用EF…
EF Code-First提供了一系列的数据注解的特性,你可以将其应用到你的领域类和属性中,数据注解属性重写了EF默认的约定. System.ComponentModel.DataAnnotations includes attributes that impacts on nullability or size of the column. [这个命名空间下,包含影响数据表列的大小和可空性的特性.] System.ComponentModel.DataAnnotations.Schema na…