我有一个使用 Entity Framework 4.3 及其迁移的 ASP.NET MVC 3 项目。现在我希望 Entity Framework 使用我已经拥有的迁移为我创建一个数据库。
当我尝试运行 Update-Database 脚本时,它给了我以下信息:
Update-Database -Verbose -ProjectName AssemblyWithMigrations -StartUpProjectName WebProjectAssembly
No migrations configuration type was found in the assembly '/* my name of assembly */'. (In Visual Studio you can use the Enable-Migrations command from Package Manager Console to add a migrations configuration).
但是,当我尝试运行时 Enable-Migrations
我看到以下内容:
Migrations have already been enabled in project 'AssemblyWithMigrations '. To overwrite the existing migrations configuration, use the -Force parameter.
所以,问题是 EF 试图解决当前的迁移版本以更新数据库,我想。但是数据库不存在,它显然失败了。
问题是:如果数据库不存在,如何使用 EF 迁移来创建数据库?或者,使用 Nuget 控制台执行此操作的正确方法是什么?
总之,我想要的是:
1. 运行命令(可能是 update-database
)将使用我的 web.config 文件创建数据库
2. 所有迁移都将按其创建顺序应用于创建的数据库。
谢谢。 :)
最佳答案
我意识到这是一个老问题,但到底是什么......
为了启用迁移以创建初始数据库,我发现以下方法很有效。
[可选,取决于您的数据库初始化程序]
这将为您提供一个单独的迁移脚本,它将带您从“没有数据库”到拥有与您当前模型匹配的数据库。
关于entity-framework - 如何使用代码优先迁移创建数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10718648/