2013-03-14 32 views
5

tôi có hành động này mà nhận một tham số và tôi muốn in ra tất cả các kết quảEntityType 'x' không có khóa được xác định. Xác định chìa khóa cho EntityType này

public ActionResult MusicaGenero(string genero) { 

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


      return View(results); 
     } 

MusicaGenero có điều này

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

thế nào có thể nhưng nó ném một ngoại lệ

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

sự cố ở đây là gì? Tôi đã thêm một chìa khóa nhưng vẫn cho tôi lỗi đó.

+1

Chúng tôi sẽ cần xem mã cho các thực thể của bạn. –

+0

Có vẻ như bạn thiếu một số thuộc tính '[Key]' từ các lớp thực thể đó. – mattytommo

Trả lời

6

Như bạn có thể thấy, bạn đang gặp phải lỗi System.Data.Entity và ít nhất trong trường hợp này, không có gì liên quan đến mã bạn đã đăng.

Khuôn khổ thực thể cần một số cách để biết trường nào cần xác định là khóa chính. Bạn có thể cho biết cách thực hiện điều đó theo hai cách.

Bạn có thể xác định thuộc tính có tên 'Id' hoặc nối 'Id' vào thuộc tính có cùng tên với thực thể. Ví dụ, một trong những sẽ làm việc

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 sẽ hiểu, dựa trên quy ước đặt tên, để làm cho lĩnh vực Id hoặc AlbumId khóa chính.

Điều khác bạn có thể làm là sử dụng thuộc tính System.ComponentModel.DataAnnotations[Key] để xác định khóa. Ví dụ, điều này sẽ làm cho trường Name khóa chính cho bảng.

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

vấn đề được giải quyết, đó là vấn đề – Misters

Các vấn đề liên quan