I have an existing SQL LocalDB v11.0 database created with database first and Entity Framework ADO.NET. The application, for personal and business finance, allows users to create different 'Activities' to record financial information about each. All data resides in the same database and reporting separately on each Activity is achieved programatically. This means that users cannot export or transfer their database to others without disclosing all data from all of that user's Activities. I wish to enable users, when creating a new Activity, to create a separate database, based on the same metadata scheme, which can be exported alone.
My initial App.config connectionString is:
<connectionStrings>
<add name="PMMEntities" connectionString="metadata=res://*/PMMData.csdl|res://*/PMMData.ssdl|res://*/PMMData.msl;provider=System.Data.SqlClient;provider connection string=" data source=(localdb)\v11.0; AttachDBFilename=|DataDirectory|\PMM.mdf; Integrated security=True;MultipleActiveResultSets=True;
</connectionStrings>
To create the new database I created a new folder in the relevant DataDirectory and then used:
string database = "|DataDirectory|\\Australia Properties\\Australia Properties.mdf";
string connectionString1 = $"Data Source=(LocalDB)\\v11.0;Integrated Security=True;Database=" + database + ";Connect Timeout = 30;App=EntityFramework" ;
using (SqlConnection connection1 = new SqlConnection(connectionString1))
[
connection1.Open();
//Do stuff here
connection1.Close();
}
The connection fails at the using statement and the database is not created in the new sub-directory (SQL will automatically create a new database with a .mdf filename if one does not already exist), leading me to think that the 'Australia Properties' database is not being created as it is not able to adopt the existing metadata scheme (PMMEntities/PMMData).
I have previously been able to open a connection to the existing database with:
var connectionString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
but this does not create a different database but simply opens the existing database.
How do I get around this?