Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EntityType 'x' has no key defined. Define the key for this EntityType

I have this action which receives a parameter and i want to print out all the results

public ActionResult MusicaGenero(string genero) {

            //should return  more than 30 rows 
             var results = con.artista.Where(x=>x.genero==genero);


            return View(results);
        }

MusicaGenero have this

@model IEnumerable<MvcApplication1.Models.detallesMusica>

@{
    ViewBag.Title = "MusicaGenero";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Musica del genero de: @ViewBag.genero</h2>

<ul>
@foreach(var detallesMusica in Model)
{
    <li>@detallesMusica.artista</li>        
    <li>@detallesMusica.nombre</li> 
    <li>@detallesMusica.publicado</li>  
    <li>@detallesMusica.costo</li>  
}
</ul>

How can but it throws an exception

\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'album' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'genero' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'artista' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'albums' is based on type 'album' that has no keys defined.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'generos' is based on type 'genero' that has no keys defined.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'artista' is based on type 'artista' that has no keys defined.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation:

\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'album' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'genero' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'artista' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'albums' is based on type 'album' that has no keys defined.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'generos' is based on type 'genero' that has no keys defined.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'artista' is based on type 'artista' that has no keys defined.

what is the problem here? I added already a key but still giving me that error.

like image 770
Misters Avatar asked Mar 14 '13 17:03

Misters


1 Answers

As you can see, you are getting System.Data.Entity errors, and these, at least in this instance, have nothing to do with the code you've posted.

The Entity Framework needs some way of know what field it should define as the primary key. You can tell it how to do that in two ways.

You can define a property with the name 'Id' or append 'Id' to a property having the same name as the entity. For example, either of these would work

public class Album
{
  public string Id {get; set;}
  public string Name {get; set;}
}
public class Album
{
  public string AlbumId {get; set;}
  public string Name {get; set;}
}

EF would understand, based on the naming convention, to make the field Id or AlbumId the primary key.

The other thing you could do is use the System.ComponentModel.DataAnnotations [Key] attribute to define the key. For example, this would make the field Name the primary key for the table.

using System.ComponentModel.DataAnnotations;
//...
public class Album
{
  [Key]
  public string Name {get; set;}
}
like image 93
Forty-Two Avatar answered Sep 24 '22 14:09

Forty-Two