locked
Extend ApplicationUser : IdentityUser with Additional Properties and Existing Data RRS feed

  • Question

  • User-374939276 posted

    I am trying to extend the ApplicationUser with additional properties. There is data I don't want to lose in the database.

    I created this: public virtual Address MyUserInfo { get; set; } and the public class Address. 

    The Address table already exists in the database with existing data.

    However, when I run the code I get this message: The model backing the 'ApplicationDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database 

     public class ApplicationUser : IdentityUser
        {
            public virtual Address Address  { get; set; }
    
            public ClaimsIdentity GenerateUserIdentity(ApplicationUserManager manager)
            {
                // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
                var userIdentity = manager.CreateIdentity(this, DefaultAuthenticationTypes.ApplicationCookie);
                // Add custom user claims here
                return userIdentity;
            }
    
            public Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
            {
                return Task.FromResult(GenerateUserIdentity(manager));
            }
    
            
        }

    public class Address
    {
    public int ID { get; set; }
    public string PhoneNumber { get; set; }
    public string Address1 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string UserID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Nullable<bool> ReceiveNewsletter { get; set; }
    public Nullable<bool> Reseller { get; set; }
    public string ResellerNumber { get; set; }
    public string PhoneNumberSecondary { get; set; }
    public bool ApprovedForPayPal { get; set; }
    public bool BrainTreePayment { get; set; }
    }

    Wednesday, January 9, 2019 8:20 AM

All replies

  • Wednesday, January 9, 2019 2:36 PM
  • User-1174608757 posted

    Hi warrenkc2,

    According to your description, you have created Address to save your additional properties , so the table of Address in database should be created first  else it will show the message.

    To solve the problem, you could delete  the record created in the __MigrationHistory table. EF will  use the migration tool when you need to update your database  but you had already done by yourself.It writes as below :

    DELETE FROM [dbo].[__MigrationHistory]

    Here is the link ,I hope it could help you.

    https://stackoverflow.com/questions/22408608/the-model-backing-the-applicationdbcontext-context-has-changed-since-the-datab

    Best Regards

    Wei Zhang 

    Thursday, January 10, 2019 8:13 AM
  • User-374939276 posted

    Thank you for your excellent answers. However, I still cannot figure out how to do what I need to without using the migrations tool.

    I need to be able to work with an existing table and data without having to try to allow the migrations to change it or delete the data.

    So, I will be just using dapper to handle the additional information tied to a user id in the database.

    Thursday, January 10, 2019 11:57 AM
  • User-2054057000 posted

    After you have Added Custom User Properties in Identity Membership System you will need to run migrations so that the Identity database adds these new properties to the table.

    The migrations commands to run are:

    PM> dotnet ef migrations add MyCommand1

    PM> dotnet ef database update

    This will solve your problem.

    When you run these migrations your previous data will remain unchanged.

    Thanks & Regards

    Sunday, January 13, 2019 10:27 AM