简体   繁体   English

实体框架主键约定。 如何允许更多列名中带有“ id”的列?

[英]Entity Framework primary key convention. How to allow more columns with 'id' in their name?

I am developing a web app on an existing database which unfortunately I cannot change. 我正在现有数据库上开发Web应用程序,很遗憾,我无法更改。 I get this error 我得到这个错误

"More than one property map found for property 'Id' when using case-insensitive search. " “使用不区分大小写的搜索时,为属性'Id'找到了多个属性映射。”

After a little search I found that the problem is that there are some conventions in EF and more specifically one that states (according to what I read here ) that: 经过一番搜索后,我发现问题在于EF中有一些约定,更具体地讲(根据我在此处的阅读内容)指出:

if the field name contains a suffix of ID (case-insensitive), then EF will automatically include it as a primary key 如果字段名称包含ID后缀(不区分大小写),则EF会自动将其作为主键

The table has already a primary key (named 'id'). 该表已经有一个主键(名为“ id”)。 Is there a way to disable the EF conventions or in any other way to allow having besides my 'id' field, a field named "something_id" without getting this error? 有没有一种方法可以禁用EF约定或以其他任何方式允许除我的“ id”字段外,还有一个名为“ something_id”的字段而不会出现此错误?

this is how to disable the id convention of EF: 这是如何禁用EF的id约定:

inside OnModelCreating 在OnModelCreating内部

            modelBuilder.Conventions.Remove<IdKeyDiscoveryConvention>(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 mvc实体框架模型:主键和另外2个键的问题 - mvc entity framework model: a primary key and 2 more keys issue 实体框架查询返回我的主键加上$ id - Entity Framework query return my primary key plus $id 使用在多个列上具有主键的实体框架更新数据库 - Update Database using Entity Framework having primary key on multiple Columns 实体框架中的主键未增加 - Primary Key not incrementing in Entity Framework “传递的主键值的数量必须与实体上定义的主键值的数量匹配。 参数名称:keyValues”实体框架 - “The number of primary key values passed must match number of primary key values defined on the entity. Parameter name: keyValues” Entity Framework 如何在没有主键实体框架的情况下映射表 - How to map table without primary key Entity Framework Entity Framework 如何为主键值生成 GUID? - How does Entity Framework generate a GUID for a primary key value? 实体框架:如何使用Fluent API实现共享主键? - Entity Framework: how to implement shared primary key with Fluent API? 实体框架一对多关系-当外键名称命名为ID时,如何重命名? - Entity framework one-to-many relationship - how to rename foreign key name when it is named Id? 自定义字符串作为具有实体框架的主键 - Custom string as Primary Key with Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM