SQL ASP.NET ConnectionString AttachDbFilename=|数据目录|

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15320428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 14:10:50  来源:igfitidea点击:

ASP.NET ConnectionString AttachDbFilename=|DataDirectory|

asp.netsqlconnection-string

提问by Alaa

This is about ConnectionStrings / ASP.NET MVC with Visual Studio 2012 ultimate & SQL Server Express 2012.

这是关于 ConnectionStrings / ASP.NET MVC with Visual Studio 2012 Ultimate & SQL Server Express 2012。

Following up with this tutorial here: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4I came across an issue with these two connection strings at my web.config:

在此处跟进本教程:http: //www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4我遇到了一个问题我的 web.config 中的这两个连接字符串:

<connectionStrings>
    <add name="DefaultConnection" 
         connectionString="Data Source=(LocalDb)\v11.0;
                           Initial Catalog=aspnet-MvcMovie-users;
                           Integrated Security=SSPI;
                           AttachDBFilename=|DataDirectory|\aspnet-MvcMovie-users.mdf"
         providerName="System.Data.SqlClient" />

    <add name="MovieDBContext" 
         connectionString="Data Source=(LocalDB)\v11.0;
                           AttachDbFilename=|DataDirectory|\Movies.mdf;
                           Integrated Security=True" 
         providerName="System.Data.SqlClient" />
</connectionStrings>

the website works fine but I couldn't fingure out why the first db is created in the App_Data folder while the second one is created in "C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA"?! I supposed that both will be created in App_data because both utilize this attribute: AttachDBFilename=|DataDirectory|!

该网站工作正常,但我无法弄清楚为什么第一个 db 是在 App_Data 文件夹中创建的,而第二个是在“C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA”中创建的?!我认为这两者都将在 App_data 中创建,因为它们都利用了这个属性:AttachDBFilename=|DataDirectory|!

note: the tutorial mentions that it should be in the App_Data & they added a screenshot that shows it there indeed!

注意:教程提到它应该在 App_Data 中,并且他们添加了一个屏幕截图,确实在那里显示了它!

I have been looking for an answer and got into the complicity of SQL (I thought User Instances might be the solution) but couldn't reach an answer for this : |

我一直在寻找答案并陷入了 SQL 的共谋(我认为用户实例可能是解决方案)但无法为此找到答案:|

(this might be useful to read about User Instances http://msdn.microsoft.com/en-us/library/bb264564(v=sql.90).aspx)

(这可能有助于阅读用户实例http://msdn.microsoft.com/en-us/library/bb264564(v=sql.90).aspx

Any ideas are greatly appreciated. Thanks in advance.

任何想法都非常感谢。提前致谢。

Regards

问候

回答by Alaa

after research/tests it turned out to be as follows:

经过研究/测试,结果如下:

VS will look at the class name of the DataContext and will look to see if you have provided a connection string with the same name as the class name; for example:

VS 将查看 DataContext 的类名,并查看您是否提供了与类名同名的连接字符串;例如:

public class MovieDataContext : DbContext

and

<connectionStrings><add name="MovieDataContext" ...

if it manages to find a matching connection string it will create the DB based on the criteria you specified in the respective data string (to add the DB to the App_Data set the path of the DB to |DataDirectory| as shown in both connection strings mentioned in the question); if the name doesn't match or you didn't provide any connection string, VS will fall back to the default settings and will create the DB in the default location/settings(usually C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA).

如果它设法找到匹配的连接字符串,它将根据您在相应数据字符串中指定的条件创建 DB(要将 DB 添加到 App_Data 将 DB 的路径设置为 |DataDirectory|,如提到的两个连接字符串所示在问题中);如果名称不匹配或您没有提供任何连接字符串,VS 将回退到默认设置,并将在默认位置/设置(通常是 C:\ SQLEXPRESS\MSSQL\DATA)。

note neither the "Integrated Security" settings nor the "Initial Catalog" play any role with this (I was able to create the DB in the App_Data with both Integrated Security = True & Integrated Security = SSPI and with/without Initial Catalog).

请注意,“集成安全性”设置和“初始目录”都没有对此起作用(我能够在 App_Data 中使用集成安全性 = True 和集成安全性 = SSPI 以及使用/不使用初始目录创建数据库)。

Hope this helps. Thanks for everyone that participated.

希望这可以帮助。感谢所有参与的人。

回答by Primc

I had the same issue. I believe the difference is in the Integrated Security setting. I have SQLExpress installed and found my Movies database in there using MS SQL Server Management Studio.

我遇到过同样的问题。我相信区别在于集成安全设置。我安装了 SQLExpress,并使用 MS SQL Server Management Studio 在那里找到了我的电影数据库。

Check out this response for a better explanation. Difference between Integrated Security = True and Integrated Security = SSPI

查看此回复以获得更好的解释。集成安全 = True 和集成安全 = SSPI 之间的区别

回答by jonni

What AMT has given is exactly right. It was confusing as it is to use connection strings with .mdf and .sdf files.

AMT给出的完全正确。将连接字符串与 .mdf 和 .sdf 文件一起使用是令人困惑的。

I have another pointer for you though, you can change the default setting where the application looks for a connection string with the name matching the class name of the context class by overriding the constructor of DBContext and providing the paramter nameOrConnectionString as follows

我有另一个指针给你,你可以改变默认设置,通过覆盖 DBContext 的构造函数并提供参数 nameOrConnectionString,应用程序查找名称与上下文类的类名匹配的连接字符串,如下所示

public BlogsContext()
            : base("name=EFBlogs")
        {
        }
public BlogsContext()
            : base("name=EFBlogs")
        {
        }

Application then searches for a connection string named EFBlogs, if it cannot find connection string then it creates the database with name EFBlogs, instead of BlogsContext

应用程序然后搜索名为 EFBlogs 的连接字符串,如果找不到连接字符串,则创建名为 EFBlogs 的数据库,而不是 BlogsContext

回答by Robert Peter Bronstein

Hi I notice a difference when you add a database and it asks do you want it to be placed in the app_data folder if you click yes then it goes to the app_data folder and the full path name of the mdf is also in the app_data folder whne you use file explorer.

嗨,我注意到添加数据库时的不同之处,如果单击是,它会询问您是否希望将其放置在 app_data 文件夹中,然后它会转到 app_data 文件夹,并且 mdf 的完整路径名也在 app_data 文件夹中您使用文件资源管理器。