什么是EF默认ID命名约定代码优先? [英] What is EF Default Id Naming Convention Code First?

查看:100
本文介绍了什么是EF默认ID命名约定代码优先?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EF代码优先方法。我开始制作映射文件(我来自nibbernate背景),但发现除非需要进行一些更改,否则您确实不需要它们。



我想知道



它将是 Product_Id 还是 ProductId



编辑



基于到目前为止的评论让我有些困惑。我之所以问是因为我添加了2个新类(即表)并将它们连接起来并重新创建了数据库。



我的表中与我的新表都有关系的FK都具有 Product_Id,我想可能是因为我没有映射。 / p>

我刚尝试使用映射和相同的问题。



所有其他表关系都具有驼峰大写字母。



编辑2



嗯,一件时髦的事在我的名字里有两次相同的名字表。一旦将其视为FK,又将其视为常规列。



编辑3



弄清楚,说出并在此处发布有助于我找出问题所在。原因是因为我在做poco方式

 公共产品产品{get; set;} 
public Guid ProductId {get;组; }

我现在释放我设置为int类型的ProductId,所以会发生什么db中称为ProductId的列,然后当它有时间制作FK时,它不再可以使用ProductId,而不得不使用Product_Id

解决方案

默认情况下, IdKeyDiscoveryConvention 已注册,并根据其文档


主键检测不区分大小写。公认的命名模式按照优先顺序为:


'Id'


[type name] Id



I am using EF code first approach. I started to make mapping files(I come from nihbernate background) but found out that you really don't need them unless you need to do some changes.

I am wondering what will it use for the naming convention of my Id's?

Will it be Product_Id or ProductId?

Edit

Based on the comments so far this leaves me a bit puzzled. I was asking because I added 2 new classes(ie tables) and hooked them up and recreated my database.

Both of the FK in my tables that have a relationship to my new tables all have "Product_Id" and I thought maybe it was because I had no mapping for it.

I just tried using mapping and same issue.

All other tables relationships have Camel Upper Case.

Edit 2

Hmm something funky is going on it has the same name twice in my table. Once it treats it as a FK and another time it treats it as just a regular column.

Edit 3

Figured it out, talking it out and posting here help me isolate the issue. The reason was because I was doing the poco way

public Product Product {get; set;}
   public Guid ProductId { get; set; }

I now release my ProductId I set as a type of int so what was happening was it would generate a column called ProductId in the db and then when it got time to make the FK it could not use ProductId anymore and had to use Product_Id

解决方案

By default the IdKeyDiscoveryConvention is registered and according to its Documentation:

Primary key detection is case insensitive. Recognized naming patterns are, in order of precedence:

'Id'

[type name]Id

这篇关于什么是EF默认ID命名约定代码优先?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