San*_*eal 20 asp.net-mvc sql-server-2008
我是使用MVC的新手,我正在尝试使用初始化程序在首次启动应用程序时将数据初始化到我的数据库中.这是我在Global.asax.cs中得到的:
System.Data.Entity.Database.SetInitializer(new MyAppInitializer());
MyAppContext db = new MyAppContext();
db.Database.Initialize(true);
Run Code Online (Sandbox Code Playgroud)
在Web.config中,这是我的连接字符串:
<connectionStrings>
<add name="MyAppContext"
connectionString="data source= MyServer; Integrated Security=True; database=MyDatabase"
providerName="System.Data.SqlClient"/>
Run Code Online (Sandbox Code Playgroud)
这是使用MS SQL 2008 R2.我的初始化程序看起来像这样:
public class MyAppInitializer : DropCreateDatabaseAlways<MyAppContext>
{
protected override void Seed(MyAppContext context)
{
var organizations = new List<Organizations>
{
new Organizations { OrgName = "AT", OrgPhone = 5093333433, OrgOfficeLocation = "ITB", OrgPointOfContact = "Tony", OrgIsActive = 1 },
new Organizations { OrgName = "Libraries", OrgPhone = 5093331122, OrgOfficeLocation = "Holland-Terrell", OrgPointOfContact = "Herald", OrgIsActive = 1 }
};
organizations.ForEach(s => context.Organizations.Add(s));
context.SaveChanges();
Run Code Online (Sandbox Code Playgroud)
我确保在SQL Server Management Studio中关闭了与服务器和数据库的连接,但是有多个人可以访问此数据库,尽管现在没有人应该使用它.我怎样才能得到它以便我可以在我的数据库中初始化这些数据?谢谢!
编辑:我已经在服务器上创建了数据库,但它是完全空的(没有表,程序等).这会引起问题吗?
Two*_*Pea 47
今天我在使用MVC codefirst时遇到了类似的问题.在尝试了各种各样的东西20分钟后,我注意到,Visual Studio中的"服务器资源管理器"选项卡有一个连接打开我的数据库.在"关闭"visual studio的服务器资源管理器选项卡中的连接后,代码能够运行并自动重新创建数据库.
Sam*_*Sam 25
在SSMS中运行这样的东西......
USE master -- be sure that you're not on MYDB
ALTER DATABASE MYDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE
DROP DATABASE MYDB;
Run Code Online (Sandbox Code Playgroud)