gpt4 book ai didi

asp.net-mvc-3 - EntityType 'x' 没有定义键。定义此 EntityType 的键

转载 作者:行者123 更新时间:2023-12-02 06:15:35 24 4
gpt4 key购买 nike

我有一个接收参数的操作,我想打印出所有结果

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.

这里有什么问题吗?我已经添加了一个 key ,但仍然出现该错误。

最佳答案

正如您所看到的,您收到了 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 会理解将字段 IdAlbumId 作为主键。

您可以做的另一件事是使用 System.ComponentModel.DataAnnotations [Key] 属性来定义键。例如,这将使字段Name成为表的主键。

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

关于asp.net-mvc-3 - EntityType 'x' 没有定义键。定义此 EntityType 的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15416022/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com