Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 704
chobo2 Avatar asked Aug 21 '13 21:08

chobo2


1 Answers

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!
like image 193
Mister Epic Avatar answered Oct 09 '22 10:10

Mister Epic