asp.net-mvc-3 – EntityType’x’没有定义键.定义此EntityType
发布时间:2020-12-16 07:05:33 所属栏目:asp.Net 来源:网络整理
导读:我有这个动作接收参数,我想打印出所有结果 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 IEnumerableM
我有这个动作接收参数,我想打印出所有结果
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. 这里有什么问题?我已经添加了一个密钥,但仍然给了我这个错误. 解决方法
如您所见,您收到的是System.Data.Entity错误,至少在这种情况下,这些错误与您发布的代码无关.
实体框架需要某种方式知道它应该定义哪个字段作为主键. 您可以定义名称为“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]属性来定义密钥. using System.ComponentModel.DataAnnotations; //... public class Album { [Key] public string Name {get; set;} } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读