实体框架无法识别自定义标识属性

时间:2016-11-11 02:56:06

标签: c# entity-framework asp.net-web-api

我已经设置了这样的ApplicationUser类型:

public class BusinessAccount : IdentityUser
{
    [Required]
    [MaxLength(100)]
    public string RepFirstName { get; set; }
    [Required]
    [MaxLength(100)]
    public string RepLastName { get; set; }
    [Required]
    public string Title { get; set; }
    [Required]
    public DateTime JoinDate { get; set; }
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<BusinessAccount> manager, string authenticationType)
    {
        var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
        // Add custom user claims here
        return userIdentity;
    }
}

我也有这个DbContext:

public class AleHubDbContext :IdentityDbContext<BusinessAccount>
    {
        public AleHubDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
            Configuration.ProxyCreationEnabled = false;
            Configuration.LazyLoadingEnabled = false;
        }

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

    }

两者都在同一名称空间中。但是,当我运行迁移时,没有出现上面定义的自定义属性。系统只是使用样板文件支持一个普通的IdentityUser样式表结构。好像EF甚至没有“看到”我的BusinessAccount实体。是否有任何其他连接可能导致问题?感谢。

版本:

Id                                  Versions                                 ProjectName                                                                        
--                                  --------                                 -----------                                                                        
EntityFramework                     {6.1.3}                                  AleHub.API                                                                         
Microsoft.AspNet.Identity.Core      {2.2.1}                                  AleHub.API                                                                         
Microsoft.AspNet.Identity.Entity... {2.2.1}                                  AleHub.API                                                                         
Microsoft.AspNet.Identity.Owin      {2.2.1}                                  AleHub.API                                                                         
Microsoft.AspNet.WebApi             {5.2.3}                                  AleHub.API                                                                         
Microsoft.AspNet.WebApi.Client      {5.2.3}                                  AleHub.API                                                                         
Microsoft.AspNet.WebApi.Core        {5.2.3}                                  AleHub.API                                                                         
Microsoft.AspNet.WebApi.Owin        {5.2.3}                                  AleHub.API                                                                         
Microsoft.AspNet.WebApi.OwinSelf... {5.2.3}                                  AleHub.API                                                                         
Microsoft.AspNet.WebApi.WebHost     {5.2.3}                                  AleHub.API                                                                         
Microsoft.CodeDom.Providers.DotN... {1.0.0}                                  AleHub.API                                                                         
Microsoft.Net.Compilers             {1.0.0}                                  AleHub.API                                                                         
Microsoft.Owin                      {3.0.1}                                  AleHub.API                                                                         
Microsoft.Owin.Host.HttpListener    {2.0.2}                                  AleHub.API                                                                         
Microsoft.Owin.Hosting              {2.0.2}                                  AleHub.API                                                                         
Microsoft.Owin.Security             {3.0.1}                                  AleHub.API                                                                         
Microsoft.Owin.Security.Cookies     {3.0.1}                                  AleHub.API                                                                         
Microsoft.Owin.Security.OAuth       {3.0.1}                                  AleHub.API                                                                         
Newtonsoft.Json                     {6.0.4}                                  AleHub.API                                                                         
Owin                                {1.0}                                    AleHub.API                                                                         
Antlr                               {3.4.1.9004}       

初步迁移......看不到自定义道具!  public partial class InitialCreate:DbMigration     {         public override void Up()         {             CREATETABLE(                 “dbo.AspNetRoles”                 c =&gt;新                     {                         Id = c.String(nullable:false,maxLength:128),                         Name = c.String(nullable:false,maxLength:256),                     })                 .PrimaryKey(t =&gt; t.Id)                 .Index(t =&gt; t.Name,unique:true,name:“RoleNameIndex”);

        CreateTable(
            "dbo.AspNetUserRoles",
            c => new
                {
                    UserId = c.String(nullable: false, maxLength: 128),
                    RoleId = c.String(nullable: false, maxLength: 128),
                })
            .PrimaryKey(t => new { t.UserId, t.RoleId })
            .ForeignKey("dbo.AspNetRoles", t => t.RoleId, cascadeDelete: true)
            .ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
            .Index(t => t.UserId)
            .Index(t => t.RoleId);

        CreateTable(
            "dbo.AspNetUsers",
            c => new
                {
                    Id = c.String(nullable: false, maxLength: 128),
                    Email = c.String(maxLength: 256),
                    EmailConfirmed = c.Boolean(nullable: false),
                    PasswordHash = c.String(),
                    SecurityStamp = c.String(),
                    PhoneNumber = c.String(),
                    PhoneNumberConfirmed = c.Boolean(nullable: false),
                    TwoFactorEnabled = c.Boolean(nullable: false),
                    LockoutEndDateUtc = c.DateTime(),
                    LockoutEnabled = c.Boolean(nullable: false),
                    AccessFailedCount = c.Int(nullable: false),
                    UserName = c.String(nullable: false, maxLength: 256),
                })
            .PrimaryKey(t => t.Id)
            .Index(t => t.UserName, unique: true, name: "UserNameIndex");

        CreateTable(
            "dbo.AspNetUserClaims",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    UserId = c.String(nullable: false, maxLength: 128),
                    ClaimType = c.String(),
                    ClaimValue = c.String(),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
            .Index(t => t.UserId);

        CreateTable(
            "dbo.AspNetUserLogins",
            c => new
                {
                    LoginProvider = c.String(nullable: false, maxLength: 128),
                    ProviderKey = c.String(nullable: false, maxLength: 128),
                    UserId = c.String(nullable: false, maxLength: 128),
                })
            .PrimaryKey(t => new { t.LoginProvider, t.ProviderKey, t.UserId })
            .ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
            .Index(t => t.UserId);

    }

    public override void Down()
    {
        DropForeignKey("dbo.AspNetUserRoles", "UserId", "dbo.AspNetUsers");
        DropForeignKey("dbo.AspNetUserLogins", "UserId", "dbo.AspNetUsers");
        DropForeignKey("dbo.AspNetUserClaims", "UserId", "dbo.AspNetUsers");
        DropForeignKey("dbo.AspNetUserRoles", "RoleId", "dbo.AspNetRoles");
        DropIndex("dbo.AspNetUserLogins", new[] { "UserId" });
        DropIndex("dbo.AspNetUserClaims", new[] { "UserId" });
        DropIndex("dbo.AspNetUsers", "UserNameIndex");
        DropIndex("dbo.AspNetUserRoles", new[] { "RoleId" });
        DropIndex("dbo.AspNetUserRoles", new[] { "UserId" });
        DropIndex("dbo.AspNetRoles", "RoleNameIndex");
        DropTable("dbo.AspNetUserLogins");
        DropTable("dbo.AspNetUserClaims");
        DropTable("dbo.AspNetUsers");
        DropTable("dbo.AspNetUserRoles");
        DropTable("dbo.AspNetRoles");
    }
}

}

0 个答案:

没有答案
相关问题