1

Added a new field to RegisterViewModel:

[Required]
[Display(Name = "Name ")]
public string Name { get; set; }

Added in AccountController:

if (ModelState.IsValid)
{
    var user = new ApplicationUser
            {
                UserName = model.Email,
                Email = model.Email,
                Name = model.Name
            };

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

    if (result.Succeeded)
    {
        await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

        return RedirectToAction("Index", "Home");
    }

    AddErrors(result);
}

Now I want create a migration file, using command

add-migration newFieldName

VS generates the file, but it is empty.

public partial class newFieldName : DbMigration
{
    public override void Up()
    {
    }

    public override void Down()
    {
    }
}

Update: ApplicationUser class

public class ApplicationUser : IdentityUser
    {
        [Required]
        public string Name { get; set; }
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

            return userIdentity;
        }
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }
Maksims
  • 167
  • 2
  • 11
  • 1
    I _think_ it's because `RegisterViewModel` is only a view model, it's not a part of your db context and it doesn't get saved in the database. `ApplicationUser` is what gets saved in the database and it seems it already has a `Name` property. – Dan Dumitru Jan 04 '18 at 13:19
  • @DanDumitru Sorry,I forgot to add this class in question. I updated question. – Maksims Jan 04 '18 at 13:26
  • And ApplicationUser is a property in your DbContext (or class that derives from it?) – Duston Jan 04 '18 at 20:11
  • @Duston It's default EF class – Maksims Jan 04 '18 at 20:59
  • So you are trying to extend IdentityUser? See [here](https://stackoverflow.com/questions/40532987/how-to-extend-identityuser-with-custom-property). You should create a baseline migration before you make any changes (`add-migration InitialBaseline -IgnoreChanges`, `update-database`). Then make your model changes and add another migration which would be just the differences. – Steve Greene Jan 05 '18 at 14:59
  • @SteveGreene Your answer help me a lot. Please design it as an answer and I will mark it as correct. – Maksims Mar 08 '18 at 11:27

0 Answers0