[英]Why is mdf file not appearing in the App_Data folder?
I've being trying to get started with Beginning ASP.NET MVC 4.我一直在尝试开始使用 Beginning ASP.NET MVC 4。
And hit a problem straight away, according to the e-book I should be able to start a new mvc 4 internet application, debug and select log-in form the UI tempate, then stop.并立即遇到问题,根据电子书,我应该能够启动一个新的 mvc 4 Internet 应用程序,调试并从 UI 模板中选择登录,然后停止。 This should create the mdf file in the App_Data folder.
这应该在 App_Data 文件夹中创建 mdf 文件。 Which I should be able to click on and open in Server Explorer.
我应该能够在服务器资源管理器中单击并打开它。
What actually happens - Database get created in my SQL Express.实际发生了什么 - 在我的 SQL Express 中创建了数据库。
Getting this step correct is crucial to following the rest of the tutorial.正确执行此步骤对于遵循本教程的其余部分至关重要。
I am familiar with MVC , but mainly the front end stuff, so I'm trying to increase my understanding of the database/models side of MVC.我熟悉 MVC ,但主要是前端的东西,所以我试图增加我对 MVC 的数据库/模型方面的理解。
This is a completely new project, no changes were made, just out of the box code.这是一个全新的项目,没有进行任何更改,只是开箱即用的代码。 On a windows XP machine, using Visual Studio 2010.
在 Windows XP 机器上,使用 Visual Studio 2010。
Connectionstring -连接字符串 -
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-HaveYouSeenMe-20131125091100;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
Can anyone please explain - why my out of the box connectionstring is creating the Db in SQL Express and how to create the mdf in the App_Data folder as book suggests.任何人都可以解释一下 - 为什么我的开箱即用的连接字符串在 SQL Express 中创建 Db 以及如何按照书的建议在 App_Data 文件夹中创建 mdf。 cheers.
干杯。
I know this says answered, but it may be as simple as clicking the "show all files" icon in the Solution Explorer.我知道这是回答,但它可能就像单击解决方案资源管理器中的“显示所有文件”图标一样简单。 By default the .mdf file will not show up, so ensure the "show all files" is selected :)
默认情况下 .mdf 文件不会显示,因此请确保选择“显示所有文件”:)
It took me sometime as well to figure this out since I want to attach the database under App_Data and nothing is happening.我花了一段时间也摸不着头脑,因为我想的App_Data下附加数据库,并没有发生任何事情。 No files were added.
添加任何文件。 I just figure it out that there are two options to click the Show All Files.
我刚弄明白,有两个选项,单击显示所有文件。
On this picture, I was clicking the Red arrow which says Show All Files.在这幅画,我被点击红色箭头,其显示所有文件说。 After sometime, I tried to click the other Show All Files on the Blue arrow and it now works.
一段时间后,我试着点击蓝色其他显示所有文件箭头,它现在的作品。
I hope that might help if you encounter the same problem.我希望可以帮助,如果你遇到同样的问题。
Just change your connection string:只需更改您的连接字符串:
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\aspnet-HaveYouSeenMe-20131125091100.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
The |DataDirectory|
|DataDirectory|
is a special token which is pointing to the ~/App_Data
folder of your application.是指向应用程序的
~/App_Data
文件夹的特殊令牌。
Checkout the following article on MSDN
which provides more details about connection strings in SQLExpress.查看
following article on MSDN
的following article on MSDN
其中提供了有关 SQLExpress 中连接字符串的更多详细信息。
The database most likely has been created in your Documents folder.该数据库很可能已在您的文档文件夹中创建。 In the Server Explorer right click on the Database (in Data Connections) and click Browse In SQL Server Object Explorer.
在对数据库(在数据连接)的服务器资源管理器右键单击,然后单击浏览在SQL Server对象资源管理器。 Then in SQL Server Object Explorer right click on your database and select Properties.
然后,在SQL Server对象资源管理器右键单击您的数据库,并选择属性。 In the Properties window look up Current Connection Parameters and see the value for Data File.
在属性窗口中的查找当前连接参数,看看数据文件中的值。
To ensure the database is created in your App_Data folder insert AttachDbFilename=|DataDirectory|\\database_name.mdf
in your connection string.为了确保数据库在App_Data文件夹插件形成
AttachDbFilename=|DataDirectory|\\database_name.mdf
您的连接的字符串中。
You are missing AttachDbFilename
你缺少
AttachDbFilename
<connectionstrings>
<add connectionstring="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\aspnet-HaveYouSeenMe-20131125091100.mdf;Integrated Security=True;User Instance=True "
name="ConnectionASPX" providername="System.Data.SqlClient" />
</connectionstrings>
In place of the physical path to a database file that is stored in the App_Data folder, you can specify the |DataDirectory|
在地方存储在App_Data文件夹中的数据库文件的物理路径,你可以指定
|DataDirectory|
variable in the AttachDbFileName
setting. AttachDbFileName
设置中的变量。
In my case, it was because the user account that I use to login to my PC is Microsoft account (Outlook email address), while Visual Studio read my local user account.就我而言,那是因为我用它来登录到我的电脑的用户帐户是Microsoft帐户(Outlook电子邮件地址),而Visual Studio的阅读我的本地用户帐户。 The local user account is the "userName" in C:\\Users\\userName\\Documents\\Visual Studio 2015\\Projects\\ProjectFolder\\App_Data.
本地用户帐户是 “username” 的在C:\\ Users \\用户名\\文档\\ Visual Studio的2015年\\项目\\ ProjectFolder \\ App_Data文件。
I'm using Visual Studio 2015 in Windows 10.我使用Visual Studio 2015年在Windows 10。
The solution turns out to be simple.该解决方案被证明是简单的。 I just need to go to my PC Settings, select Accounts and choose to sign in with local account instead of Microsoft account.
我只是需要去我的电脑设置,选择帐户,并选择与本地帐户,而不是微软帐户登录。
You can also go into the server explorer and right click on the data connections .您还可以进入服务器资源管理器并右键单击数据连接。 Then press "add connections" and change the data source to be "server database file" and under database file name(new or existing) you go in the app_data folder, if you cant find the .mdf file, then browse to that folder and add the name you want to give your file.
然后按“添加连接”,改变数据源是“服务器数据库文件”,并在数据库文件名(新的或现有的),你在App_Data文件夹中去,如果你不能找到.mdf文件,然后浏览到该文件夹,添加你想给你的文件的名称。
`SulemansStore\SulemansStore\App_Data\SulemansDatabase.mdf`
I browse to App_Data
and add my databasefile name Sulemansdatabase.mdf
(this part I added if I am creating a new database file).我浏览到
App_Data
并添加我的databasefile名Sulemansdatabase.mdf
(这部分我加了,如果我创建一个新的数据库文件)。
Do you have an App_Data
folder?你有
App_Data
文件夹吗? In my case, it was showing up in Visual Studio but it did not actually exist.就我而言,它出现在 Visual Studio 中,但实际上并不存在。 This was because git does not push empty directories, so cloning the repo into another location would result in no
App_Data
folder.这是因为 git 不会推送空目录,因此将 repo 克隆到另一个位置将导致没有
App_Data
文件夹。 Once I created it manually, the issue was resolved.一旦我手动创建它,问题就解决了。
Even after using |DataDirectory|, it didnt work for me.即使在使用 |DataDirectory| 之后,它对我也不起作用。 It worked after keeping .mdf file name as same as solution name.
它在保持 .mdf 文件名与解决方案名称相同后工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.