C# EntityType未定义任何键

C# EntityType未定义任何键,c#,asp.net-mvc,key,identity,C#,Asp.net Mvc,Key,Identity,我正在创建一个应用程序,用户通过Facebook oAuth登录,然后设置歌曲列表。我收到以下错误消息: BandFinderCsharp.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. BandFinderCsharp.Models.IdentityUserRole: : EntityType 'Identity

我正在创建一个应用程序,用户通过Facebook oAuth登录,然后设置歌曲列表。我收到以下错误消息:

BandFinderCsharp.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.

BandFinderCsharp.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.

IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.

IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.

“我的歌曲控制器”中出现错误消息:
`namespace BandFinder.Controllers.Bread
{
公共类歌曲控制器:控制器
{
私有SongDBContext db=新的SongDBContext();
//获取:歌曲
公共行动结果索引()
{

返回视图(db.Songs.ToList());查看实体时,我缺少定义主键字段的[Key]属性

看这个问题,首先回答:

如果
ApplicationUser
类是要保存在数据库中的对象,则它必须包含名为
Id
的字段,默认情况下,该字段是Entity Framework链接到的对象的主键

您的对象应该如下所示:

public class ApplicationUser
{
    public int Id { get; set; }
    public string Name { get; set; }
}
或者,如果要将其他属性设置为对象的主键,则应在该字段上方添加
[key]
属性,并且还需要添加
System.ComponentModel.DataAnnotations
命名空间:

public class ApplicationUser
{
    public int Id { get; set; }
    [Key]
    public string Name { get; set; }
}

我添加了:[Key]公共重写字符串Id{get;set;}到我的“ApplicationUser”模型。这还没有修复它。我从“IdentityUser”继承,无法更改该类。您可以为
IdentityUserLogins
IdentityUserRole
提供模型类吗?它们的所有公共成员都定义为属性吗?请参阅:@Lukas我提供了模型类。
namespace Microsoft.AspNet.Identity.EntityFramework
{
    //
    // Summary:
    //     Default EntityFramework IUser implementation
    public class IdentityUser : IdentityUser<string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>, IUser, IUser<string>
    {
        //
        // Summary:
        //     Constructor which creates a new Guid for the Id
        public IdentityUser();
        //
        // Summary:
        //     Constructor that takes a userName
        //
        // Parameters:
        //   userName:
        public IdentityUser(string userName);
    }
}
namespace Microsoft.AspNet.Identity.EntityFramework
{
    //
    // Summary:
    //     Entity type for a user's login (i.e. facebook, google)
    public class IdentityUserLogin : IdentityUserLogin<string>
    {
        public IdentityUserLogin();
    }
}
public class ApplicationUser
{
    public int Id { get; set; }
    public string Name { get; set; }
}
public class ApplicationUser
{
    public int Id { get; set; }
    [Key]
    public string Name { get; set; }
}