I have a console application that interacts with a backend database using EF. Core. I have the DBContext that looks as follows.
class HRDepartmentContext : DbContext
{
public DbSet<Employee> Employee { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlite("Data Source=somepath\\employees.db");
}
It's consumed in a class that looks as follows.
class EmployeeManager
{
public CreateEmployee(string firstname, string lastname)
{
using (var db = new EmployeeContext())
{
db.Database.EnsureCreated();
// logic for employee creation
}
}
// other methods for delete/update the employees
public UpdateEmployee(...) {...}
public DeleteEmployee(...) {...}
}
My questions.
a) What is the best way of calling EnsureCreated method as I don't want to call it in the individual Create/Update/Delete Employee methods.
b) What is the best way of handling schema changes scenarios in future versions of the application? Reading the documentation, it does not look like EsureCreated will be able to handle the scenario. Obviously upgrade schema need to be performed without any existing data loss.
UPDATE: I want to avoid using any EF command lines for migration in case of schema changes. I prefer the code to handle it.