none
What happens with primary key fields whose names don't end of ID? RRS feed

  • Question

  • This question relates to ASP.NET MVC, but I think it's more of a entity framework question than a MVC question. In MVC 5 (and I think in MVC 4, but I know MVC 5 better) at least as far as books on MVC go, the typical way of designing applications is to either use Code First, or (my preferred way of doing it) Code First with Existing Database. However, again most examples I see the primary key of any table is appended with "ID". e.g.: PersonID or DepartmentID. However in many databases I work with the habit of the DBA was to use something like Code or Type. As in PersonCode (for those tables which list all people at an organization) or PersonType (for tables that would list the sort of person the person is, as in either a manager or a worker-bee. Yes, I know that the primary key name carries with it some sematic meaning that it probably shouldn't, but that's not the point. The database was designed that way, so I have to work with it like that.)

    What I would like to know is if the "ID" at the end of the primary field's name is special in some way? By that I mean is something like "PersonID" taken to be, by convention, a primary key in a database, as far as entity framework is concerned for generating the classes that it does for Code First with Existing Database in ASP.NET MVC? MVC is big into the "convention over configuration" paradigm; I just want to know if this extends to EF's generation of classes in MVC.


    Rod

    Thursday, October 30, 2014 6:04 PM

Answers

  • Yes, by convention EF will interpret any property named Id or ending in ClassnameId as the primary key, but you can override the convention by using the Key attribute on the property that you woul like to be the key.

    Please mark as answer, if this was it. Visit my SQL Server Compact blog http://erikej.blogspot.com

    • Marked as answer by Doctor-Who Friday, October 31, 2014 1:50 AM
    Thursday, October 30, 2014 6:33 PM

All replies

  • What I would like to know is if the "ID" at the end of the primary field's name is special in some way?

    It's just a property name in an object/entity that was dervived from a column name on db table. The column name could be IDRod, RodTheMagnificent, RodID or just ID. It's whatever the column name is that was defined by someone who created the table definitions.

    Using ID at the end of a column name is just a standard column nameing convention that's been around for decades. ID has no special meaning or usage here other than what name may identify a primay key column on a table. And I have seen tables that don't have ID in the column name for primary-key table columns too.

    Thursday, October 30, 2014 6:33 PM
  • Yes, by convention EF will interpret any property named Id or ending in ClassnameId as the primary key, but you can override the convention by using the Key attribute on the property that you woul like to be the key.

    Please mark as answer, if this was it. Visit my SQL Server Compact blog http://erikej.blogspot.com

    • Marked as answer by Doctor-Who Friday, October 31, 2014 1:50 AM
    Thursday, October 30, 2014 6:33 PM
  • Yes, by convention EF will interpret any property named Id or ending in ClassnameId as the primary key, but you can override the convention by using the Key attribute on the property that you woul like to be the key.

    Is this a code first thing? I have never seen this apply to EF using DB first. However,  I only use EF DB first, even in MVC solutions using N-Tier and/or WCF is in the mix.

    Thursday, October 30, 2014 8:31 PM
  • Yes, it is a code first convention. Have yiu looked at the EF reverse poco template?

    Please mark as answer, if this was it. Visit my SQL Server Compact blog http://erikej.blogspot.com

    Thursday, October 30, 2014 8:36 PM
  • Yes, it is a code first convention. Have yiu looked at the EF reverse poco template?

    I only use DB first with DTO(s),  that I have created on my own  or used the Entity-2-DTO EF solution for VS pointing to the EF model to create the DTO(s).

    That EF code first in a MVC solution up at the Controller can trun out to be a total nightmare.  I like to functional test the DAL using EF with a test harness and test project hosted by VS Unit Test solution or with Resharper well before I get up to the UI doing anything. :)

    Thursday, October 30, 2014 8:46 PM
  • Yes, by convention EF will interpret any property named Id or ending in ClassnameId as the primary key, but you can override the convention by using the Key attribute on the property that you woul like to be the key.

    Please mark as answer, if this was it. Visit my SQL Server Compact blog http://erikej.blogspot.com


    Very interesting. I would have thought that it would be by convention, but I wanted to double check. And thank you for the link to Eric's blog post.

    Rod

    Friday, October 31, 2014 1:51 AM
  • similar question here, How can i tell to EF that a property that ends with id (forexample department_id) is not a primary key?
    Wednesday, October 7, 2015 9:24 AM