-1

I have the following class (inheriting from BaseEntity which just has an int Id property):

public class User : BaseEntity
{
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime Created { get; set; }
    public DateTime? LastLogon { get; set; }
    public string LastIpAddress { get; set; }
    public string Password { get; set; }        
    public string CustomData { get; set; }
    public int FailedLogInAttempts { get; set; }
    public bool LockedOut { get; set; }
    public UserRole Role { get; set; }
}

It is mapped by the following class:

public class UserMap : EntityTypeConfiguration<User>
{
    public UserMap()
    {
        HasKey(t => t.Id);
        Property(t => t.Created);
        Property(t => t.CustomData);
        Property(t => t.Email);
        Property(t => t.FailedLogInAttempts);
        Property(t => t.FirstName);
        Property(t => t.LastIpAddress);            
        Property(t => t.LastLogon).IsOptional();
        Property(t => t.LastName);
        Property(t => t.LockedOut);
        Property(t => t.Password);            
    }
}

Now on occasion when I run the project I find that the table is dropped and recreated. I can tell this because I have seen the table disappear and reappear in SQL Server (by repeatedly spamming a select query on the table for want of a better method!).

I have a custom onModelCreating as I am pulling mappings from external DLLs as well (the user is not from an external DLL). The code for my custom onModelCreating is:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    var typesToRegister = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes())
    .Where(type => !String.IsNullOrEmpty(type.Namespace))
    .Where(type => type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));

    foreach (var type in typesToRegister)
    {
        dynamic configurationInstance = Activator.CreateInstance(type);
        modelBuilder.Configurations.Add(configurationInstance);
    }

    Database.SetInitializer<DataContext>(new MigrateDatabaseToLatestVersion<DataContext, Migrations.Configuration>());            
    base.OnModelCreating(modelBuilder);
}

I have also customised my migration config with the following so that migrations are automatic:

public Configuration()
{           
    AutomaticMigrationsEnabled = true;     
}

I am sure it's down to me doing something stupid but as it doesn't happen every time I clean => rebuild => run the project I am finding it very hard to track down the problem.

To recap:

  • I have a model that has not changed (Including namespaces etc, properties types of the model from classes I have created etc.)
  • Sometimes when I run the project the table this model relates to is dropped and recreated.

I would appreciate if anyone could help me track down where I can begin to look for the root cause of the problem. If you require me to post anymore of my code please let me know.

Thanks.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
John
  • 685
  • 1
  • 6
  • 20
  • Can you post the code for MigrateDatabaseToLatestVersion class? Want to make sure your are not deriving that class from `DropCreateDatabaseAlways` or `DropCreateDatabaseIfModelChanges`. – Dipen Shah Oct 18 '18 at 03:14
  • Hi @DipenShah the class is just the standard `System.Data.Entity.MigrateDatabaseToLatestVersion` class. – John Oct 19 '18 at 12:04
  • Totally forgot that one! Will it be possible for you to create a small project to replicate the issue. I created a small project with class you specified but it is behaving as expected. – Dipen Shah Oct 19 '18 at 14:32
  • Aside from `AutomaticMigrationsEnabled = true;`, have you tried adding `AutomaticMigrationDataLossAllowed = false;`? Do you have a `Seed()` method that you can post? – Andy Refuerzo Oct 21 '18 at 05:49
  • You are checking if table is dropped/recreated by "repeatedly spamming a select query on the table"!!? Why? Do you not have any data in your table? And if you are not loosing the data, then the table was not dropped/recreated. – Hooman Bahreini Oct 21 '18 at 22:01
  • Yes, the data I had in the table disappears. I can also see from the create_date field sys.tables changes. – John Oct 22 '18 at 00:42
  • @AndyRefuerzo At one stage I did have `AutomaticMigrationDataLossAllowed` set to true but even after I changed it to false it still happened. My seed method is just the default empty one. – John Oct 22 '18 at 08:45

1 Answers1

0

You can change

AutomaticMigrationsEnabled = false;

and then, update your model only when you make changes on it using the console pointing to to your entity project:

add-migration [Migrator_Name]
update-database
David Castro
  • 1,773
  • 21
  • 21
  • The problem with this is my models are spread across several assemblies, so it does not pick up the models outside that assembly. Also, I would still get the model doesn't match the DB error? – John Oct 12 '18 at 23:09
  • I haven't worked with that scenario, but you can try updating each model of each assembly one by one and see if it works. – David Castro Oct 15 '18 at 14:47