[英]Seed method not called, Entity Framework 6
I have a DatabaseInitializer
class我有一个
DatabaseInitializer
类
public class DatabaseInitializer : CreateDatabaseIfNotExists<DatabaseContext>
{
protected override void Seed
(
DatabaseContext databaseContext
)
{
// Seed the hash methods.
var defaultHashMethod = new HashMethod
{
Description = "Default",
CreateDate = DateTime.Now
};
databaseContext.HashMethod.Add(defaultHashMethod);
databaseContext.SaveChanges();
}
}
In my DatabaseContext class I set the initializer在我的 DatabaseContext 类中,我设置了初始化程序
public DatabaseContext() : base("DatabaseContext")
{
InitializeDatabase();
}
private void InitializeDatabase()
{
Database.SetInitializer(new DatabaseInitializer());
if (!Database.Exists())
{
Database.Initialize(true);
}
}
As far as I can understand the seed method is only invoked once you perform an operation such as a query.据我所知,只有在执行诸如查询之类的操作时才会调用种子方法。 My database is created successfully and I'm querying the table, but the seed method is never called.
我的数据库已成功创建,我正在查询表,但从未调用过种子方法。
Update:更新:
It seems like the problem is caused because of a class that is inheriting
from my DatabaseContext
class, when using this class to perform database operations, the seed method is not called.问题似乎是由于从我的
DatabaseContext
类inheriting
的类引起的,使用此类执行数据库操作时,不会调用种子方法。 When using my DatabaseContext
class, everything works as expected使用我的
DatabaseContext
类时,一切都按预期工作
public DbSet<TestEntity> TestEntity { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
您需要从Package Manager Console
调用Update-Database
。
The only way I could get this to work was to call the seed method myself我能让这个工作的唯一方法是自己调用种子方法
Here are the methods for my DatabaseContext class这是我的 DatabaseContext 类的方法
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在这里我改变了我的 InitializeDatabase 方法
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);
}
}
to get Seed
method to be called when you are not using AutomaticMigration
, you should use MigrateDatabaseToLatestVersion
initializer for your code-first database.要在不使用
AutomaticMigration
时调用Seed
方法,您应该为代码优先数据库使用MigrateDatabaseToLatestVersion
初始值设定项。
like this:像这样:
Database.SetInitializer(new MigrateDatabaseToLatestVersion<YourContext,YourConfiguration>());
this way, Seed
method will be called every time the migration is done successfully.这样,每次迁移成功完成时都会调用
Seed
方法。
This can happen if your Update-Database command does not run successfully, and this does not necessarily mean that it errors out.如果您的 Update-Database 命令没有成功运行,就会发生这种情况,这并不一定意味着它出错了。 There might be changes that EF recognizes as "outstanding" that need to be added to a migration.
可能存在 EF 认为需要添加到迁移中的“未完成”更改。
Try calling "Add-Migration {migrationNameHere}" and then try "Update-Database" again.尝试调用“Add-Migration {migrationNameHere}”,然后再次尝试“Update-Database”。
我遇到了这个问题,问题是我的 Context 构造函数没有使用与 web.config 中相同的名称。
If you are using Code-First then you can populate the data when the application runs for the first time.如果您使用的是 Code-First,那么您可以在应用程序第一次运行时填充数据。
Create a DbInitializer创建一个 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在 Global.asax.cs Application_Start 方法中注册这个 DbInitializer
Database.SetInitializer(new My.namespace.MyDbInitializer());
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] 注释。
[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.
当我尝试查询本应存在的数据时,只是对我的存储库 obj 的空引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.