1

I have an IdentityDbContext like this:

public class MyIdentityContext : IdentityDbContext<AppUser, AppRole, int>
{
   public DbSet<App> Apps { get; set; }
   public DbSet<Team> Teams { get; set; }
   public DbSet<Avatar> Avatars { get; set; }
   public DbSet<Device> Devices { get; set; }
   public DbSet<Subscription> Subscriptions { get; set; }


   public MyIdentityContext(DbContextOptions<MyIdentityContext> options) : base(options)
   {
   }   // ctor
}   // Cls

If I add a property to App, Team Device, etc I can add a migration and update the database just fine.

Example:

public class Team
{
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public ICollection<AppUser> Members { get; set; }

        public string ABD { get; set; } <--NEW PROPERTY
}    // Cls

However if I do the same in my AppUser class:

        public class AppUser : IdentityUser<int>
        {
           public string FirstName { get; set; }
           public string LastName { get; set; }

           /// <summary>
           /// What app is this user connected to.
           /// </summary>
           public string ApplicationName { get; set; }

           /// <summary>
           /// Where does the employee work
           /// </summary>
           public string Company { get; set; }

           /// <summary>
           /// What section of the company does the employee work in.
           /// </summary>
           public string Department { get; set; }
     
           /// <summary>
           /// Maximum device this user can use - 0 means unlimited
           /// </summary>
           public int DeviceLimit { get; set; } = 0;

           /// <summary>
           /// This users avatar
           /// </summary>
           public Avatar Avatar { get; set; }

           /// <summary>
           /// Devices connected to this user
           /// </summary>
           public ICollection<Device> Devices { get; set; }

           /// <summary>
           /// Teams that this user is a member of
           /// </summary>
           public ICollection<Team> Teams { get; set; }

           /// <summary>
           /// Apps that this user is subscribed to
           /// </summary>
           public ICollection<Subscription> Subscriptions { get; set; }

           public string ABD { get; set; } <--NEW PROPERTY (causes error)

           public AppUser()
           { }
}    // Cls

I get the following error:

An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider.

Error: One or more errors occurred. (Invalid column name 'ABD'.)

What am I doing wrong?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ShanieMoonlight
  • 1,623
  • 3
  • 17
  • 28

1 Answers1

3

OK, so I figured out the problem.

I had a SeedUsers method in my Startup.cs and because add-migration runs the whole project first, it hits an exception because of the mismatch between the un-migrated db and my new code.

I just commented out the call to SeedUsers and it worked.

Thanks for the help.

ShanieMoonlight
  • 1,623
  • 3
  • 17
  • 28
  • Ah, good catch, I didn't even think about that. I was wondering why something was trying to utilize a column you wanted to add via a migration, and that definitely makes sense. – Matt U May 31 '21 at 18:48