FullStack
FullStack

Reputation: 187

Update database without migration in Entity Framework

I've created an ASP.NET MVC sample application in Visual Studio. I added two classes like below :

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Price { get; set; }
    public ICollection<Product> Products { get; set; }
}

public class Order
{
    public int Id{ get; set; }
    public Product Product { get; set; }
    public int ProductId { get; set; }
    public DateTime RegisterDate { get; set; }
}

and then I added two DbSet to the main DbContext like this:

 public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public IDbSet<Product> Products { get; set; }
    public IDbSet<Order> Orders { get; set; }

    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

But now when I run project, I get this error :

The model backing the 'ApplicationDbContext' context has changed since the database was created

I just wanted to know is there a way to update the database without using migrations (Add-migrations and update-database)?

Upvotes: 5

Views: 16187

Answers (3)

Vinod Srivastav
Vinod Srivastav

Reputation: 4255

With EF6:

I think what you can do it to have the Database.SetInitializer in your DbContext Class constructor to have the database created on the first run. This I guess you already have, next you can comment it where it says Database.SetInitializer<MyContext>(new CreateDatabaseIfNotExists<MyContext>());

and delete the dbo.__MigrationHistory table created in your database.

Now you can use Alter Table.. to add remove columns in your database table then in your C# Table class to match.

Upvotes: 2

ashin
ashin

Reputation: 2603

You can enable automatic migrations to achieve this. This approach definitely uses code-first migration but you don't need to use Add-Migration command or update-database.

Run Enable-Migrations –EnableAutomaticMigrations from package manager console first to generates a Configuration class as follows:

internal sealed class Configuration : DbMigrationsConfiguration<ApplicationDbContext>
{
  public Configuration()
  {
    AutomaticMigrationsEnabled = true;
  }

  protected override void Seed(ApplicationDbContext context)
  { 
  }
}

Now you could either update the db context's constructor to migrate to latest version as follows:

public ApplicationDbContext()
    : base("DefaultConnection", throwIfV1Schema: false)
{
   Database.SetInitializer(
             new MigrateDatabaseToLatestVersion<ApplicationDbContext, Configuration>());
   this.Database.Initialize(true);
}

Or migrate to the latest version on application start up as follows:

using (var context = new ApplicationDbContext())
 {
    Database.SetInitializer(
              new MigrateDatabaseToLatestVersion<ApplicationDbContext, Configuration>());
    context.Database.Initialize(true);
 }

UPDATE

If you prefer not to use migrations at all, you may consider setting the database initializer to null and handle the db migration manually using scripts

Database.SetInitializer<YourContext>(null);

Upvotes: 4

bubi
bubi

Reputation: 6491

You can update the database structure in the way you want (queries, via DBMS user interface and so on).
If you do so you need to disable configuration check.

Database.SetInitializer<YourContext>(null);

(I insert this in a static constructor in the context)

Upvotes: 6

Related Questions