[英]Asp.net IDentity 2 Database First AspNetUserLogins' has no key defined
i'm using database first approche with asp.net identity 2 , now in my asp.net mvc project i have two datacontext one for the identity and other for my exist database . 我正在使用具有asp.net标识2的数据库优先方法,现在在我的asp.net mvc项目中,我有两个datacontext,一个用于标识,另一个用于我现有的数据库。 i have a table in my exist database called Client and i wan't to create one to many relation with AspNetUsers i doit like this
我在现有数据库中有一个名为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; }
}
and this is my ApplicationDbContext 这是我的ApplicationDbContext
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
and this is my Myappcontext 这是我的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; }
}
when i run the application and enter my email and password to connect i get this error 当我运行该应用程序并输入我的电子邮件和密码进行连接时,出现此错误
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.
i don't now how i fix this problem because i didn't find a guide how to create relation when we use database first and identity 2 我现在不解决该问题,因为当我们首先使用数据库和身份2时,我没有找到如何创建关系的指南
and i'm sorry about my bad language 我为我的语言不好而感到抱歉
After 2 days of searching i didn't find any solutions for my probleme because i'm beginer in asp.net identity and ef6 i decide to create one DbContext for put identity and my database and using code first to recreate my database like this : 经过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();
}
}
i don't know if this is the best method to work but i have no choice until now 我不知道这是否是最好的方法,但是直到现在我别无选择
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.