C# 按照实体代码优先的方法设定数据库种子
因此,在我使用向导从现有数据库创建模型后,我的C# 按照实体代码优先的方法设定数据库种子,c#,asp.net,asp.net-mvc,entity-framework,entity-framework-migrations,C#,Asp.net,Asp.net Mvc,Entity Framework,Entity Framework Migrations,因此,在我使用向导从现有数据库创建模型后,我的配置.cs被删除 namespace SnakeGame.Migrations { using System; using System.Data.Entity; using System.Data.Entity.Migrations; using System.Linq; internal sealed class Configuration : DbMigrationsConfiguration<S
配置.cs
被删除
namespace SnakeGame.Migrations
{
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
internal sealed class Configuration : DbMigrationsConfiguration<SnakeGame.Models.ApplicationDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(SnakeGame.Models.ApplicationDbContext context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data. E.g.
//
// context.People.AddOrUpdate(
// p => p.FullName,
// new Person { FullName = "Andrew Peters" },
// new Person { FullName = "Brice Lambson" },
// new Person { FullName = "Rowan Miller" }
// );
//
}
}
}
但是我在context.IPs
、context.BannedIP
s和context.Score
上都有错误。我得到的错误是
SnakeGame.Models.ApplicationDbContext不包含定义
为了
我正在想办法解决这个问题。可以看到my Migrations文件夹的完整代码。我认为我在代码优先迁移中的所有尝试都把我的项目搞砸了。废话。您是否用迁移更新了数据库??我的项目中有一个命令示例
/*
* X DATA CONTEXT
*/
//this command enables migrations
Enable-Migrations -StartUpProjectName X.Web -ProjectName X.DAL.Repository -EnableAutomaticMigrations -ContextTypeName XDataContext -Verbose -MigrationsDirectory:XDataContextMigrations
//this command create a new migration, please set up the name of the DBMigration
Add-Migration [NAME] -StartUpProjectName X.Web -ProjectName X.DAL.Repository -ConfigurationTypeName XUpdateDatabaseInitializer -Verbose
//this command updates the current database
Update-Database -StartUpProjectName X.Web -ProjectName X.DAL.Repository -ConfigurationTypeName XUpdateDatabaseInitializer -Verbose
//this command rollbacks the current Database to Specific Target applying Down() methods
Update-Database –TargetMigration: SpecificTargetMigrationName -StartUpProjectName X.Web -ProjectName X.DAL.Repository -ConfigurationTypeName XUpdateDatabaseInitializer -Verbose
因此,如果您已经更新了数据库,我建议您完全删除当前数据库,并使用Migrations命令重新创建它
希望有帮助。您的实体都是在SnakeDb上下文中定义的,而不是ApplicationDbContext,因此请将种子更改为
protected override void Seed(SnakeGame.Migrations.SnakeDb context)
...
在你进行反向工程之后,你可能想要将东西移动到你希望你的应用程序拥有的任何结构中。对我来说,我将POCO复制到一个单独的“实体”项目中,然后将EF和上下文内容移动到一个“数据”项目中,但也可以将它们移动到不同的文件夹中
第二,由于对数据库进行了反向工程,因此需要进行基线初始迁移。您可以注释掉Up()和Down()代码,也可以通过
Add-Migration InitialCreate –IgnoreChanges
您能显示全部错误吗?您是否将该脚本保存在文件或其他文件中?当您通过Visual Studio构建/编译/部署/任何项目时,是否有办法使其运行?致@Retired_MSFT_Millennizer:我将此脚本保存在靠近迁移目录的文件中,以便在需要时运行所需的命令(添加、更新或回滚)。对Steeve说:我没有意识到他使用了错误的DbContext,所以,别担心,你的答案是对的。更改为
受保护的覆盖无效种子(SnakeDb context)
会产生错误“找不到类型或命名空间'SnakeDb'(你是否缺少using指令或程序集引用)”,是的,你需要使用蛇形游戏;或在上下文名称前加前缀。见编辑后的答案。从长远来看,您可能应该将您的上下文移出Migrations文件夹。
protected override void Seed(SnakeGame.Migrations.SnakeDb context)
...
Add-Migration InitialCreate –IgnoreChanges