C
C
CodeInside2018-03-23 12:53:18
C++ / C#
CodeInside, 2018-03-23 12:53:18

Why is the seed method of the DatabaseInitializer class not called?

DB context

public class EntityDataModel : DbContext, IDataModel
    {
        static EntityDataModel()
        {
            Database.SetInitializer<EntityDataModel>(new DatabaseInitializer());
        }

        public DbSet<Person> Persons { get; set; }
    }

Person.cs
public class Person
    {
        public int Id { get; set; }
        public string Names { get; set; }
        public DateTime DateOfBirth { get; set; }
        public string MaritalStatus { get; set; }
        public string Address { get; set; }
        public string Email { get; set; }
    }

DatabaseInitializer.cs
internal class DatabaseInitializer : DropCreateDatabaseAlways<EntityDataModel>
    {
        protected override void Seed(EntityDataModel db)
        {
            AddPersonsToDb(ref db);
            db.SaveChanges();
        }

        private void AddPersonsToDb(ref EntityDataModel db)
        {
            db.Persons.Add(
                new Person
                {
                    Names = "Лебедев Сергей Вячеславович",
                    DateOfBirth = new System.DateTime(1989, 11, 20),
                    MaritalStatus = "Не женат",
                    Address = "140702, г. Черемуино, ул. Голландская, дом 54, квартира 236",
                    Email = "[email protected]"
                }
                );
        }

The debugger enters the static constructor of the database context. Not in the seed method of the DatabaseInitializer class. When the application starts, the database is not created. It was created with the help of migrations. Why is seed not being called?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
stcmd04236, 2018-03-27
@stcmd04236

You have overridden the Seed. Add at the endbase.Seed(db);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question