在codefirst一中也说了Mapping是实体与数据库的纽带,model通过Mapping映射到数据库,我们可以从数据库的角度来分析?首先是映射到数据库,这个是必须的。数据库里面一般包括表、列、约束、主外键、级联操作、实体关系(E-R图)、存储过程、视图、锁、事务、数据库结构更新等。在接下来的日子里,通过数据库的这些名词,来学习C#是如何实现或者调用它们来通过代码操作数据库。

在code first一中主要是学习了对数据库的映射,今天主要是学习表的映射。一个model映射一个表,多个modle映射一个表,一个model映射多个表,子类映射表。

一、表名的映射

学习model与表的映射肯定要知道表名的映射,可以通过using System.ComponentModel.DataAnnotations.Schema;命名空间下的[Table("TestStudent")]特性来约定表名的映射。

二、一个model映射一个表

这个就不用多说,一般情况都是一个model映射一个表,在code first一中也有Student类映射到Student表。

三、子类映射表

面向对象的三大特征之一就是继承,model中也会有继承实体,那子类如何映射到表中呢?

首先定义了一个Person类,一个继承Person的Student类。

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace EFCodeFirstModels
{
public enum SexType { Male, Female } public class Person
{
[Key]
public string PersonId { get; set; }
//姓名
public string Name { get; set; }
//性别
public SexType Sex { get; set; }
//年龄
public int Age { get; set; } public Person(string personId, string name, SexType sex, int age)
{
PersonId = personId;
Name = name;
Sex = sex;
Age = age;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace EFCodeFirstModels
{
public class Student : Person
{
public string StuId { get; set; } public string Major { get; set; } public string School { get; set; } public Student(string stuId, string major, string school, string personId, string name, SexType sex, int age) : base(personId, name, sex, age)
{
this.StuId = stuId;
this.Major = major;
this.School = major;
this.PersonId = personId;
this.Name = name;
this.Sex = sex;
this.Age = age;
}
} }

数据库上下文还是一默认的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using EFCodeFirstModels;
using System.Configuration; namespace EFCodeFirstDataAccess
{
public class EFCodeFirstDbContext:DbContext
{ public EFCodeFirstDbContext() : base("MyStrConn")
{
}
public DbSet<Person> Persons { get; set; } }
}

在Main中增加一个Person对象,一个Student对象。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EFCodeFirstModels;
using EFCodeFirstDataAccess; namespace EFCodeFirstDemo
{
class Program
{
static void Main(string[] args)
{
//using能及时释放资源,例如数据库连接异常,可以即使将上下文释放
using (var db=new EFCodeFirstDbContext())
{
Person person = new Person("", "张三", SexType.Female, );
db.Persons.Add(person);
Student stu = new Student("", "软件工程", "蓝翔", "", "CYW", SexType.Female, );
db.Persons.Add(stu);
db.SaveChanges();
Console.WriteLine("Success");
}
Console.ReadLine(); }
}
}

运行之后可以看到数据库生成了一个People表,多了一列:Discriminator,用来区别子类和父类对象

如果你想改名Discriminator列名,可以使用Fluent API来设置,需要在数据库上下文中重写OnModelCreating()方法。

public class EFCodeFirstDbContext:DbContext
{ public EFCodeFirstDbContext() : base("MyStrConn")
{
}
public DbSet<Person> Persons { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Person>().Map(m =>
{
m.ToTable("Person");
m.Requires("PersonType").HasValue("Person");
}).Map<Student>(m =>
{
m.Requires("PersonType").HasValue("Student");
}); }
}

如果Person只有一个派生类,那我们可以使用布尔值来区别

            modelBuilder.Entity<Person>().Map(m =>
{
m.ToTable("Person");
m.Requires("IsStudent").HasValue(false);
}).Map<Student>(m =>
{
m.Requires("IsStudent").HasValue(true);
});

上面是两个Model生成一个表,基类和派生类都映射到同一张表中,通过使用鉴别列来识别是否为子类型。这是Code First默认规则使用的表映射方法TPH(Table Per Hierarchy)。其实还有两种映射方法。一种是TPT,一种是TPC.

TPT:Table Per Type,TPH将所有层次的类都放在了一个表里,而TPT在一个单独的表中储存来自基类的属性,在派生类定义的附加属性储存在另一个表里,并使用外键与主表相连接。这种方式只需指定子类表名。

 [Table("Student")]

TPC:Table Per Concrete Type,基类与派生类都映射在不同的表中,不同的是派生类中还包括了基类的字段。TPC只能用Fluent API来配置

            modelBuilder.Entity<Person>().Map(m =>
{
m.ToTable("Person");
}).Map<Student>(m =>
{
m.ToTable("Student");
m.MapInheritedProperties();
});

四、多个Model映射一个表

上面虽然是两个类Person、Student映射到一个表中,但主键都在基类上定义的,映射一个表还比较容易。如果是两个没有继承关系的Model如何映射到同一张表呢?这个我们在上面Person类Student类的基础上增加了一个IDCard类,主要是表示身份证类。假设Person的PersonId就是IDCard的PersonId。两个Model映射一个表,那两个Molde肯定是一对一的关系,映射的表名相同,主键名和类型也要相同,这些是必须的。同时还要指明两个类的外键,只指明一个都不行。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema; namespace EFCodeFirstModels
{
[Table("Person")]
public class IDCard
{
//身份证号
[Key,ForeignKey("People")]
public string PersonId { get; set; } //法定出生日期
public DateTime BirthDate { get; set; } public Person People { get; set; } public IDCard(string personId, DateTime birstDate)
{
PersonId = personId;
BirthDate = birstDate;
} }
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace EFCodeFirstModels
{
public enum SexType { Male, Female } [Table("Person")]
public class Person
{
[Key, ForeignKey("Card")]
public string PersonId { get; set; }
//姓名
public string Name { get; set; }
//性别
public SexType Sex { get; set; }
//年龄
public int Age { get; set; } public IDCard Card { get; set; } public Person(string personId, string name, SexType sex, int age, IDCard card)
{
PersonId = personId;
Name = name;
Sex = sex;
Age = age;
Card = card;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace EFCodeFirstModels
{
public class Student : Person
{
public string StuId { get; set; } public string Major { get; set; } public string School { get; set; } public Student(string stuId, string major, string school, string personId, string name, SexType sex, int age, IDCard card) : base(personId, name, sex, age, card)
{
this.StuId = stuId;
this.Major = major;
this.School = major;
this.PersonId = personId;
this.Name = name;
this.Sex = sex;
this.Age = age;
this.Card = card;
}
} }

在Mian中先添加一个Person来看下运行结果。

            //using能及时释放资源,例如数据库连接异常,可以即使将上下文释放
using (var db=new EFCodeFirstDbContext())
{ IDCard card = new IDCard("", DateTime.Now);
Person person = new Person("", "张三", SexType.Female, , card);
db.Persons.Add(person);
db.SaveChanges();
Console.WriteLine("Success");
}
Console.ReadLine(); }

从上面的截图看到Person表中也有Student的属性,感觉真是不可思议,amazing.

我们添加一个Person一个Student来看下效果

        static void Main(string[] args)
{
//using能及时释放资源,例如数据库连接异常,可以即使将上下文释放
using (var db=new EFCodeFirstDbContext())
{ IDCard card = new IDCard("", DateTime.Now);
Person person = new Person("", "张三", SexType.Female, , card);
db.Persons.Add(person);
IDCard card2 = new IDCard("", DateTime.Now);
Student stu = new Student("", "挖掘机", "蓝翔", "", "赵铁蛋", SexType.Male, , card2);
db.Persons.Add(stu);
db.SaveChanges();
Console.WriteLine("Success");
}
Console.ReadLine(); }

IDCard和Person生成了同一表,我们可以像上面的Fluent API来改变Discriminator列名。也可以使用TPT的方式生成两个表,只需在Student类上指明映射的表名。

但是TPC就不行了。就会报错,这可能也是只新增Person的时候Student中的属性也会自动添加上的原因吧。

五、一个Model映射多个表

一个Model映射多个表和上面多个Model映射一个表正好反过来,还用上面Student和Person举例。

            modelBuilder.Entity<Student>().Map(m=> {
m.ToTable("Person");
m.Properties(p => p.PersonId);
m.Properties(p=>p.Name);
m.Properties(p=>p.Age);
m.Properties(p => p.Sex); }).Map(m=> {
m.ToTable("Student");
m.Properties(p => p.PersonId);
m.Properties(p=>p.StuId);
m.Properties(p => p.Major);
m.Properties(p => p.School); });

上面将一个Student类映射给两个表一个Student一个person.

            //using能及时释放资源,例如数据库连接异常,可以即使将上下文释放
using (var db=new EFCodeFirstDbContext())
{ Person person = new Person("", "张三", SexType.Female, );
db.Persons.Add(person); Student stu = new Student("", "挖掘机", "蓝翔", "", "赵铁蛋", SexType.Male, );
db.Persons.Add(stu);
db.SaveChanges();
Console.WriteLine("Success");
}

我们上面添加了一个Person类对象,一个Student类对象,这是Student对象会添加到两个表中。

EF实体框架之CodeFirst二的更多相关文章

  1. EF实体框架之CodeFirst四

    在EF实体框架之CodeFirst二中也提到数据库里面一般包括表.列.约束.主外键.级联操作.实体关系(E-R图).存储过程.视图.锁.事务.数据库结构更新等.前面几篇博客把表.存储过程.视图这些算是 ...

  2. EF实体框架之CodeFirst一

    对于SQL Server.MySql.Oracle等这些传统的数据库,基本都是关系型数据库,都是体现实体与实体之间的联系,在以前开发时,可能先根据需求设计数据库,然后在写Model和业务逻辑,对于Mo ...

  3. EF实体框架之CodeFirst五

    上一博客学习了下基本的约定配置,留下几个遗漏的,这篇就是学习下遗漏一复杂类型. 一.什么是复杂类型? 书中说道:“复杂类型也可视作值类型(?)可以作为附加属性添加到其他类.复杂类型与实体类型的区别在于 ...

  4. EF实体框架之CodeFirst七

    前面的6篇博客基本把Code First学习的差不多了,今天这篇学习下code first中的并发控制和事务,基本也快学完了,顶多就差数据迁移. 在数据库中也是有锁和事务的概念,在C#中也是存在,当然 ...

  5. EF实体框架之CodeFirst六

    上午的时候把复杂类型学习了一下,想着趁着周六日把Code First学习完,所以下午还是把Code First中的关系学习下.在数据库中最重要的恐怕就是E-R图了,E-R体现了表与表直接的关系.使用C ...

  6. EF实体框架之CodeFirst三

    前两篇博客学习了数据库映射和表映射,今天学习下数据库初始化.种子数据.EF执行sql以及执行存储过程这几个知识. 一.数据库初始化策略 数据库初始化有4种策略 策略一:数据库不存在时重新创建数据库 D ...

  7. EF实体框架之CodeFirst八

    前面七篇基本把Code First学习了一下,不过code first中会出现一个问题,就是数据迁移的问题. 一.数据准备 还是在前面的demo上修改,这次使用Province和City类. publ ...

  8. 【MVC 1】MVC+EF实体框架—原理解析

    导读:在之前,我们学过了三层框架,即:UI.BLL.DAL.我们将页面显示.逻辑处理和数据访问进行分层,避免了一层.两层的混乱.而后,我们又在经典三层的基础上,应用设计模式:外观.抽象工厂+反射,使得 ...

  9. 【EF 1】EF实体框架 原理+实例

    一.知识回顾 到目前为止,自己学到的链接数据库操作已经经历了几个阶段,分别是:学生信息管理和(第一次)机房收费时的直接连接数据库操作表格,然后是机房个人重构中应用的操作实体,在其中还利用了一个很重要的 ...

随机推荐

  1. php多线程pthreads的安装与使用

    安装Pthreads 基本上需要重新编译PHP,加上 --enable-maintainer-zts 参数,但是用这个文档很少:bug会很多很有很多意想不到的问题,生成环境上只能呵呵了,所以这个东西玩 ...

  2. java代码调用oracle存储过程

    一.简介 与调用mysql存储过程类型过程都是如下 1.创建连接 Connection conn = DriverManager.getConnection(url, user, password); ...

  3. internet connection sharing has been disabled by the network administrator

    Start > Run > gpedit.msc Locate; Computer Configuration/Administrative Templates/Network/Netwo ...

  4. 第二篇 :微信公众平台开发实战Java版之开启开发者模式,接入微信公众平台开发

    第一部分:微信公众号对接的基本介绍 一.填写服务器配置信息的介绍 登录微信公众平台官网后,进入到公众平台后台管理页面. 选择 公众号基本设置->基本配置 ,点击“修改配置”按钮,填写服务器地址( ...

  5. 设计模式C#实现(九)——工厂方法模式和简单工厂

    工厂方法模式:定义一个用于创建对象的接口,让子类决定实例化哪一个类.Factory Method使一个类的实例化延迟到其子类. 构成: 1.Product工厂方法创建的对象的接口 2.Concrete ...

  6. USACO section1.2 Miking cows

    /* ID: vincent63 LANG: C TASK: milk2 */ #include <stdio.h> #include<stdlib.h> #include&l ...

  7. POJ 1151 / HDU 1542 Atlantis 线段树求矩形面积并

    题意:给出矩形两对角点坐标,求矩形面积并. 解法:线段树+离散化. 每加入一个矩形,将两个y值加入yy数组以待离散化,将左边界cover值置为1,右边界置为2,离散后建立的线段树其实是以y值建的树,线 ...

  8. MySQL数据库学习笔记(四)----MySQL聚合函数、控制流程函数(含navicat软件的介绍)

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  9. Java学习----Java概述

    一.常用DOS命令 d:      盘符切换 dir(directory)  列出当前目录下的文件以及文件夹 md (make directory)  创建目录 rd (remove director ...

  10. 在WPF控件上添加Windows窗口式调整大小行为

    起因 项目上需要对Canvas中的控件添加调整大小功能,即能在控件的四个角和四条边上可进行相应的拖动,类似Windows窗口那种.于是在参考以前同事写的代码基础上,完成了该功能. 代码实现 Adorn ...