笔记: EF出现列名 'Discriminator' 无效: 类没有加入数据库上下文也被数据迁移生成表: 出现该问题一般是使用了某个基类继承了实体类: 原因是code first的POCO实体对象的继承问题,EF会把项目中在DbContext中引用的所有的Model类及这些Model类对应的子类都生成对应映射视图.如果数据库没有对应表或字段就会报错. 解决办法:只需要在类前面加上[NotMapped],表示应从数据库映射中排除属性或类. [NotMapped] public class Comm
文章演示使用EF自动创建数据库第一个步骤创建实体类. 一.创建表映射实体类 using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Data.Common; using System.Data.Entity; using System.Li
使用EF code first编写类继承的时候,出现列名 'Discriminator' 无效 . 字段中没有列名 'Discriminator' 原来在代码中,定义了一个类A,类B,B继承A,但是数据库中不存在B表,项目中只是用于ViewModel的定义而已. 此时,需要做的是,解除B表的映射. [NotMapped] public class B : A { //to do something }
创建好表实体类后,接着就是创建数据库上下文(继承DbContext)并将实体类添加进来. 代码示例如下: using DBClientEntity; using System; using System.Collections.Generic; using System.Data.Entity; using System.Data.Entity.Core.Objects; using System.Data.Entity.Infrastructure; using System.Data.Ent
这次的Demo如标题所示, 首先第一步EF创建数据库 创建两个类,一个是图书类,一个是图书类别的类 using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; namespace WebApplication2.DAL { public class Book { [Key] public int B
本章节讲解EF里的继承映射关系,分为TPH.TPT.TPC.具体: 1.TPH:Table Per Hierarchy 这是EF的默认的继承映射关系:一张表存放基类和子类的所有列,自动生成的discriminator列用来区分基类和子类的数据.新建一个度假村Resort实体类试试: /// <summary> /// 度假村类 /// </summary> public class Resort : Lodging //这里继承了Lodging类 { public string E
I.EF里的默认映射 上篇文章演示的通过定义实体类就可以自动生成数据库,并且EF自动设置了数据库的主键.外键以及表名和字段的类型等,这就是EF里的默认映射.具体分为: 数据库映射:Code First 默认会在本地的SQL Expression数据库中建立一个和DbContext的子类的全名相同的数据库,全名指的是命名空间加上类名: 表映射:Code First 默认会按照类型名复数建立数据表,比如说Destination类对应的表名就叫Destinations: 列映射:Code First