2013-03-14 13 views
5

mam tę akcję, która przyjmuje parametr i chcę, aby wydrukować wszystkie wynikiEntityType "x" nie ma zdefiniowanego klucza. Zdefiniować klucz do tego 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 mieć ten

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

jak może, ale zgłasza wyjątek

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

na czym polega problem? Dodałem już klucz, ale nadal daje mi ten błąd.

+1

Będziemy musieli zobaczyć kod dla Twoich podmiotów. –

+0

Wygląda na to, że brakuje ci niektórych atrybutów "[Key]" z tych klas jednostek. – mattytommo

Odpowiedz

6

Jak widać, otrzymujesz błędy System.Data.Entity, a te, przynajmniej w tym przypadku, nie mają nic wspólnego z opublikowanym przez Ciebie kodem.

Organizacja Entity Framework potrzebuje wiedzieć, które pole należy zdefiniować jako klucz podstawowy. Możesz powiedzieć, jak to zrobić na dwa sposoby.

Można zdefiniować właściwość o nazwie "Id" lub dołączyć "Id" do właściwości o tej samej nazwie, co obiekt. Na przykład, jeden z nich będzie działać

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 by zrozumieć, na podstawie konwencji nazewnictwa, aby pole Id lub AlbumId klucz podstawowy.

Inną rzeczą, którą można zrobić, to zdefiniować klucz za pomocą atrybutu System.ComponentModel.DataAnnotations[Key]. Na przykład sprawiłoby to, że pole Name jest kluczem podstawowym dla tabeli.

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

problem rozwiązany, to był problem – Misters

Powiązane problemy