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.
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.
SourceInitial
migration, then edit the migrations history table (i.e.__EFMigrationsHistory
).