- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用MySql.Data.EntityFrameworkCore连接mysql的提供者。现在我想在使用 DropCreateDatabaseAlways
初始化数据库时播种数据,但是当我尝试这样使用时却找不到
Database.SetInitializer<SmContext>(new SMDbInitializer<SmContext>());
还有我的SMDbInitializer
看起来像这样。
private class SMDbInitializer<T> : DropCreateDatabaseAlways<SmContext>
{
protected override void Seed(SmContext context)
{
//Some code
base.Seed(context);
}
}
最佳答案
您可以创建一个 DbInitializer.cs
并调用 dbContext.Database.EnsureDeleted() ;删除现有数据库。
public static class DbInitializer
{
public static void Initialize(MyDbContext context)
{
//Ensures that the database for the context does not exist. If it does not exist, no action is taken.
//If it does exist then the database is deleted.
//Warning: The entire database is deleted an no effort is made to remove just the database objects that are used by the model for this context.
context.Database.EnsureDeleted();
//create the database
context.Database.EnsureCreated();
// Look for any students.
if (context.Students.Any())
{
return; // DB has been seeded
}
var students = new Student[]
{
new Student{FirstMidName="Carson",LastName="Alexander",EnrollmentDate=DateTime.Parse("2005-09-01")},
new Student{FirstMidName="Meredith",LastName="Alonso",EnrollmentDate=DateTime.Parse("2002-09-01")},
new Student{FirstMidName="Arturo",LastName="Anand",EnrollmentDate=DateTime.Parse("2003-09-01")},
new Student{FirstMidName="Gytis",LastName="Barzdukas",EnrollmentDate=DateTime.Parse("2002-09-01")},
};
foreach (Student s in students)
{
context.Students.Add(s);
}
context.SaveChanges();
//Applies any pending migrations for the context to the database.Will create the database if it does not already exist.
//Note that this API is mutually exclusive with DbContext.Database.EnsureCreated().
//EnsureCreated does not use migrations to create the database and therefore the
//database that is created cannot be later updated using migrations.
context.Database.Migrate();
}
}
在 Program.cs 中,修改 Main 方法以在应用程序启动时执行以下操作:
public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args).Build();
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var context = services.GetRequiredService<SchoolContext>();
DbInitializer.Initialize(context);
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred while seeding the database.");
}
}
host.Run();
}
关于c# - 在 MySql.Data.EntityFrameworkCore 中找不到 Database SetInitializer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58207053/
我在 edm 设计器(VS10)中使用“DbContext Entity Generator”作为生成项创建了一个模型。 在生成的 DbContext 子类中,它覆盖了构造函数,因此我不能在其他部分类
我想在一个程序集中隔离我所有的 EF 4.1 实现。 Database.SetInitializer 调用通常放置在 Web 应用程序的 Application_Start 方法中。从我的 DbCon
许多人可能知道为什么我们需要使用下面显示的代码。但是,我想将此逻辑分成多个层,并且不想在我的 Web 层中引用 Entity Framework DLL,因此我最终将此代码放在了我的 DbContex
我在 Entity Framework V4.1 中使用代码优先模式。我遇到的一件看起来很奇怪的事情是,每当您对模型进行任何更改时,数据库都会被删除并创建新的。这非常不整洁,因为每次删除并重新创建数据
canonical answer关于放置 Database.SetInitializer 调用的位置在 Global.asax 中用于 Web 项目。我正在寻找另一种选择。 我们使用 Entity F
我正在开发一个项目,该项目最终可能会有多个 UI 版本/变体,但到目前为止,我的解决方案 Web 中有两个子项目 - 包含带有 ASP.NET MVC 的 Web 界面。服务项目是我定义数据库上下文和
我正在尝试使用 CTP4 预览将 Entity Framework (代码优先)添加到一直使用测试数据运行的 MVC 应用程序。 我目前遇到这个错误: 支持“SchedulerContext”上下文的
现在,在开发中,我在Global.asax.cs文件中具有以下代码,该代码使用我的SeedSampleData方法调用Entity Framework(v4.1)SetInitializer。所有这一
我正在使用 Entity Framework 并在我的上下文中继承自 DbContext。 public class MyContext : DbContext, IMyContext { s
给定连接字符串,我正在尝试编写一种方法来创建数据库并在其上运行迁移。 我需要多个连接,因为我在单独的数据库中记录了审计日志。 我使用类似的代码从 app.config 中获取连接字符串 Configu
使用 EF5.0 运行 MVC4 项目 在开发我的模型时,EF 会在每次模型更改时创建一个新数据库,并使用示例数据进行播种。模型设计现已完成,数据库中填充了实时数据,因此我对负责创建数据库的行进行了注
我创建了两个 DbContext,一个用于应用程序配置,第二个用于日志记录。 原因是,我想在日志记录数据库上设置最大大小,这样它就不会用完所有可用磁盘空间并阻止其他数据库工作。 在我的 global.
我正在使用MySql.Data.EntityFrameworkCore连接mysql的提供者。现在我想在使用 DropCreateDatabaseAlways 初始化数据库时播种数据,但是当我尝试这样
我正在使用 Entity Framework 4.1 中引入的 DbContext 和 Code First API。 数据模型使用基本数据类型,例如string 和DateTime。在某些情况下,我
我几个小时以来一直在寻找这个奇怪的错误,但没有找到任何东西。我有一个非常简单的实体: public class Company { public Guid Id { get; set; }
我已经注释掉了对 Database.SetInitializer<>() 的所有调用在我的 EF Code First 应用程序中。但是,即使没有任何显式调用来创建数据库,我的 Asp.net MVC
我的问题在于缺乏MVC经验。基本上,我在数据库中有两个表-人-报价 我为每个人创建了一个模型、一个 Controller 和一个模型,因此结构如下所示: public class Offer {
我的代码优先数据库运行良好。如果我对数据库上下文进行了更改,则数据库将在下次启动应用程序时更新。但后来我向数据库添加了一些模型,并在重新启动应用程序时收到此错误: Introducing FOREIG
尝试首先使用 EF 4.1 代码进行一些单元测试。我有我的实时数据库(SQL Server)和我的单元测试数据库(Sql CE)。在与 EF、Sql CE 4.0 和事务支持战斗(并失败)后,我决定运
我有一个从抽象基类继承的上下文类 AuditableDbContext : DbContext . AuditableDbContext需要两个参数,一个用于审计员,另一个用于要审计的上下文。 在继承
我是一名优秀的程序员,十分优秀!