Seed method not called, Entity Framework 6

前端 未结 7 794
我在风中等你
我在风中等你 2020-12-28 18:29

I have a DatabaseInitializer class

public class DatabaseInitializer : CreateDatabaseIfNotExists
{
    protected override          


        
相关标签:
7条回答
  • 2020-12-28 18:47

    My seed was not being executed either. However, it was because I added a column to a model that I had no intention of using in my actual database and forgot to use the [NotMapped] annotation.

        [NotMapped]
        public string Pair { get; set; }
    

    There was no error message relating to this being the cause at all. Just a null reference to my repository obj when I tried to query data that should have been there.

    0 讨论(0)
  • 2020-12-28 18:48

    I had this issue and the problem was my Context constructor did not use the same name as in my web.config.

    0 讨论(0)
  • 2020-12-28 18:48

    If you are using Code-First then you can populate the data when the application runs for the first time.

    • Create a DbInitializer

      public class MyDbInitializer : IDatabaseInitializer<MyDbContext>
      {
         public void InitializeDatabase(MyDbContext context)
         {
            if (context.Database.Exists())
            {
              if (!context.Database.CompatibleWithModel(true))
              {
                  context.Database.Delete();
              }
            }
            context.Database.Create();
      
            User myUser = new User()
            {
              Email = "a@b.com",
              Password = "secure-password"
            };
            context.Users.AddOrUpdate<User>(p => p.Email, myUser);
            context.SaveChanges();
         }
      }
      
    • Register this DbInitializer in your Global.asax.cs Application_Start method

      Database.SetInitializer(new My.namespace.MyDbInitializer());
      
    0 讨论(0)
  • 2020-12-28 18:56

    This can happen if your Update-Database command does not run successfully, and this does not necessarily mean that it errors out. There might be changes that EF recognizes as "outstanding" that need to be added to a migration.

    Try calling "Add-Migration {migrationNameHere}" and then try "Update-Database" again.

    0 讨论(0)
  • 2020-12-28 19:02

    The only way I could get this to work was to call the seed method myself

    Here are the methods for my DatabaseContext class

     public DatabaseContext() : base("DatabaseContext")
     {
        InitializeDatabase();
     }
    
     public DatabaseContext(string connectionString) : base(connectionString)
     {
         Database.Connection.ConnectionString = connectionString;
         InitializeDatabase();
     }
    
     protected override void OnModelCreating(DbModelBuilder modelBuilder)
     {
         modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
     }
    

    Here I changed my InitializeDatabase method from

    private void InitializeDatabase()
    {
        Database.SetInitializer(new DatabaseInitializer());
        if (!Database.Exists())
        {
            Database.Initialize(true);
        }            
    }
    

    to

    protected virtual void InitializeDatabase()
    {
        if (!Database.Exists())
        {
            Database.Initialize(true);
            new DatabaseInitializer().Seed(this);
        }            
    }
    
    0 讨论(0)
  • 2020-12-28 19:06

    You need to call Update-Database from the Package Manager Console.

    0 讨论(0)
提交回复
热议问题