注册ApplicationUser时出错:使用实体框架'无效列名* _Id'

时间:2018-02-07 19:00:32

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

我遇到了一个我无法找到解决方案的问题。

我正在尝试构建类似于this的Saas月度结算解决方案。我正在使用SaasEcomm git代码向ApplicationUser添加其他字段和功能。请参阅下文我使用SaasEomUser class并添加其他字段和帐单邮寄地址。

public class ApplicationUser : SaasEcomUser //Instead of 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);
        return userIdentity;
    }

    public virtual BillingAddress BillingAddress { get; set; }
}

我得到的错误消息让我觉得实体框架对于适当的ID是多么困惑,但没有任何运气修复它:

Invalid column name 'SaasEcomUser_Id'.

我的代码中没有'SaasEcomUser_Id',这就是我认为它是实体框架的原因。以下是在错误上调用的代码(CreateAsync):

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        model.SubscriptionPlan = "basic_monthly";

        if (ModelState.IsValid)
        {
            var user = new ApplicationUser
            {
                UserName = model.Email,
                Email = model.Email,
                RegistrationDate = DateTime.Now,
                LastLoginTime = DateTime.Now,
                BillingAddress = new BillingAddress()
            };

            var result = await UserManager.CreateAsync(user, model.Password);

        return View(model);
    }

OnModelCreating:

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ApplicationUser>().HasKey<string>(m => m.Id);

        modelBuilder.Entity<ApplicationUser>().Map(m => m.MapInheritedProperties());

        base.OnModelCreating(modelBuilder);
    }

我可能会离开基地,但有没有办法让EF知道主键是Id?还是别的错了?

感谢任何可以提前帮助的人!如果我能提供任何其他代码,请告诉我们!

0 个答案:

没有答案