0

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Michael Mahony
  • 1,310
  • 3
  • 15
  • 33
  • ID is the default, it's also the common convention for SQL. – Maess Dec 18 '13 at 17:08
  • "won't work" is wrong - it just takes a little extra setup. – D Stanley Dec 18 '13 at 17:12
  • @Maess : Quite controversial statement regarding common convention for `ID`. Many people/books call it antipattern... – a1ex07 Dec 18 '13 at 17:12
  • I stand by that it is the common convention, regardless of what some authors think. Right/Wrong is not the same as what the common convention is. See: http://stackoverflow.com/questions/1369593/primary-key-foreign-key-naming-convention – Maess Dec 18 '13 at 17:15

1 Answers1

5

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

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

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

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

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

Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122
  • 1
    Is that only if you're doing code first? If you do database first, it should get the PK no matter what it's called, right? – simon at rcl Dec 18 '13 at 17:12
  • @simonatrcl for sure. In some case you've got to help EF to find the right PK (views for example), but that's different. – Raphaël Althaus Dec 18 '13 at 17:16
  • @simonatrcl: the EDMX model will inspect the table and find the primary key constraint and use that to determine the primary key - it can be named anything you like – marc_s Dec 18 '13 at 17:19
  • Thanks both of you. I was pretty sure but its good to have it confirmed. – simon at rcl Dec 18 '13 at 17:25