Update database without migration in Entity Framework

Viewed 11359

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 Answers

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.

Related