I am using entity framework code first approach and have following model and db context.
public class Patient
{
public int Id { get; set; }
public string FirstName { get; set; }
}
public class PatientContext:DbContext
{
public DbSet<Patient> Persons { get; set; }
}
Now to have the model created in database I have to Run Enable Migration and if any changes then have to run Update database using package manager console.
But is there any way I can do that using code. So when ever some one run console application it will create all table schema.
class Program
{
static void Main(string[] args)
{
//Code to Create my tables
//Something similar to enable migration and update database
}
}
I can have a record created inside main app and that will create the table structure, but creating an record to create table structure seems redundant. Also , if there are any mode changes, following code will throw an exception.
Is there a better approach?
static void Main(string[] args)
{
using (PatientContext pcontext = new DatabaseMigApp.PatientContext())
{
pcontext.Patients.Add(new Patient() { FirstName = "Steve",Id = 1});
pcontext.SaveChanges();
}
}