i have added the following initializer for my asp.net web application :-
namespace Learning.DAL
{
public class SchoolInitializer : DropCreateDatabaseIfModelChanges<LearningContext>
{
protected override void Seed(LearningContext context)
{
var students = new List<Student>
{
new Student { FirstMidName = "test", LastName = "test2",
EnrollmentDate = DateTime.Parse("2012-01-01") },
//code does here
but i have the following questions that i need some help:-
-
If the application is on production when will the application checks if
IfModelChangesand re-create the Db accordingly? only when i stop and re-run the server, or when a specific page is visited by the first user? -
will dropping and re-creating the database causes the data that are already there to be deleted?
-
if i remove a property from an entity set in my new changes what will happen to its data?
-
if i add a non-nullable property to an exsiting entity set, what will happen when the database is dropped and re-created ?
BR
IIS recycles AppDomains periodically. EF caches its model once per app domain. Hence when your application is recycled and your application uses the context for the first time EF will run your initializer. So at that point if the model is different from the database your
SchoolInitializerwill be executed.Yes. Only the data inserted in your
Seedmethod will be there after the recreation of database.EF detects that the model has been changed and database will be dropped and re-created.
My advice is, database initializers are preferred in development environment. For production it will be better to create the database using a script. This will be useful in change management. You can also look at database migrations feature in EF.