5

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)?

3
  • 2
    i think you're looking for DropCreateDatabaseIfModelChanges Commented Aug 5, 2017 at 7:53
  • 1
    You can use a database initializer as Rise suggests. These are handy for early development and have their own seed methods. Once you get non-seed data you want to preserve you can switch to migrations. Using a null initializer won't create the database objects - it will assume the code model matches the database (dangerous). Commented Aug 5, 2017 at 14:50
  • You can delete the dbo.__MigrationHistory and try. Please see my answer below stackoverflow.com/a/67821254/3057246 Commented Mar 7, 2022 at 7:33

3 Answers 3

6

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)

Sign up to request clarification or add additional context in comments.

Comments

4

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);

2 Comments

The answer to "Update database without migration" is to run –EnableAutomaticMigrations?!?!
"I just wanted to know is there a way to update the database without using migrations (Add-migrations and update-database)?" - this question gave me a notion that the OP wanted to get it done without handling the migration manually..and moreover there was no mention that the OP wanted to take care of migration manually using scripts..anyways, i have updated the answer..thanks for pointing out!
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.