chobo2 chobo2 - 2 months ago 12
C# Question

What is EF Default Id Naming Convention Code First?

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

Answer

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

Comments