本文介绍了种子法不叫,实体框架6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DatabaseInitializer

 公共类DatabaseInitializer:CreateDatabaseIfNotExists< ; DatabaseContext> 
{
保护覆盖无效种子

DatabaseContext databaseContext

{
//种子的哈希方法。
变种defaultHashMethod =新HashMethod
{
说明=默认,
CREATEDATE = DateTime.Now
};

databaseContext.HashMethod.Add(defaultHashMethod);

databaseContext.SaveChanges();
}
}

在我DatabaseContext I类设置初始化

 公共DatabaseContext():基地(DatabaseContext)
{
InitializeDatabase();
}

私人无效InitializeDatabase()
{
Database.SetInitializer(新DatabaseInitializer());
如果
{
Database.Initialize(真)(Database.Exists()!);
}
}



据我可以理解种子法只一旦执行操作,如查询调用。我的数据库创建成功,我查询的表,但种子方法不会被调用。



更新:



这似乎是问题,因为一类就是继承引起的从我 DatabaseContext 类,使用这个类来执行数据库操作时,种子法不叫。当使用我的 DatabaseContext 类,一切正常。



 公共DbSet< TestEntity> TestEntity {搞定;组; } 

保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
{
modelBuilder.Conventions.Remove< PluralizingTableNameConvention>();
}


解决方案

只有我能得到的方式这个工作是调用种方法自己



下面是我的DatabaseContext类

公共DatabaseContext():基地(DatabaseContext)
{
InitializeDatabase();
}

公共DatabaseContext(字符串的connectionString):基地(的connectionString)
{
Database.Connection.ConnectionString =的connectionString;
InitializeDatabase();
}

保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
{
modelBuilder.Conventions.Remove< PluralizingTableNameConvention>();
}

下面我从


$改变了我的InitializeDatabase方法b $ b

 私人无效InitializeDatabase()
{
Database.SetInitializer(新DatabaseInitializer());
如果
{
Database.Initialize(真)(Database.Exists()!);
}
}



 受保护的虚拟无效InitializeDatabase()
{
如果(!Database.Exists())
{
Database.Initialize (真正);
新DatabaseInitializer()种子(本);
}
}


I have a DatabaseInitializer class

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

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. When using my DatabaseContext class, everything works as expected

public DbSet<TestEntity> TestEntity { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
解决方案

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);
    }            
}

这篇关于种子法不叫,实体框架6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 21:41