Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

database first - EntityType xxx has no key defined. Define the key for this EntityType

I have database with 80 tables (each table has got primary key). I have generated model in ASP.NET MVC project from database (Add - New Item - ADO.NET Entity Data Model). But in generated model I have 80 classes (for each table in database) without attribute [Key] for example:

//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Blog.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Comments
    {
        public int CommentId { get; set; }        
        public string Content { get; set; }
    }
}

So I have errors: EntityType xxx has no key defined. Define the key for this EntityType.

I often change database and then model in project so I can't each time add [Key] attribute to 80 AUTOGENERATED classes - what can I do??

like image 234
michael Avatar asked Aug 03 '13 17:08

michael


2 Answers

I had same issue and solved it by adding the [Key] attributes into each class, but to avoid doing this manually, I modified the .tt file that is used to generate each of the classes. I added an if condition in the loop that loops through each simple property of the class. It adds the string "[Key]" before each primary key field. If you have a table with multiple fields making up the PK then you'd have to manually add the attributes for this ([Key, Column(Order = 0)]) or further work to update the .tt file (I was too lazy since I only have one table so far with a composite key). Once done, I saved the .tt file and it regenerated all of my classes and these errors went away.

foreach (var edmProperty in simpleProperties)
{
        if (ef.IsKey(edmProperty)) { 
#>           [Key]
<#      } 
like image 130
Kelly Avatar answered Oct 22 '22 08:10

Kelly


If your model contains any tables with multiple primary keys, you'll need to add the Column annotation to your .tt file near line 66 as well, like this:

var simpleProperties = typeMapper.GetSimpleProperties(entity);
if (simpleProperties.Any())
{
    var keyIndex = 0; // This is a new line
    foreach (var edmProperty in simpleProperties)
    {
        // The following if block is new
        if (ef.IsKey(edmProperty)) 
        {
#>          [System.ComponentModel.DataAnnotations.Key]
            [System.ComponentModel.DataAnnotations.Schema.Column(Order = <#=keyIndex.ToString()#>)]
<#
            keyIndex++;
#>
<#      }
#>
        <#=codeStringGenerator.Property(edmProperty)#>
<#
    }
}
like image 35
Chris Schiffhauer Avatar answered Oct 22 '22 08:10

Chris Schiffhauer