I can't refresh database model. I am using Asp .net 5.
This is a short example of what I done:
1.Create class person in "person.cs" file, which is inside "Model" directory.
namespace MyWeb.Models
{
public class person
{
public int personID { get; set; }
public string login { get; set; }
public string password { get; set; }
public string name { get; set; }
public string surname { get; set; }
public DateTime dateOfBirth { get; set; }
}
}
2. Create myContext.cs file in Models directory.
namespace MyWeb.Models
{
public class myContext : DbContext
{
public DbSet<person> persons { get; set; }
}
}
3.Create myInitializer.cs inside Models directory
namespace MyWeb.Models
{
public class myInitializer :DropCreateDatabaseIfModelChanges<myContext>
{
protected override void Seed(myContext context)
{
var persons= new List<person>
{
new person{ login = "me", name = "meme", password = "abc", surname = "abc" },
new person{ login = "me2", name = "meme2", password = "abc2", surname = "abc2" }
};
foreach (var p in persons)
{
context.persons.Add(p);
}
context.SaveChanges();
}
}
}
4.Inside Global.asax.cs add following line = Database.SetInitializer(new myInitializer())
namespace MyWeb
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
Database.SetInitializer<myContext>(new myInitializer());
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
After first run everything was working fine, database was created, records was added to database, but when model has been changed database don't reload. I search on the web about this but nothing helps.
How I can refresh database model?
you can use Enable-Migrations
command, then Add-Migration "description of changes"
and finally Update-Database
from the package manager console