问题描述
我正在使用ASP.NET MVC 3项目,它使用Entity Framwork 4.3及其迁移。现在我希望Entity Framework使用我已经有的迁移来为我创建一个数据库。当我尝试运行Update-Database脚本时,它给出以下内容:
更新 - 数据库-Verbose - ProjectName AssemblyWithMigrations -StartUpProjectName WebProjectAssembly
在程序集'/ *中我没有找到迁移配置类型'/ *我的名称的组件* /'。 (在Visual Studio中,可以使用Package Manager Console中的Enable-Migrations命令添加迁移配置)。
但是,当我尝试运行启用迁移
我看到以下内容:
项目AssemblyWithMigrations中已启用迁移。要覆盖现有迁移配置,请使用-Force参数。
所以,问题是EF试图解决当前的迁移版本来更新数据库,我想。但是数据库不存在,它显然是失败的。
问题是如何使用EF迁移创建数据库,如果不存在?或者,使用Nuget控制台做什么正确的方法?
总而言之,我想要什么:
1.运行命令(也许 update-database
),将使用我的web.config文件
创建数据库2.所有迁移将以创建顺序应用于创建的数据库。
谢谢。 :
我意识到这是一个古老的问题,但是什么是... ...
要启用迁移以创建初始数据库,我发现以下功能很好。
- 删除数据库从SQL Server对象资源管理器中
- 从Migrations文件夹中删除所有现有的迁移。
-
在Package-Management-Console中键入添加迁移InitialCreate
[可选,具体取决于您的数据库初始化程序]
- 在Package-Management-Console中,输入update-database
这将为您提供一个单一的迁移脚本,没有数据库使数据库与您当前的模型匹配。
I'm having ASP.NET MVC 3 project that uses Entity Framwork 4.3 and its migrations. Now I want Entity Framework to create a database for me using migrations that I have already.When I trying to run Update-Database script it gives me the following:
Update-Database -Verbose -ProjectName AssemblyWithMigrations -StartUpProjectName WebProjectAssemblyNo 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).
But, when I'm trying to run Enable-Migrations
I see following:
Migrations have already been enabled in project 'AssemblyWithMigrations '. To overwrite the existing migrations configuration, use the -Force parameter.
So, the problem is EF trying to resolve current migration version to update database, I suppose. But database doesn't exist and it obviously fails.
The question is: how to use EF migrations for creating database if it doesn't exist? Or, what is the right way to do that using Nuget console?
In summary, what I want:1. run command (perhaps update-database
) that will create database using my web.config file2. all migrations will be applied on the created database in their creation order.
Thanks. :)
I realise this is an old question, but what the hell....
To enable migrations to create the initial database I've found the following works well.
- Delete your database from within SQL Server Object Explorer
- Delete all your existing migrations from within the Migrations folder.
In Package-Management-Console type "Add-Migration InitialCreate"
[optional, depending on your database initializer]
- In Package-Management-Console type "update-database"
This will give you a single migration script which will take you from "no database" to having a database that matches your current model.
这篇关于如何使用代码首次迁移创建数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!