Answered by:
EF create local db

Question
-
Hi guys! please tell me how create local db with ef?
public AupContext() : base("name=AupContext") { Database.SetInitializer<AupContext>(new CreateDatabaseIfNotExists<AupContext>()); }
<connectionStrings> <add name="AupContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\data.mdf;Initial Catalog=Test;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings>
Add migration and Update database and get error
- Moved by Kristin Xie Thursday, December 10, 2015 6:35 AM move to better forum
Wednesday, December 9, 2015 6:08 PM
Answers
-
Hi Jecuss,
|DataDirectory| (enclosed in pipe symbols) is a substitution string that indicates the path to the database. It eliminates the need to hard-code the full path which leads to several problems as the full path to the database could be serialized in different places. DataDirectory also makes it easy to share a project and also to deploy an application.
For example, instead of having the following connection string:
<connectionStrings> <add name="AupContext" connectionString="Data Source= (LocalDB)\v11.0;AttachDbFilename=D:\Data\data.mdf;Initial Catalog=Test;Integrated Security=True" providerName="System.Data.SqlClient"/> </connectionStrings>
To set the DataDirectory property, call the AppDomain.SetData method. If you do not set the DataDirectory property, the following default rules will be applied to access the database folder:
•
For applications that are put in a folder on the user's computer, the database folder uses the application folder.
•
For applications that are running under ClickOnce, the database folder uses the specific data folder that is created.
Best Regards,
Friday, December 11, 2015 1:37 AM
All replies
-
There is a MSDN EF forum you should posr to.Wednesday, December 9, 2015 7:52 PM
-
Hi Jecuss,
This forum is about the C# programming language, IDE, libraries, samples, and tools, as your issue is more related to the ADO.NET Entity Framework, we help you move it to the ADO.NET Entity Framework and LINQ to Entities forum.
Thank you for your understanding.
Best Regards,
Albert Zhang
Thursday, December 10, 2015 6:32 AM -
Hi Jecuss,
|DataDirectory| (enclosed in pipe symbols) is a substitution string that indicates the path to the database. It eliminates the need to hard-code the full path which leads to several problems as the full path to the database could be serialized in different places. DataDirectory also makes it easy to share a project and also to deploy an application.
For example, instead of having the following connection string:
<connectionStrings> <add name="AupContext" connectionString="Data Source= (LocalDB)\v11.0;AttachDbFilename=D:\Data\data.mdf;Initial Catalog=Test;Integrated Security=True" providerName="System.Data.SqlClient"/> </connectionStrings>
To set the DataDirectory property, call the AppDomain.SetData method. If you do not set the DataDirectory property, the following default rules will be applied to access the database folder:
•
For applications that are put in a folder on the user's computer, the database folder uses the application folder.
•
For applications that are running under ClickOnce, the database folder uses the specific data folder that is created.
Best Regards,
Friday, December 11, 2015 1:37 AM