Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF6 "No migrations have been applied to the target database."

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!

like image 703
alex440 Avatar asked Feb 05 '14 13:02

alex440


2 Answers

Well, the answer is that somehow during the refactor the namespace of ConfigurationDbContext changed from

demo10.Migrations.ConfigurationDbContext

to

demo10.Migrations.ConfigurationDbContext.ConfigurationDbContext

and the ContextKey column of the __MigrationHistory table should be the ConfigurationDbContext namespace (you can omit the DbContext, like to have

 demo10.Migrations.Configuration

in the table for

demo10.Migrations.ConfigurationDbContext

namespace).

like image 143
alex440 Avatar answered Nov 18 '22 18:11

alex440


Leaving an answer here as I had a similar problem that wasn't solved by this:

I had the password wrong in the app.config I was using against a different environment. Get-Migrations doesn't give you an error about failed credentials, it just tells you "No migrations have been applied to the target database", which is hugely misleading.

Hopefully someone else who comes across this will discover it's as simple as a typo in their connection string!

edit: the irony of typo-ing the word typo in an answer about my typo.

like image 7
Andy_Vulhop Avatar answered Nov 18 '22 16:11

Andy_Vulhop