如何在更改身份模型后在ASP.Net MVC 5应用程序中创建迁移?

时间:2016-06-09 17:18:58

标签: c# asp.net asp.net-mvc asp.net-mvc-5 asp.net-identity

我正在使用ASP.NET MVC 5 / Entity 6框架以代码优先方法在c#中构建应用程序。我使用documentation将身份2中的Id属性从string更改为integer

然后我使用以下命令

创建迁移
Add-Migration InitialCreate

由于以下错误,迁移无法添加

DbContexts.CustomUserLogin: : EntityType 'CustomUserLogin' has no key defined. Define the key for this EntityType.
DbContexts.CustomUserRole: : EntityType 'CustomUserRole' has no key defined. Define the key for this EntityType.
CustomUserLogins: EntityType: EntitySet 'CustomUserLogins' is based on type 'CustomUserLogin' that has no keys defined.
CustomUserRoles: EntityType: EntitySet 'CustomUserRoles' is based on type 'CustomUserRole' that has no keys defined.

我通过添加更多外键和一些类似的关系

ApplicationUser类进行了一些更改
using System.Data.Entity;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Proj1.Areas.Base.Models;
using System.Collections.Generic;

namespace Proj1.Models
{
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser<int, CustomUserLogin, CustomUserRole, CustomUserClaim> 
    {
        [ForeignKey("Role")]
        public int RoleId { get; set; }

        [ForeignKey("Client")]
        public int CurrentClientId { get; set; }

        [ForeignKey("Campaign")]
        public int? CurrentCampaignId { get; set; }

        public virtual Role Role { get; set; }

        public virtual Client Client { get; set; }

        public virtual Campaign Campaign { get; set; }

        public virtual List<UserToPrivilege> UserToPrivileges { get; set; }

        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, int> 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 ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole, int, CustomUserLogin, CustomUserRole, CustomUserClaim>
    {
        public ApplicationDbContext()
            : base("DefaultConnection")
        {
        }

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

    }

    public class CustomUserRole : IdentityUserRole<int> { }
    public class CustomUserClaim : IdentityUserClaim<int> { }
    public class CustomUserLogin : IdentityUserLogin<int> { }

    public class CustomRole : IdentityRole<int, CustomUserRole>
    {
        public CustomRole() { }
        public CustomRole(string name) { Name = name; }
    }

    public class CustomUserStore : UserStore<ApplicationUser, CustomRole, int,
        CustomUserLogin, CustomUserRole, CustomUserClaim>
    {
        public CustomUserStore(ApplicationDbContext context)
            : base(context)
        {
        }
    }

    public class CustomRoleStore : RoleStore<CustomRole, int, CustomUserRole>
    {
        public CustomRoleStore(ApplicationDbContext context)
            : base(context)
        {
        }
    } 
}

这是什么问题?我该如何解决?

0 个答案:

没有答案