首页 > 解决方案 > Asp.net IDentity 2 Database First AspNetUserLogins' 没有定义键

问题描述

我首先使用带有 asp.net 身份 2 的数据库,现在在我的 asp.net mvc 项目中,我有两个 datacontext 一个用于身份,另一个用于我的现有数据库。我在我的现有数据库中有一个名为 Client 的表,我不想与 AspNetUsers 创建一对多的关系,我这样做是这样的

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
    public string Code { get; set; }
    public int? ClientId { get; set; }
    public virtual Client Client { get; set; }
}

这是我的 ApplicationDbContext

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

这是我的 Myappcontext

public partial class Myappcontext: DbContext
{
    public Myappcontext()
        : base("name=Myappcontext")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public virtual DbSet<C__MigrationHistory> C__MigrationHistory { get; set; }
    public virtual DbSet<AspNetRoles> AspNetRoles { get; set; }
    public virtual DbSet<AspNetUserClaims> AspNetUserClaims { get; set; }
    public virtual DbSet<AspNetUserLogins> AspNetUserLogins { get; set; }
    public virtual DbSet<AspNetUsers> AspNetUsers { get; set; }
    public virtual DbSet<Client> Client { get; set; }
    public virtual DbSet<products> products { get; set; }
    public virtual DbSet<sysdiagrams> sysdiagrams { get; set; }
}

当我运行应用程序并输入我的电子邮件和密码进行连接时,我收到此错误

 EntityType 'AspNetUserLogins' has no key defined. Define the key for     this EntityType.
AspNetUserLogins: EntityType: EntitySet 'AspNetUserLogins' is based on type 'AspNetUserLogins' that has no keys defined.

我现在不知道如何解决这个问题,因为当我们首先使用数据库和身份 2 时,我没有找到如何创建关系的指南

我很抱歉我的语言不好

标签: c#asp.net-mvcentity-frameworkasp.net-identity

解决方案


经过 2 天的搜索,我没有为我的问题找到任何解决方案,因为我是 asp.net 身份和 ef6 的初学者,我决定为 put 身份和我的数据库创建一个 DbContext,并首先使用代码重新创建我的数据库,如下所示:

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
    public string Code { get; set; }
    public int? ClientId { get; set; }
    public virtual Client Client { get; set; }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
    : base("DefaultConnection", throwIfV1Schema: false)
    {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext,Migrations.Configuration>());
    }
    public DbSet<Client> Clients { get; set; }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

我不知道这是否是最好的工作方法,但直到现在我别无选择


推荐阅读