c# - Can't refresh database model -
i can't refresh database model. using asp .net 5.
this short example of done:
1.create class person in "person.cs" file, 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 working fine, database created, records added database, when model has been changed database don't reload. search on web nothing helps.
how can refresh database model?
you can use enable-migrations
command, add-migration "description of changes"
, update-database
package manager console
Comments
Post a Comment