Extending MVC5 and Entity Framework 6 ApplicationUser causing invalid column error on user registration

asp.net-identity asp.net-mvc-5 c# entity-framework-6

Question

I'm attempting to extend the ApplicationUser class in order to store UserProfile data in a different table. Nevertheless, I'm having difficulties getting this to function and when I call theUserManager.CreateAsync in my AccountController function.

Invalid column name 'UserProfileId'.

class of my application's users

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

Class for my user profile

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; }
}

My initial migration of entity-generated code

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

My registration code for the new user

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

using the automapping configuration

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

NOTE: I've attempted to remove theint UserProfileId and this in turn produced a field referred to asUserProfile_Id It was problematic in and of itself.

1
4
1/3/2014 5:16:04 AM

Accepted Answer

resolved it Nothing mentioned in response to my question has anything to do with the issue. It turned out that the erroneous DBContext was created and placed into the AccountController as a result of my use of Dependency Injection.

What seems to be occurring is that the UserStore was created using its default constructor because I did not have a DbContext mapped in my AutoFaq configuration. And it appears that in this instance, constructing an IdentityContext is preferable than using the DataContext class from my application. It failed on UserProfileId because this context did not have the new UserProfile attribute.

By including a specific constructor argument in my AutoFaq setup, I was able to resolve issue.

        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>>();    
3
1/4/2014 10:08:52 AM

Popular Answer

Just another chance to help people like myself that had the right constructor hit. In the database that was registered with my startup, I had a technique for creating seed data. The same issue was produced by cs (Core 2) when it attempted to query the table I was trying to extend. I suppose it made sense when I found it. I was able to solve the problem by commenting out the lines and running add-migration at the same time.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow