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.