EntityType 'x' does not have a key. Define a key for this EntityType

I have this action that receives a parameter and I want to print 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 has it

@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? I have already added the key, but still give me this error.

+5
source share
1 answer

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

For the Entity Framework, you need to know in which field it should be defined as the primary key. You can tell how to do this in two ways.

"Id" "Id" , . ,

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 , Id AlbumId .

, , System.ComponentModel.DataAnnotations [Key] . , Name .

using System.ComponentModel.DataAnnotations;
//...
public class Album
{
  [Key]
  public string Name {get; set;}
}
+6

All Articles