添加配置文件数据的Asp.net标识会导致错误

时间:2013-12-18 17:55:02

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

您好我正在尝试使用新的ASP.NET Identity Framework向我的数据库添加一些配置文件信息。我不断收到此错误:

  

“实体类型用户不是当前上下文模型的一部分。”

这是我到目前为止所做的:

private UserManager<User> UserManager
{
        get { return userManager ?? (userManager = new UserManager<User>(UserStore)); }
}
.............
   var user = new User
        {
            UserName = applicationUser.EmailAddress,
            Password = applicationUser.Password,
            BirthDate = applicationUser.BirthDate,
            FirstName = applicationUser.FirstName,
            Gender = applicationUser.Gender,
            IsTermsAndConditionChecked = applicationUser.IsTermsAndConditionChecked,
            LastName = applicationUser.LastName
        };

var identityResult = UserManager.Create(user, user.Password);   

..............

public class User : IdentityUser
{
    public string Password { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public DateTime? BirthDate { get; set; }

    public int Gender { get; set; }

    public bool IsTermsAndConditionChecked { get; set; }
}


public class DbContext : IdentityDbContext<IdentityUser> , IDbContext
{
    public DbContext()
        : base("CodeArtConnectionString")
    {

    }
}

public class AppUserStore : UserStore<User>
{
    private static DbContext dbContext;
    private static IDependencyContainerWrapper dependencyContainer;
    private static IDependencyContainerWrapper DependencyContainer
    {
        get
        {
            return dependencyContainer ?? (dependencyContainer =
                       HttpContextWrapper.Application[GeneralConstants.DEPENDENCY_CONTAINER_KEY] as IDependencyContainerWrapper);
        }
    }

    public static DbContext DbContext
    {
        get
        {
            return dbContext ?? (dbContext = DependencyContainer.Resolve<IDbContext>() as DbContext);
        }
    }

    public AppUserStore() : base(DbContext)
    {

    }
}

任何人都可以告诉我我做错了什么吗?

1 个答案:

答案 0 :(得分:1)

我认为您需要在DbContext类中使用User而不是IdentityUser,如下所示:

public class DbContext : IdentityDbContext<User> , IDbContext
{
    public DbContext()
        : base("CodeArtConnectionString")
    {

    }
}