none
Get-PrimaryKey : Cannot find primary key property for type

    Question

  • Hi all

    Add Code Generation Item...
    Added DbContext Generator
    The class below was created for my table called "Component":

        public partial class Component 
        { 
            public int component_id { get; set; } 
            public System.DateTime created { get; set; } 
            public string created_by { get; set; } 
            public Nullable<System.DateTime> modified { get; set; } 
            public string modified_by { get; set; } 
            public string name { get; set; } 
            public string href { get; set; } 
        }
    

    Now, I am trying to re-create the classes to use Repositories:

    Install-Package MvcScaffolding
    Scaffold Controller Component –Repository -Force

    And I am gettting the following error:

    PM> Scaffold Controller Component –Repository -Force
    Scaffolding ComponentsController...
    Get-PrimaryKey : Cannot find primary key property for type 'Att.Uds.CustomXml.Models.Component'. No properties appear to be primary keys.
    At C:\Users\...\My Projects\_MVC3\Att.Uds.CustomXml\packages\T4Scaffolding.1.0.4\tools\EFRepository\T4Scaffolding.EFRepository.ps1:19 char:29
    + $primaryKey = Get-PrimaryKey <<<< $foundModelType.FullName -Project $Project -ErrorIfNotFound
    + CategoryInfo : NotSpecified: (:) [Get-PrimaryKey], Exception
    + FullyQualifiedErrorId : T4Scaffolding.Cmdlets.GetPrimaryKeyCmdlet

    Get-ProjectType : Cannot find a type matching the name 'AttUdsCustomXmlContext'. Try specifying the fully-qualified type name, including namespace.
    At C:\Users\...\My Projects\_MVC3\Att.Uds.CustomXml\packages\MvcScaffolding.1.0.4\tools\Controller\MvcScaffolding.Controller.ps1:76 char:66
    + if (!$foundDbContextType) { $foundDbContextType = Get-ProjectType <<<< $DbContextType -Project $Project }
    + CategoryInfo : NotSpecified: (:) [Get-ProjectType], InvalidOperationException
    + FullyQualifiedErrorId : T4Scaffolding.Cmdlets.GetProjectTypeCmdlet

    Workaround:
    If I rename component_id to id, everything works. At this point, I am still very confused of how to use EF.
    I want to use the database exactly like the DBA design without doing any modifications to the field names.

    Is this normal? what is the best practice or a solution for this?

    Thank you


    Max
    Tuesday, October 04, 2011 8:28 PM

Answers

  • Hi

    By default EF considers property as PrimaryKey, only if it is in format of ID or {classname}ID...in your case it should be ComponentId or Id.. Still you can carry with Component_Id by specifying [Key] Attribute.

    change your class to

     public partial class Component 
        { 
            [Key]
            public int component_id { get; set; } 
            public System.DateTime created { get; set; } 
            public string created_by { get; set; } 
            public Nullable<System.DateTime> modified { get; set; } 
            public string modified_by { get; set; } 
            public string name { get; set; } 
            public string href { get; set; } 
        }


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    • Marked as answer by Maximusdm Wednesday, October 05, 2011 2:58 PM
    Wednesday, October 05, 2011 4:21 AM

All replies

  • Hi

    By default EF considers property as PrimaryKey, only if it is in format of ID or {classname}ID...in your case it should be ComponentId or Id.. Still you can carry with Component_Id by specifying [Key] Attribute.

    change your class to

     public partial class Component 
        { 
            [Key]
            public int component_id { get; set; } 
            public System.DateTime created { get; set; } 
            public string created_by { get; set; } 
            public Nullable<System.DateTime> modified { get; set; } 
            public string modified_by { get; set; } 
            public string name { get; set; } 
            public string href { get; set; } 
        }


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    • Marked as answer by Maximusdm Wednesday, October 05, 2011 2:58 PM
    Wednesday, October 05, 2011 4:21 AM
  • aww thanks! sounds like I need to research about EF conventions!

     

    thank you


    Max
    Wednesday, October 05, 2011 2:58 PM
  • To what namespace belongs the [Key] ?
    Tuesday, December 27, 2011 12:22 PM
  • OMG!!! THANK YOU!!  I have been looking for this answer for the last two days. 

    Do you know if there is a way to override this default?  Our convention for Primary keys is {fieldname}Key I realize I can update my model (we are using an EF model then genning POCO from it) but this is a bit of extra maintenance.

     

    Thank you,

    Chad

    Friday, January 06, 2012 5:20 PM
  • Just go to your model, select the key field and change its Name.

     

    Chad

    Friday, January 06, 2012 5:22 PM