5

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

abatishchev
  • 98,240
  • 88
  • 296
  • 433
chobo2
  • 83,322
  • 195
  • 530
  • 832

2 Answers2

9

You can use either ProductId or Id and it will be recognized as your primary key by Entity Framework's naming conventions.

You can also use the Key data annotation if you want to break convention.

[Key]
public int Product_Id {get;set;} //Note the underscore

You can also use the Fluent API:

modelBuilder.Entity<Product>().HasKey(t => t.Product_Id); //Breaking convention again!
Mister Epic
  • 16,295
  • 13
  • 76
  • 147
8

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

Community
  • 1
  • 1
haim770
  • 48,394
  • 7
  • 105
  • 133
  • "telling a bit different story": not really, it merely combines the `IdKeyDiscoveryConvention` and the [`StoreGeneratedIdentityKeyConvention`](http://msdn.microsoft.com/en-us/library/system.data.entity.modelconfiguration.conventions.storegeneratedidentitykeyconvention%28v=vs.103%29.aspx) (seemingly lacking documentation) in a single paragraph. –  Aug 21 '13 at 22:14
  • True. but only in case the type is `Int16`, `Int32` or `Int64`. – haim770 Aug 21 '13 at 22:20
  • Ah, that is a good point. The fact that `Guid` has been dropped surprises me. –  Aug 21 '13 at 22:38