环境:

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. Failed to resolve:com.android.support:appcompat-v7:报错处理

    既然是版本问题,那就的先去了解自己的电脑安装的SDK工具版本,点开SDK Manager图标,然后选中Updates就可以看到了 这里我的 sdk 工具版本就是26.1.1了 报错是因为自己的andr ...

  2. 【转】Appium 优化版

    Appium 开源分享优化版 之前分享过PageObject+Python+Appium 本版本是对上次版本较大改版,主要解决了: 失败重连一次(默认一次)可配置多次 基于appium1.7.1 ui ...

  3. CentOS7 firewalld防火墙配置

    [root@ecs ~]# firewall-cmd --version       //查看版本0.3.9 [root@ecs ~]# firewall-cmd --state        //查 ...

  4. Django自动获取项目中的全部URL

    import re from collections import OrderedDict from django.conf import settings from django.utils.mod ...

  5. ES之五:ElasticSearch聚合

    前言 说完了ES的索引与检索,接着再介绍一个ES高级功能API – 聚合(Aggregations),聚合功能为ES注入了统计分析的血统,使用户在面对大数据提取统计指标时变得游刃有余.同样的工作,你在 ...

  6. osg探究补充:DatabasePager类简介

    简介 DatabasePager类,也就是常说的数据库分页技术,简单来说,就是在进行数据库查找时,有可能满足条件的数据很多,为了提高相应速度我们进行数据查找时进行分页查找与显示,当点击下一页时才会进行 ...

  7. 我的idea突然没有SVN了是怎么回事

    总结一下没有svn选项的几种情况: 情况1:IntelliJ IDEA打开带SVN信息的项目不显示SVN信息,项目右键SVN以及图标还有Changes都不显示解决方法 在VCS菜单中有个开关,叫Ena ...

  8. python模块:xml.dom.minidom

    """Simple implementation of the Level 1 DOM. Namespaces and other minor Level 2 featu ...

  9. MySQL中校验规则(collation)的选取对实际数据筛选的影响

    在mysql中存在着各种utf8编码格式,如下表:1)utf8_bin2)utf8_general_ci utf8_bin将字符串中的每一个字符用二进制数据存储,区分大小写.utf8_genera_c ...

  10. centos下安装nethogs

    wget https://github.com/raboof/nethogs/archive/v0.8.1.tar.gzyum install libpcap-develtar zxvf v0.8.1 ...