扩展MVC5和实体框架6 ApplicationUser导致用户注册无效列错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了扩展MVC5和实体框架6 ApplicationUser导致用户注册无效列错误相关的知识,希望对你有一定的参考价值。

我正在尝试扩展ApplicationUser类,以便我可以在单独的表中包含UserProfile信息。但是,当我在AccountController中调用UserManager.CreateAsync方法时,我无法解决此问题,并且遇到异常。

列名称“UserProfileId”无效。

我的应用程序用户类

public class ApplicationUser : IdentityUser
{
    [ForeignKey("UserProfileId")]
    public virtual UserProfile UserProfile { get; set; }
    public int UserProfileId { get; set; }
}

我的用户档案类

public class UserProfile
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Required, MaxLength(200)]
    public string EmailAddress { get; set; }

    [Required, MaxLength(50)]
    public string FirstName { get; set; }

    [Required, MaxLength(50)]
    public string LastName { get; set; }
}

我的第一个实体生成了代码迁移

CreateTable(
    "dbo.AspNetUsers",
    c => new
        {
            Id = c.String(nullable: false, maxLength: 128),
            UserName = c.String(),
            PasswordHash = c.String(),
            SecurityStamp = c.String(),
            UserProfileId = c.Int(),
            Discriminator = c.String(nullable: false, maxLength: 128),
        })
    .PrimaryKey(t => t.Id)
    .ForeignKey("dbo.UserProfile", t => t.UserProfileId, cascadeDelete: true)
    .Index(t => t.UserProfileId);

我在注册时创建新用户的代码

var user = Mapper.Map<ApplicationUser>(model);
var result = await UserManager.CreateAsync(user, model.Password);

使用Automapping配置

Mapper.CreateMap<RegisterViewModel, ApplicationUser>()
    .ForMember(dest => dest.UserProfile, opt => opt.MapFrom(v => Mapper.Map<RegisterViewModel, Data.Model.UserProfile>(v)));

Mapper.CreateMap<RegisterViewModel, Data.Model.UserProfile>()
    .ForMember(dest => dest.FirstName, opt => opt.MapFrom(v => v.FirstName))
    .ForMember(dest => dest.LastName, opt => opt.MapFrom(v => v.LastName))
    .ForMember(dest => dest.EmailAddress, opt => opt.MapFrom(v => v.EmailAddress));

注意:我已经尝试删除int UserProfileId,而是创建了一个名为UserProfile_Id的字段,它本身就引起了问题。

答案

解决了它。问题与我的问题中发布的任何内容无关。它最终是因为我使用了依赖注入导致错误的DBContext被创建并插入到AccountController中。

似乎正在发生的是因为我的AutoFaq设置中没有映射DbContext,用于创建UserStore的构造函数是默认设置。在这种情况下,似乎意味着创建IdentityContext而不是我的应用程序DataContext类。但是,此上下文没有新的UserProfile属性,因此它在UserProfileId上出错。

我通过在AutoFaq设置中添加特定的构造函数参数来修复此问题。

        builder.RegisterType<Data.Model.DataContext>()
            .As<IDataContext>()
            .InstancePerHttpRequest();

        // Added this here to ensure the context passed in is my DataContext
        builder.RegisterType<UserStore<ApplicationUser>>()
               .As<IUserStore<ApplicationUser>>()
               .WithParameter((pi, ctx) => { return pi.Name == "context"; },
                              (pi, ctx) => { return ctx.Resolve<IDataContext>(); }
                          );

        builder.RegisterType<UserManager<ApplicationUser>>().As<UserManager<ApplicationUser>>();    
另一答案

谢谢,接受的答案帮助了我。对于任何使用Ninject的人来说,这是我最终得到的代码

Bind<IUserStore<ApplicationUser>>().ToMethod(
   ctx =>
   {
     var dbContext = ApplicationDbContext.Create();
     return new UserStore<ApplicationUser>(dbContext);
   }
);

Bind<UserManager<ApplicationUser>>().ToSelf();
另一答案

只是另一种可能性来拯救那些像我一样被正确的构造函数击中的人。我有一个方法在我的startup.cs(Core 2)中注册的数据库中创建种子数据,这是查询我试图扩展的表并导致相同的错误。当我发现它时,我觉得很有意义。在我运行add-migration时评论这些行足以让我解决这个问题。

以上是关于扩展MVC5和实体框架6 ApplicationUser导致用户注册无效列错误的主要内容,如果未能解决你的问题,请参考以下文章

如何在 MVC5 中访问 Kendo UI 网格内的实体框架域对象

实体框架 6 不保存更改

获取实体框架 6 在其下面的 SELECT 语句中使用 NOLOCK

ASP.NET MVC5+EF6搭建三层实例

带有 MVC 5 和实体框架的 jQuery 数据表

有没有办法在 lambda 表达式树中使用“动态”?