ASP.NET ConnectionString AttachDbFilename=|DataDirectory|

橙三吉。 提交于 2019-11-30 21:30:20
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:

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).

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).

Hope this helps. Thanks for everyone that participated.

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.

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

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

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!