2013-03-14 14 views
5

Ho questa azione che riceve un parametro e voglio stampare tutti i risultatiEntityType 'x' non ha una chiave definita. Definire la chiave per questo EntityType

public ActionResult MusicaGenero(string genero) { 

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


      return View(results); 
     } 

MusicaGenero avere questo

@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> 

Come può, ma viene generata un'eccezione

\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. 

qual è il problema qui? Ho aggiunto già una chiave ma continuo a darmi quell'errore.

+1

Avremo bisogno di vedere il codice per le vostre entità. –

+0

Sembra che manchi alcuni attributi di '[Chiave]' da quelle classi di entità. – mattytommo

risposta

6

Come puoi vedere, stai ricevendo errori System.Data.Entity e questi, almeno in questo caso, non hanno nulla a che fare con il codice che hai postato.

Entity Framework richiede un modo per sapere quale campo deve essere definito come chiave primaria. Puoi dirgli come farlo in due modi.

È possibile definire una proprietà con il nome "Id" o aggiungere "Id" a una proprietà con lo stesso nome dell'entità. Ad esempio, uno di questi funzionerebbe

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 avrebbe capito, in base alla convenzione di denominazione, per rendere il campo Id o AlbumId la chiave primaria.

L'altra cosa che si potrebbe fare è usare l'attributo System.ComponentModel.DataAnnotations[Key] per definire la chiave. Ad esempio, ciò renderebbe il campo Name la chiave primaria per la tabella.

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

problema risolto, questo era il problema – Misters

Problemi correlati