简体   繁体   English

在 EF4 中自动创建数据库的问题

[英]problems with automatic creation of the database in EF4

I'm learning ASP.NET MVC 3 with Entity Framework Code First.我正在学习带有实体框架代码的 ASP.NET MVC 3。 I'm following a tutorial and I downloaded the corresponding solution for testing on my local machine.我正在学习教程,并下载了相应的解决方案以在本地计算机上进行测试。 Now, something I didn't understand very well is about the automatic creation of the database (if this one didn't exist yet on disk).现在,我不太了解的是关于数据库的自动创建(如果磁盘上尚不存在该数据库)。 The very first time I run the application, the database is created for me.我第一次运行应用程序时,为我创建了数据库。 That's ok.没关系。

Here is the section in Web.config这是 Web.config 中的部分

<add name="BlogContext" 
     connectionString="Data Source=.\SQL2008;Initial Catalog=CodeFirstMVC.mdf;Integrated Security=SSPI"
     providerName="System.Data.SqlClient" /> 

But I have two problems:但是我有两个问题:

1st.第一个。 For testing purpose, I deleted the database on disk and run again the solution.出于测试目的,我删除了磁盘上的数据库并再次运行该解决方案。 I thought that the database would be automatically created but I was wrong: I got the error message below:我以为数据库会自动创建,但我错了:我收到以下错误消息:

{"Unable to open the physical file \"c:\\Program Files\\Microsoft SQL Server\\MSSQL10.SQL2008\\MSSQL\\DATA\\CodeFirstMVC.mdf.mdf\". Operating system error 2: \"2(failed to retrieve text for this error. Reason: 15105)\".\r\nCannot open database \"CodeFirstMVC.mdf\" requested by the login. The login failed.\r\nLogin failed for user 'sa'.\r\nFile activation failure. The physical file name \"c:\\Program Files\\Microsoft SQL Server\\MSSQL10.SQL2008\\MSSQL\\DATA\\CodeFirstMVC.mdf_log.LDF\" may be incorrect."}

I noticed that if I changed the file name in my Web.config then the database is again successfully created.我注意到,如果我更改了 Web.config 中的文件名,那么数据库将再次成功创建。 Can you explain me?你能给我解释一下吗? Why do I have to change the database name to get it running again?为什么我必须更改数据库名称才能让它再次运行?

2nd.第二。 The database is created in the folder located in C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA.数据库在位于 C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA 的文件夹中创建。 I would like to store my database in the App_Data directory.我想将我的数据库存储在 App_Data 目录中。 How can I proceed?我该如何进行?

Initial catalog is not path to file. Initial catalog不是文件路径。 It is the name of database.它是数据库的名称。 AttachDbFilename is used to specify the file so your connection string should look like: AttachDbFilename用于指定文件,因此您的连接字符串应如下所示:

Data Source=.\SQL2008;Initial Catalog=CodeFirstMVC;AttachDbFilename=|DataDirectory|CodeFirstMVC.mdf;Integrated Security=SSPI

Where |DataDirectory|在哪里|DataDirectory| instructs SQL server to use local application data directory instead of global SQL Server data directory.指示 SQL 服务器使用本地应用程序数据目录而不是全局 SQL 服务器数据目录。 Local data directory for web application is App_Data . web 应用程序的本地数据目录是App_Data

Edit:编辑:

I just noticed that you are probably using full SQL server instead of SQL server express.我刚刚注意到您可能正在使用完整的 SQL 服务器而不是 SQL 服务器快递。 As I know creating database in App_Data automatically is feature of SQL server express.据我所知,在 App_Data 中自动创建数据库是 SQL 服务器快递的功能。 That also explains first error because SQL server created database called CodeFirstMVC.mdf and stored the database in its global data directory within CodeFirstMVC.mdf.mdf file and transaction log in CodeFirstMVC.mdf.ldf file.这也解释了第一个错误,因为 SQL 服务器创建了名为 CodeFirstMVC.mdf 的数据库,并将数据库存储在 CodeFirstMVC.mdf.mdf 文件中的全局数据目录中,并将事务日志存储在 CodeFirstMVC.mdf.ldf 文件中。 It also registered that database internally.它还在内部注册了该数据库。 By deleting files you didn't remove database from SQL server.通过删除文件,您并未从 SQL 服务器中删除数据库。 You just break its functionality but SQL server still believes that the database exists.您只是破坏了它的功能,但 SQL 服务器仍然认为数据库存在。 That is also reason why you have to change the name to make it work.这也是为什么您必须更改名称才能使其正常工作的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM