EF code first,set composite primary key 复合key问题
2017-09-08 11:08
316 查看
环境:
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”,在方法体中添加如“
参考文献:
http://msdn.microsoft.com/en-us/data/JJ591617.aspx#1.2
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和asp.net mvc4遇到的问题总结
- EF codeFirst Database.SetInitializer的四种选项
- Entity Framework Code First - Stop autoincrement and use my Primary Key
- EF Code First 使用继承类的一个问题
- 安装EntityFramework(EFCodeFirst)遇到的问题(0X80004005)的解决办法
- Entity Framework Code First ---EF Power Tool 和MySql一起使用遇到的问题
- Entity Framework Code First ---EF Power Tool 和MySql一起使用遇到的问题
- vs2015 EF code first 问题待解决
- MVC5中Model层开发数据注解 EF Code First Migrations数据库迁移 C# 常用对象的的修饰符 C# 静态构造函数 MSSQL2005数据库自动备份问题(到同一个局域网上的另一台电脑上) MVC 的HTTP请求
- 使用EF codefirst时遇到的几个问题
- Entity Framework Code First使用者的福音 --- EF Power Tool使用记之二(问题探究)(转)
- EF code first 迁移问题
- Entity Framework Code First使用者的福音 --- EF Power Tool使用记之二(问题探究)
- EF CodeFirst 碰到WCF的各种问题解决
- Entity Framework Code First ---EF Power Tool 和MySql一起使用遇到的问题
- Create Primary Key using Entity Framework Code First
- ef codeFirst 修改表结构 增加字段等 EF code first需要重新生成库导致数据丢失的问题.
- (转 )Entity Framework Code First使用者的福音 --- EF Power Tool使用记之二(问题探究)
- Entity Framework Code First使用者的福音 --- EF Power Tool使用记之二(问题探究)