Hello,
public class AppContext: DbContext
{
private readonly string _databasePath;
public AppContext()
{
_databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "App.db");
Database.EnsureCreated();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite($"Filename={_databasePath}");
}
public DbSet<Product> Product{ get; set; }
public DbSet<Category> Category{ get; set; }
}
Initially above code works fine and it will create a database.
public DbSet<Product> Product{ get; set; }
public DbSet<Category> Category{ get; set; }
public DbSet<Customer> Customer{ get; set; }
But if I added a new model then I have to first delete a database and then create a new database like below code. By using this approach I lost my all data.
public AppContext()
{
_databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "App.db");
Database.EnsureDeleted();
Database.EnsureCreated();
}
So, is there any way to add new table sqllite database without deleting existing data.