【问题标题】:Asp.net Identity adding profile data results in errorAsp.net Identity 添加配置文件数据导致错误
【发布时间】:2013-12-18 17:55:01
【问题描述】:

您好,我正在尝试使用新的 ASP.NET 身份框架将一些配置文件信息添加到我的数据库中。我不断收到此错误:

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

这是我目前所拥有的:

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)
    {

    }
}

谁能告诉我我做错了什么?

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-25
      • 2019-09-03
      • 2014-06-08
      • 2016-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多