实体框架:我的数据库中的标识字段的名称是否重要?

时间:2022-12-20 21:28:13

I am new to entity framework and learning it slowly. I have searched for this answer and do not find an answer. Let's say I have the following table definition:

我是实体框架的新手并慢慢学习它。我搜索了这个答案,但没有找到答案。假设我有以下表定义:

Products
-----------
ProdID (PK, identity field)
ProdName
ProdSKU

Is it OK that the identity field is named "ProdID" or does it need to say "ID"? I've been told by another developer on my team that it MUST be called "ID" or entity framework won't work, but that makes no sense to me. Any advice is welcome.

身份字段命名为“ProdID”还是需要说“ID”?我的团队中的另一位开发人员告诉我,它必须被称为“ID”,否则实体框架将无效,但这对我来说毫无意义。欢迎任何建议。

1 个解决方案

#1


5  

They are naming conventions in EF (code first, of course)

它们是EF中的命名约定(代码优先,当然)

If you call it Id or <NameOfEntity>Id (so ProductsId in your case), it will be "auto detected" as the PK.

如果您将其称为Id或 Id(在您的情况下为ProductsId),它将被“自动检测”为PK。

If you don't follow conventions, you'll have to tell EF that it's the PK.

如果你不遵守惯例,你必须告诉EF它是PK。

With an attribute on your property ([Key]), or with fluent api (modelBuilder.Entity<Products>().HasKey(t => t.ProdID);).

使用属性上的属性([Key]),或者使用流畅的api(modelBuilder.Entity ()。HasKey(t => t.ProdID);)。

So don't trust other developers (I'm a developer).

所以不要相信其他开发人员(我是开发人员)。

#1


5  

They are naming conventions in EF (code first, of course)

它们是EF中的命名约定(代码优先,当然)

If you call it Id or <NameOfEntity>Id (so ProductsId in your case), it will be "auto detected" as the PK.

如果您将其称为Id或 Id(在您的情况下为ProductsId),它将被“自动检测”为PK。

If you don't follow conventions, you'll have to tell EF that it's the PK.

如果你不遵守惯例,你必须告诉EF它是PK。

With an attribute on your property ([Key]), or with fluent api (modelBuilder.Entity<Products>().HasKey(t => t.ProdID);).

使用属性上的属性([Key]),或者使用流畅的api(modelBuilder.Entity ()。HasKey(t => t.ProdID);)。

So don't trust other developers (I'm a developer).

所以不要相信其他开发人员(我是开发人员)。