前面,我们已经了解了Code-First利用领域类,怎么为我们创建数据库的简单示例。现在我们来学习一下Code-First约定吧。

  • 什么是约定

约定说白了,就是基于一套规矩办事,这里就是基于你定义好的领域类,然后根据默认的规矩来配置概念模型。Code-First约定定义在这个命名空间下面:

System.Data.Entity.ModelConfiguration.Conventions

现在来大致浏览一下都有哪些约定吧:

  • 类型发现

在前面的章节中,我们在上下文类中创建了DbSet属性的类集合,然后Code-First会根据这个DbSet属性为我们创建数据表。

codeFirst会为我们包含任何引用类型到这些类集合中,甚至尽管这写引用类型定义在不同的程序集中。

例如:下面的代码中,Student实体包含了Teacher类的引用,然而上下文类总,却并未包含Teacher的类型的DBSet属性。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EF1
{
   public class Student
    {
        public int StudentId { get; set; }

        public string StudentName { get; set; }

        public DateTime DateOfBirth { get; set; }
        public byte[] Photo { get; set; }
        public decimal Height { get; set; }
        public float Weight { get; set; }

        public Standard Standard { get; set; }

        public Teacher Teacher { get; set; }

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EF1
{
  public  class Teacher
    {
      public int TeacherId { get; set; }

      public string TeacherName { get; set; }
    }
}

数据上下文类中,并没有包含Teacher类型的DbSet属性:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EF1
{
   public class DbContextClass:DbContext
    {
       public DbContextClass()
           : base("ConnectionString")
       { }
       public DbSet<Student> Studnets { get; set; }

       public DbSet<Standard> Standards { get; set; }

    }
}

然后我们运行程序,运行成功之后,打开数据库,发现:

哈哈,是不是报错了?

具体的错误消息是:The model backing the 'DbContextClass' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269)

这是因为,我们改了实体,没有开启数据库迁移特性,不过还有另外一种解决办法,我这里,

我们改一下上下文类:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EF1
{
   public class DbContextClass:DbContext
    {
       public DbContextClass()
           : base("ConnectionString")
       { }
       public DbSet<Student> Studnets { get; set; }

       public DbSet<Standard> Standards { get; set; }

       protected override void OnModelCreating(DbModelBuilder modelBuilder)
       {
           Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DbContextClass>());

           base.OnModelCreating(modelBuilder);
       }

    }
}

这里改动的语句:

Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DbContextClass>());

意思是,当模型发生改变的时候,就删掉重新创建数据库。

现在我们运行程序,发现就能运行成功了。

然后我们看一下数据库:

多了一个Teacher表。

看一下Teacher的表结构:

然后看一下Student表的表结构:

Code-First可以推断类,尽管上下文中并没有包含Teacher的属性。

所以,我们总结一下类型发现的约定:

  • Code-First包含类型定义也就是DbSet属性在上下文类中;Code-First includes types defined as a DbSet property in context class.
  • Code-First包含引用类型在实体类中,尽管他们定义在不同的程序集下;Code-First includes reference types included in entity types even if they are defined in different assembly.
  • Code-First包含派生类,尽管这个类要定义成DbSet属性。(Code-First includes derived classes even if only the base class is defined as DbSet property.)
  • 主键约定

在之前的例子中,我们看见了Code-First为每个表都创建了主键,这个默认的规则就是,类名Id(大小写不敏感,即不区分大小写)。这个主键的类型可以是任何类型,但是如果主键的类型是numeric 或者GUID,它将会被配置成自增列。

然后,如果你定义主键的属性名字不是Id或者不是类名+ID,然后在运行的时候,就会报错。

我们看看下面的代码就知道了:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EF1
{
   public class Student
    {
        public int StdID { get; set; }

        public string StudentName { get; set; }

        public DateTime DateOfBirth { get; set; }
        public byte[] Photo { get; set; }
        public decimal Height { get; set; }
        public float Weight { get; set; }

        public Standard Standard { get; set; }

        public Teacher Teacher { get; set; }

    }
}

改完之后,我们运行程序,就会报错:

具体的错误消息:

One or more validation errors were detected during model generation:

EF1.Student: : EntityType 'Student' has no key defined. Define the key for this EntityType.
Studnets: EntityType: EntitySet 'Studnets' is based on type 'Student' that has no keys defined.

意思是没有定义主键》》》

如果你想要StdID成为主键,可以使用数据注解或者Fluent APIS,我们在后面将会学到。

  • 关系约定

