环境:

EF core 2.0

Net core 2.0

错误:

因实体定义了多个key,打开数据库时程序报以下错误

An unhandled exception occurred while processing the request.

InvalidOperationException: Entity type '***' has composite primary key defined with data annotations. To set composite primary key, use fluent API.

具体的意思是无效的操作异常:实体(****)使用"data annotations"的方式已经定义了复合key。设置复合key,请使用“fluent API”方式。

解决方式:

官方文档 http://msdn.microsoft.com/en-us/data/JJ591617.aspx#1.2

There are two main ways you can configure EF to use something other than conventions, namely  annotations or EFs fluent API. The annotations only cover a subset of the fluent API functionality, so there are mapping scenarios that cannot be achieved using annotations. This article is designed to demonstrate how to use the fluent API to configure properties.

The code first fluent API is most commonly accessed by overriding the  OnModelCreating method on your derived  DbContext. The following samples are designed to show how to do various tasks with the fluent api and allow you to copy the code out and customize it to suit your model, if you wish to see the model that they can be used with as-is then it is provided at the end of this article.

在code first 中的DbContext 自己的实现类中,重载方法“OnModelCreating”,在方法体中添加如“modelBuilder.Entity<Department>().HasKey(t => new { t.DepartmentID, t.Name });

        protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Department>().HasKey(t => new { t.DepartmentID, t.Name });
base.OnModelCreating(modelBuilder);
}

参考文献:

http://msdn.microsoft.com/en-us/data/JJ591617.aspx#1.2

EF code first,set composite primary key 复合key问题的更多相关文章

  1. EF Code First一对一、一对多、多对多关联关系配置

    1.EF Code First一对一关联关系 项目结构图: 实体类: Account.cs using System; using System.Collections.Generic; using ...

  2. Inheritance with EF Code First: Part 3 – Table per Concrete Type (TPC)

    Inheritance with EF Code First: Part 3 – Table per Concrete Type (TPC) This is the third (and last) ...

  3. Inheritance with EF Code First: Part 2 – Table per Type (TPT)

    In the previous blog post you saw that there are three different approaches to representing an inher ...

  4. Inheritance with EF Code First: Part 1 – Table per Hierarchy (TPH)

    以下三篇文章是Entity Framework Code-First系列中第七回:Entity Framework Code-First(7):Inheritance Strategy 提到的三篇.这 ...

  5. EF Code First学习系列

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

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

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

  7. IoC容器Autofac - Autofac + Asp.net MVC + EF Code First(转载)

    转载地址:http://www.cnblogs.com/JustRun1983/archive/2013/03/28/2981645.html  有修改 Autofac通过Controller默认构造 ...

  8. MVC项目实践,在三层架构下实现SportsStore-01,EF Code First建模、DAL层等

    SportsStore是<精通ASP.NET MVC3框架(第三版)>中演示的MVC项目,在该项目中涵盖了MVC的众多方面,包括:使用DI容器.URL优化.导航.分页.购物车.订单.产品管 ...

  9. EF Code First DataAnnotations

    Key EF框架要求每个实体必须有主键字段,他需要根据这个主键字段跟踪实体.CodeFirst方法在创建实体时,也必须指定主键字段,默认情况下属性被命名为ID.id或者[ClassName]Id,将映 ...

随机推荐

  1. yum安装命令:遇到的问题报错如下: File "/usr/bin/yum", line 30 except KeyboardInterrupt, e: 通过看报错可以了解到是使用了python2的语法,所以了解到当前yum使用的Python2,因为我单独安装了python3,且python3设置为默认版本了,所以导致语法问题 解决方法: 使用python2.6 yum install

    1.安装zip yum install -y unzip zip 2.安装lrszs yum -y install lrzsz 3.安装scp 遇到下面的问题: 结果提示: No package sc ...

  2. 考研部分概念和流程(若不全和错误可提示我补充,另考研帮app推荐)

    上大学必须经过全国统一高考,而就读硕士研究生的途径相对而言要多一些,也更灵活一些.已经工作的人,除了放弃工作报考研究生以外,还可以不脱产申请攻读学位,或申请单独考试.不脱产申请攻读学位,通俗的讲,就是 ...

  3. laravel简书(1)

    Laravel的社区生态 中文社区(http://laravel-china.org) 5.4中文文档(http://d.laravel-china.org/docs/5.4) Laravel源码地址 ...

  4. cnetos 下 rar 解压

    第一步:http://www.rarlab.com/rar/rarlinux-x64-5.3.0.tar.gz 下载 文件 或 wget http://www.rarlab.com/rar/rarli ...

  5. JS-基础动画心得

    写在前面的话:这两种动画方式主要在于对其中算法的理解,理解其中的向上和向下取整很关键.还有一个我犯的毛病,写样式的时候忘记给轮播图ul定位,导致效果出不来,所以有bug时记得排除下css 常用的三种动 ...

  6. jQuery对象和DOM对象相互转换

    DOM对象转为DOM对象: obj = document.getElementById('id') 使用$()包括对象即可 $(obj) jQuery对象转为DOM对象: 在对象后面添加[0] $(' ...

  7. python之模块的导入

    今天在做一个项目的时候卡在模块导入这个点上了.赶紧回头总结一下 一.被导入的文件和工作的脚本在一个目录下 1.导入一个.py文件里的功能或参数(导入模块) 先看一下目录结构: module里有两个功能 ...

  8. poj1860

    刚上来一堆英文着实有点蒙逼,仔细分析是一个Bellman的变形,只要能找出一个无限增大的环这个题就好解决了,我这里用的SPFA,用邻接链表进行储存,直接套用的模板,部分变量名字没有改的很好 #incl ...

  9. sqlserver数据库创建快照发布遇到的错误:对路径“XXXX”访问被拒绝

    在创建了一个sqlserver数据库的发布后,显示创建成功,但当查看快照代理状态时,显示“对路径‘XXXX’访问被拒绝”,这一错误导致订阅无法实现.然后网上有一方案帮忙解决了,主要有以下操作: 1)在 ...

  10. qhfl-7 结算中心

    结算中心,即从购物车前往支付前的确认页面,这里要开始选择优惠券了 """ 前端传过来数据 course_list 课程列表 redis 中将要存放的结算数据 { sett ...