在一个DbContext中移动身份(代码优先)

时间:2014-07-30 15:09:25

标签: c# asp.net-mvc entity-framework ef-code-first asp.net-identity

我有asp.net MVC应用程序,实体框架6,代码第一个功能

这是我的身份类代码

namespace Core.Domain.Identity
{
public partial class AspNetRoles : BaseEntity
{
    public AspNetRoles()
    {
        AspNetUsers = new HashSet<AspNetUsers>();
    }
    public string RlId { get; set; }
    public string Name { get; set; }
    public virtual ICollection<AspNetUsers> AspNetUsers { get; set; }
}

public partial class AspNetUserClaims : BaseEntity
{
    public string UserId { get; set; }
    public string ClaimType { get; set; }
    public string ClaimValue { get; set; }
    public virtual AspNetUsers AspNetUsers { get; set; }
}

public partial class AspNetUserLogins : BaseEntity
{
    //[Key]
    //[Column(Order = 0)]
    public string LoginProvider { get; set; }
    //[Key]
    //[Column(Order = 1)]
    public string ProviderKey { get; set; }
    //[Key]
    //[Column(Order = 2)]
    public string UserId { get; set; }
    public virtual AspNetUsers AspNetUsers { get; set; }
}

public partial class AspNetUsers : BaseEntity
{
    public AspNetUsers()
    {
        AspNetUserClaims = new HashSet<AspNetUserClaims>();
        AspNetUserLogins = new HashSet<AspNetUserLogins>();
        AspNetRoles = new HashSet<AspNetRoles>();
    }

    public string UsId { get; set; }
    //[StringLength(256)]
    public string Email { get; set; }
    public bool EmailConfirmed { get; set; }
    public string PasswordHash { get; set; }
    public string SecurityStamp { get; set; }
    public string PhoneNumber { get; set; }
    public bool PhoneNumberConfirmed { get; set; }
    public bool TwoFactorEnabled { get; set; }
    public DateTime? LockoutEndDateUtc { get; set; }
    public bool LockoutEnabled { get; set; }
    public int AccessFailedCount { get; set; }
    //[Required]
    //[StringLength(256)]
    public string UserName { get; set; }
    public virtual ICollection<AspNetUserClaims> AspNetUserClaims { get; set; }
    public virtual ICollection<AspNetUserLogins> AspNetUserLogins { get; set; }
    public virtual ICollection<AspNetRoles> AspNetRoles { get; set; }
}
}

映射代码

public partial class AspNetRolesMap : EntityTypeConfiguration<AspNetRoles>
{
    public AspNetRolesMap()
    {
        this.ToTable("AspNetRoles");
        this.HasKey(anr => anr.RlId)
            .Property(anr => anr.RlId).IsRequired().HasMaxLength(128);
        this.Property(anr => anr.Name).IsRequired();
        this.HasMany(anr => anr.AspNetUsers)
            .WithMany(anr => anr.AspNetRoles)
            .Map(m => m.ToTable("AspNetUserRoles").MapLeftKey("RoleId").MapRightKey("UserId"));

    }
}

public partial class AspNetUserClaimsMap : EntityTypeConfiguration<AspNetUserClaims>
{
    public AspNetUserClaimsMap()
    {
        this.ToTable("AspNetUserClaims");
        this.HasKey(anuc => anuc.Id)
            .Property(anuc => anuc.Id).IsRequired();
        this.Property(anuc => anuc.ClaimType).IsMaxLength();
        this.Property(anuc => anuc.ClaimValue).IsMaxLength();

        //this.HasRequired(anuc => anuc.AspNetUsers)
        //  .WithMany()
        //  .HasForeignKey(anuc => anuc.UserId);
    }
}

public partial class AspNetUserLoginsMap : EntityTypeConfiguration<AspNetUserLogins>
{
    public AspNetUserLoginsMap()
    {
        this.ToTable("AspNetUserLogins");
        this.HasKey(x => new { x.LoginProvider, x.ProviderKey, x.UserId });
        //this.HasKey(anul => anul.LoginProvider)
        //.Property(anul => anul.LoginProvider).HasMaxLength(128).IsRequired();

        //this.HasKey(anul => anul.ProviderKey)
        //  .Property(anul => anul.ProviderKey).HasMaxLength(128).IsRequired();

        //this.HasKey(anul => anul.UserId)
        //  .Property(anul => anul.UserId).HasMaxLength(128).IsRequired();

        //this.HasRequired(anul => anul.AspNetUsers)
        //  .WithMany()
        //  .HasForeignKey(anul => anul.UserId);
    }
}

public partial class AspNetUsersMap : EntityTypeConfiguration<AspNetUsers>
{
    public AspNetUsersMap()
    {
        this.ToTable("AspNetUsers");
        this.HasKey(anr => anr.UsId)
            .Property(anr => anr.UsId).HasMaxLength(128).IsRequired();
        this.Property(anu => anu.EmailConfirmed).IsRequired();
        this.Property(anu => anu.PasswordHash).IsMaxLength();
        this.Property(anu => anu.SecurityStamp).IsMaxLength();
        this.Property(anu => anu.PhoneNumber).IsMaxLength();
        this.Property(anu => anu.PhoneNumberConfirmed).IsRequired();
        this.Property(anu => anu.TwoFactorEnabled).IsRequired();
        this.Property(anu => anu.LockoutEndDateUtc).IsOptional();
        this.Property(anu => anu.LockoutEnabled).IsRequired();
        this.Property(anu => anu.AccessFailedCount).IsRequired();
        this.Property(anu => anu.UserName).HasMaxLength(256).IsRequired();

        this.HasMany(anu => anu.AspNetUserClaims)
            .WithRequired(anu => anu.AspNetUsers)
            .HasForeignKey(anu => anu.UserId);
        this.HasMany(anu => anu.AspNetUserLogins)
            .WithRequired(anu => anu.AspNetUsers)
            .HasForeignKey(anu => anu.UserId);

        //this.HasMany(anu => anu.AspNetRoles)
        //  .WithMany(anu => anu.AspNetUsers)
        //  .Map(m => m.ToTable("AspNetUserRoles").MapLeftKey("UsId").MapRightKey("Id"));
    }
}

和上下文

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 class PsCoDbContext : IdentityDbContext<ApplicationUser>, IDbContext
{

    public PsCoDbContext() : base("name=DataModel")
    {

    }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
        .Where(type => !String.IsNullOrEmpty(type.Namespace))
        .Where(type => type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
        foreach(var type in typesToRegister)
        {
            dynamic configurationInstance = Activator.CreateInstance(type);
            modelBuilder.Configurations.Add(configurationInstance);
        }
        base.OnModelCreating(modelBuilder);
    }


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

}

如果我开始申请,我会得到一个例外

类型&#39; System.InvalidOperationException&#39;的例外情况发生在EntityFramework.dll中但未在用户代码中处理 附加信息:实体类型&#39; IdentityRole&#39;和#AspNetRoles&#39;无法分享表格#AspNetRoles&#39;因为它们不在同一类型层次结构中,或者没有有效的一对一外键关系,并且它们之间具有匹配的主键。

enybody可以帮我解决我的错误吗?

0 个答案:

没有答案