通过导航属性,Code-First能够推断出,两个实体之间的关系,这个导航属性可以是简单的引用类型或者是集合类型,例如,我们在Student实体中定义了Standard导航属性,然后我们在Stanrard实体中定义了Icollection<Student>导航属性,所以,Code-First将会自动为我们创建一对多的关系。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EF1
{
   public class Student
    {
        public int StdID { get; set; }

        public string StudentName { get; set; }

        public DateTime DateOfBirth { get; set; }
        public byte[] Photo { get; set; }
        public decimal Height { get; set; }
        public float Weight { get; set; }

       /// <summary>
       /// 导航属性
       /// </summary>
        public Standard Standard { get; set; }

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EF1
{
   public class Standard
    {
       public int StandardId { get; set; }

       public string StandardName { get; set; }

       /// <summary>
       /// 集合类型的导航属性
       /// </summary>
       public ICollection<Student> Students { get; set; }
    }
}

然后我们运行程序成功之后,看到数据库:

Code-First约定,为我们创建了外键

<navigation property Name>_<primary key property name of navigation property type> e.g. Standard_StandardId.

  • 外键约定

我们已经知道了,当我们添加一个外键属性的时候,Code--First会我们创建一个外键。推荐做法是,包含一个外键属性的依赖关系。

请看下面的代码;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EF1
{
   public class Student
    {
       public int StudentId { get; set; }

        public string StudentName { get; set; }

        public DateTime DateOfBirth { get; set; }
        public byte[] Photo { get; set; }
        public decimal Height { get; set; }
        public float Weight { get; set; }

       /// <summary>
       /// 导航属性
       /// </summary>
        public Standard Standard { get; set; }

       /// <summary>
       /// Foriegn key  for  Standard
       /// </summary>
        public int StandardId { get; set; }

    }
}

这样改动之后,我们在运行程序,发现怎么了?是不是又报错了?

这个是因为,外键表Standard中没有数据。

我们往Standard表中,插入一条数据,然后改一下Main函数里面的代码:再运行程序>>>>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EF1
{
    class Program
    {
        static void Main(string[] args)
        {

            Student stu = , StudentName = , Weight = , DateOfBirth = DateTime.Now,StandardId=1};

            using (var db = new DbContextClass())
            {
                db.Studnets.Add(stu);
                db.SaveChanges();
            }

            Console.WriteLine("添加成功");
            Console.ReadKey();
        }
    }
}

运行之后,是不是没有出错了?

现在让我们再来看一下数据库:

发现什么变化了么?是不是这个外键的名字不再是以外键属性的名字+外键实体的主键ID命名了?

并且你注意到了没有,这个外键属性也是不能为空的?

因为int类型是不能为空的!!

code-Forst可以基于外键属性的【为空性】(nullability )推断出多重性的关系,如果这个属性是可以为空的,然后这个关系可以看成是null,否则的话,就是not null,你可以修改

StandardId属性的类型从int变成Nullable<int>,然后运行程序的话,得到的外键就是可以为空了。

  • 复杂类型约定

Code-First可以为类创建复杂类型,可以不包含主键属性,并且不使用数据注解和Fluent APIs来注册主键

上面这写就是Code-First约定的大概内容了,这些约定可以被打破,通过使用数据注解和Fluent APIs,在EF6中,你可以自定义约定。

附上系列目录:

3.Code-First 约定(EF Code-First系列)的更多相关文章

  1. Entity Framework 6新特性:全局性地自定义Code First约定

    2012年12月11日,Entity Framework已经发布了Entity Framework 6 Alpha2,因项目需要,目前已使用了其中的两个特性,今天就来介绍一下第一个特性:全局性地自定义 ...

  2. Code First 约定

    Code First 约定 借助 Code First,可通过使用 C# 或 Visual Basic .NET 类来描述模型.模型的基本形状可通过约定来检测.约定是规则集,用于在使用 Code Fi ...

  3. EntityFramWork(3 code First 约定)

      Code First 约定 借助 Code First,可通过使用 C# 或 Visual Basic .NET 类来描述模型.模型的基本形状可通过约定来检测.约定是规则集,用于在使用 Code ...

  4. EF Code First学习系列

    EF Model First在实际工作中基本用不到,前段时间学了一下,大概的了解一下.现在开始学习Code First这种方式.这也是在实际工作中用到最多的方式. 下面先给出一些目录: 1.什么是Co ...

  5. 1.什么是Code First(EF Code First 系列)

    EF4.1中开始支持Code First .这种方式在领域设计模式中非常有用.使用Code First模式,你可以专注于领域设计,根据需要,为你一个领域的对象创建类集合,而不是首先来设计数据库,然后来 ...

  6. 16.翻译系列:EF 6 Code -First中使用存储过程【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/entityframework6/code-first-insert-update-delete-stored ...

  7. EF和MVC系列文章导航:EF Code First、DbContext、MVC

    对于之前一直使用webForm服务器控件.手写ado.net操作数据库的同学,突然来了EF和MVC,好多新概念泉涌而出,的确犹如当头一棒不知所措.本系列文章可以帮助新手入门并熟练使用EF和MVC,有了 ...

  8. 【EF】Entity Framework 6新特性:全局性地自定义Code First约定

    应用场景 场景一:EF Code First默认使用类名作为表名,如果我们需要给表名加个前缀,例如将类名Category映射到表Shop_Category.将Product映射到Shop_Produc ...

  9. EF Code First 学习笔记:约定配置 Data Annotations+Fluent API

    要更改EF中的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面;还有一个就 ...

  10. ASP.NET Web API实践系列02,在MVC4下的一个实例, 包含EF Code First,依赖注入, Bootstrap等

    本篇体验在MVC4下,实现一个对Book信息的管理,包括增删查等,用到了EF Code First, 使用Unity进行依赖注入,前端使用Bootstrap美化.先上最终效果: →创建一个MVC4项目 ...

随机推荐

  1. CSS的定位

        定位的基本思想:允许你定义元素框相对于其正常位置应该出现的位置,或者相对于父元素.另一个元素甚至浏览器窗口本身的位置        一切皆为框   div.h1 或 p 元素常常被称为块级元素 ...

  2. ASP.NET Core 优雅的在开发环境保存机密(User Secrets)

    前言 在应用程序开发的过程中,有的时候需要在代码中保存一些机密的信息,比如加密密钥,字符串,或者是用户名密码等.通常的做法是保存到一个配置文件中,在以前我们会把他保存到web.config中,但是在A ...

  3. Microsoft开源跨平台的序列化库——Bond

    上个月Microsoft开源了Bond,一个跨平台的模式化数据处理框架.Bond支持跨语言的序列化/反序列化,支持强大的泛型机制能够对数据进行有效地处理.该框架在Microsoft公司内部的高扩展服务 ...

  4. Lesson 6 Percy Buttons

    Text I have just moved to a house in Bridge Street. Yesterday a bagger knocked at my door. He asked ...

  5. [.net 面向对象程序设计进阶] (21) 反射(Reflection)(下)设计模式中利用反射解耦

    [.net 面向对象程序设计进阶] (21) 反射(Reflection)(下)设计模式中利用反射解耦 本节导读:上篇文章简单介绍了.NET面向对象中一个重要的技术反射的基本应用,它可以让我们动态的调 ...

  6. ASP.NET MVC 从零开始 - 请求处理

    这篇文章是从我的 github 博客 lxconan.github.io 导入的. 这是这个系列的第三篇了.前两篇文章请参见: ASP.NET MVC 从零开始 - Create and Run AS ...

  7. AngularJS2 + ASP.NET MVC项目

    环境:VS2015, NodeJS:v 6.5, npm: v3.10, AngularJs 2 通过将ASP.NET MVC项目与Angualr 2官网上的quick start整合的过程中遇到些问 ...

  8. Jmeter 使用Jmeter与Badboy进行压力测试

    1. 介绍 Badboy是一个录制请求的工具,这里用它来生成文件给JMeter用. JMeter是一个用java写的开源的性能测试工具,用于模拟在服务器.网络或者其他对象上附加高负载以测试他们提供服务 ...

  9. 决策树和基于决策树的集成方法(DT,RF,GBDT,XGBT)复习总结

    摘要: 1.算法概述 2.算法推导 3.算法特性及优缺点 4.注意事项 5.实现和具体例子 内容: 1.算法概述 1.1 决策树(DT)是一种基本的分类和回归方法.在分类问题中它可以认为是if-the ...

  10. iOS-C基础

    iOS开发系列--C语言之基础知识 概览 当前移动开发的趋势已经势不可挡,这个系列希望浅谈一下个人对IOS开发的一些见解,这个IOS系列计划从几个角度去说IOS开发: C语言 OC基础 IOS开发(i ...