2

I was working on a Xamarin.Forms application and using EF 6 targeting .net standard 2.0. The application is live on app store with my DBContext class as follows.

public class DatabaseContext : DbContext
{
    private readonly string _databasePath;

    // Hotworks related tables
    public DbSet<User> User { get; set; }
    public DbSet<UserProfile> UserProfile { get; set; }

    public DatabaseContext(string databasePath)
    {
        _databasePath = databasePath;
        Database.EnsureCreated();
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite($"Filename={_databasePath}");    
    }
}

Now, I have to add a new column to the User Table. For that, I have created a .net core console application and generated the migration folder using below commands.

dotnet ef migrations add Initial
dotnet ef update database

Then, I copied the generated Migration folder to my dbContext project and changed the Database.EnsureCreated() to Database.Migration() But, I'm getting SQLite error 1: Table "User" already exists. Any help? Code already in the production having Database.EnsureCreated() but now I have to migrate and update the database but, it is not working and always I'm getting the table already exist error.

4
  • Which database are you using?
    – habib
    Feb 27, 2019 at 5:57
  • @habib Microsoft.EntityFrameworkCore 2.0 Feb 27, 2019 at 6:28
  • 1
    From the official Microsoft EntityFrameowrkCore documentation on DatabaseFacade.EnsureCreated Method : Note that this API does not use migrations to create the database. In addition, the database that is created cannot be later updated using migrations. If you are targeting a relational database and using migrations, you can use the DbContext.Database.Migrate() method to ensure the database is created and all migrations are applied. Source Feb 27, 2019 at 10:12
  • 1
    One way to solve your issue is to manually update your database to be equivalent to the Initial migration, then edit the migrations history table (i.e. __EFMigrationsHistory). Feb 27, 2019 at 19:12

1 Answer 1

0

Here's a safer approach compared to my previous comment

  1. add-migration Initial for the models that is equivalent to the production database (i.e. without the newly added column in User table and other changes that you did after deploying to production).
  2. Manually update the production database __EFMigrationsHistory to include the Initial migration.
  3. add-migration XXX for the updated schema (i.e. new columns, etc).
  4. XXX should then be applied to your production database by the Database.EnsureMigrate() method.
2
  • Hi @Paolo, Thanks for the response. I ran the "dotnet ef migrations add Initial" without adding the new column in the model class and generated the "Migration" folder. After that I'm not able to understand how to update manually the database and add new column to User table. Also, I'm using EF core 2.0 version for Xamarin.Forms so, there is no method named "Database.EnsureMigrate()". Could you please elaborate a little so I can follow the steps and update the database. Feb 28, 2019 at 9:17
  • @MayankShrivastava Try creating a temp database with the Initial migration and also created using Database.EnsureMigrate. examine its __EFMigrationsHistory, the row in that table should be manually inserted to your production database. Feb 28, 2019 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.