EF 5代码首次迁移批量SQL数据播种 [英] EF 5 Code First Migration Bulk SQL Data Seeding
本文介绍了EF 5代码首次迁移批量SQL数据播种的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的问题是如何种植已经在我的数据库中的大量数据?
内部密封类配置:MigrationsConfiguration< VoiceLab.Models.EFDataContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(VoiceLab.Models.EFDataContext context)
{
//这里要做的是种植数千或已经在db中的记录
}
}
谢谢,
解决方案
以下是通过在seed方法中提供.sql文件来种植批量数据的方法。
public class AppContextInitializer:CreateDatabaseIfNotExists< AppContext>
{
protected override void Seed(AppContext context)
{
var sqlFiles = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory,* .sql)。OrderBy(x = > x);
foreach(sqlFiles中的字符串文件)
{
context.Database.ExecuteSqlCommand(File.ReadAllText(file));
}
base.Seed(context);
}
}
I am using EF5 with MVC4. The problem is that I have large data in my DB which I already imported from legacy DB. I want to load seed that data when model changes. My question is how can I seed large amount of data that is already in my DB?
internal sealed class Configuration : MigrationsConfiguration<VoiceLab.Models.EFDataContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(VoiceLab.Models.EFDataContext context)
{
//what to do here to seed thousands or records that are already in db
}
}
thanks,
解决方案
Here is how you can seed bulk data by just providing the .sql files in seed method.
public class AppContextInitializer : CreateDatabaseIfNotExists<AppContext>
{
protected override void Seed(AppContext context)
{
var sqlFiles = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.sql").OrderBy(x => x);
foreach (string file in sqlFiles)
{
context.Database.ExecuteSqlCommand(File.ReadAllText(file));
}
base.Seed(context);
}
}
这篇关于EF 5代码首次迁移批量SQL数据播种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文