问题描述
我有与我的ASP.Net MVC 5项目中的状态不好,用code-一是实体框架。我不关心丢失数据,我只是希望能够从头开始,重新创建数据库,并开始使用code-首先迁移。
I have got into a bad state with my ASP.Net MVC 5 project, using Code-First Entity Framework. I don't care about losing data, I just want to be able to start fresh, recreate the database and start using Code-First migrations.
目前我的状态,每一个试图更新数据库内的异常结果被抛出,或收到错误消息。另外,网站无法正确地访问数据库。我怎样才能消灭所有迁移,重新创建数据库,并从头开始,而无需创建一个新的项目?换句话说,我要保持我的code,但删除数据库。
Currently I am in a state where every attempt to Update-Database results in an exception being thrown or getting an error message. Also the website can't access the database correctly. How can I wipe all migrations, re-create the database and start from scratch without having to create a new project? In other words, I want to keep my code but drop the database.
后来我也希望得到同步部署数据库(Azure上的SQL Server)。同样,我不介意删除所有数据 - 我只是想获得它的工作。
Later I will also want to get the deployment database (SQL Server on Azure) in sync. Again, I don't mind dropping all the data - I just want to get it working.
请提供任何操作方法步骤,回到原始状态。大部分AP preciated。
Please provide any how-to steps to get back to a clean state. Much appreciated.
推荐答案
请按照下面的步骤:
1)首先进入服务器资源管理器在Visual Studio中,检查该项目的密度纤维板数据连接连接,如果是这样,右键单击并删除。
1) First go to Server Explorer in Visual Studio, check if the ".mdf" Data Connections for this project are connected, if so, right click and delete.
2)进入Solution Explorer中,单击显示所有文件图标。
2 )Go to Solution Explorer, click show All Files icon.
3)进入App_Data文件,右击并删除所有密度纤维板文件为这个项目。
3) Go to App_Data, right click and delete all ".mdf" files for this project.
4)点鼠标右键删除迁移文件夹并删除。
4) Delete Migrations folder by right click and delete.
5)转到SQL Server Management Studio中,确保DB这个项目是不存在的,否则将其删除。
5) Go to SQL Server Management Studio, make sure the DB for this project is not there, otherwise delete it.
6)进入包管理器控制台在Visual Studio和类型:
6) Go to Package Manager Console in Visual Studio and type:
-
启用的迁移-Force
-
添加迁移的init
-
更新,数据库
Enable-Migrations -Force
Add-Migration init
Update-Database
7)运行应用程序
请注意:在步骤6中部分3,如果你得到一个错误无法附加文件...,这可能是因为你没有在SQL Server中完全删除数据库文件。
Note: In step 6 part 3, if you get an error "Cannot attach the file...", it is possibly because you didn't delete the database files completely in SQL Server.
这篇关于如何为实体框架重新创建数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!