In an mvc5 application, I created a CodeFirst data model, and performed several migrations on it.
I then refactored my project, and moved all the data/migrations classes to a new project, which is referenced by my presentation tier.
The dbcontext successfully connects and performs read/write operations on the DB after the project change.
When I made a minor change to the model and ran add-migration
, the EF created a migration with the code to create the database from the beginning, like it didn't "see" the existing tables.
Of course, when i ran
Get-Migrations -ConfigurationTypeName ConfigurationDbContext
i got
No migrations have been applied to the target database.
The __MigrationHistory
in the DB is intact and the migrations/configuration classes namespaces didn't change. Also, apparently, the ConnectionString is OK, otherwise he would have trouble working with the db and i would get the "The model backing the context has changed since the database was created" or similar error.
EDIT:
As suggested in the comment, I specify the exact connection string in the DbContext constructor, and not the connectionstirng name in web.config, as it was in the original mvc project.
Still no history/changes in the db when running Get-Migrations and update-databse.
when I run Get-Migrations -ConfigurationTypeName My_Namespace.Migrations.ConfigurationDbContext.ConfigurationDbContext
I get
No migrations have been applied to the target database.
If I try to specify the connection string
Get-Migrations -ConfigurationTypeName My_Namespace.Migrations.ConfigurationDbContext.ConfigurationDbContext -ConnectionString "Server=my_server;Initial Catalog=my_catalog;User Id=my_user;Password=my_pass" -ConnectionProviderName="System.Data.SqlClient" -verbose -debug
the PM gets stuck on >>
sign, until I restart or clear the window...
if I omit the ConnectionProviderName="System.Data.SqlClient"
, the console asks me to enter it, and after I enter, it shows connection to the correct db,
Target database is: 'my_catalog (DataSource: my_server Provider: System.Data.SqlClient, Origin: Explicit)
but still no migrations...
No migrations have been applied to the target database.
Why can it be and what can be done to further investigate/resolve this issue?
EDIT 2:
the constructor of my dbcontext is simple:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base(
"Server=my_server_name;Initial Catalog=my_db_name;UserId=my_username;Password=my_password"
)
{}
public DbSet<Model1> Model1Entities { get; set; }
public DbSet<Model2> Model2Entities { get; set; }
public DbSet<Model3> Model3Entities { get; set; }
}
it is based on IdentityDbContext as in the template mvc5 applicaiton, because I didn't want to create a different dbcontext for it.
and the ConfigurationDbContext is automatic
internal sealed class ConfigurationDbContext : DbMigrationsConfiguration<ApplicationDbContext>
{
public ConfigurationDbContext()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(ApplicationDbContext context)
{
}
}
Thanks!