2013-03-14 3 views
5

У меня есть это действие, которое принимает параметр и я хочу, чтобы распечатать все результатыEntityType 'x' не имеет определенного ключа. Определите ключ для этого 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 имеют этот

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

Как можно, но он бросает исключение

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

В чем проблема? Я добавил уже ключ, но все же даю мне эту ошибку.

+1

Нам нужно будет увидеть код для ваших объектов. –

+0

Похоже, что вам не хватает некоторых атрибутов '[Key]' из этих классов сущностей. – mattytommo

ответ

6

Как вы можете видеть, вы получаете ошибки System.Data.Entity, и они, по крайней мере, в этом случае не имеют ничего общего с кодом, который вы опубликовали.

В Entity Framework требуется какой-то способ узнать, какое поле должно быть определено в качестве первичного ключа. Вы можете сказать, как это сделать двумя способами.

Вы можете определить свойство с именем «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;} 
} 
+0

проблема решена, это была проблема – Misters

Смежные вопросы

 Смежные вопросы