Reputation: 6908
I'm doing a project utilizing Entity Framework's Code-First approach. Things were going fine, until we changed the location of where the database is created:
connectionString="Data Source=(LocalDB)\ProjectsV12;AttachDbFilename=|DataDirectory|\Schema.Test.mdf;Integrated Security=True;Connect Timeout=30"
And then we set DataDirectory
:
AppDomain.CurrentDomain.SetData("DataDirectory", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\TestDB\\");
Now, we have several databases with names SCHEMA_TEST_*
, where the *
is some random number(I'm sure it's not random, but I hadn't figured out how it's generated). Also, I'm not sure what is causing the creation of another database, rather than the first created. Why is it creating the other databases, and why can't it just be called Schema.Test
like I'd mentioned in the connectionString
?
UPDATE
When I remove the AttachDbFilename
section of the connection string, it all appears to go back to normal no matter what I do. Though, admittedly, I'm still not sure how or why the extra databases are created. Though, it seems to be done every time there's a change in the schema. Which shouldn't happen since, we are utilizing migrations.
Upvotes: 4
Views: 251
Reputation: 552
This bit of code will turn off entityframework from monitoring your schema changes. Once you do this you have to fix schema changes your self.
Link: http://msdn.microsoft.com/en-us/data/jj556205.aspx
Stop Entity Framework from modifying database
DbContext = new DataContext();
DbContext.Configuration.AutoDetectChangesEnabled = false;
Upvotes: 1
Reputation: 47774
You have set your DataDirectory using the following code:
AppDomain.CurrentDomain.SetData("DataDirectory",
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
+ "\\TestDB\\");
So effectively your DataDirectory values is someval\\TestDB\\
This you use in your connectionString as
AttachDbFilename=|DataDirectory|\Schema.Test.mdf;
So won't the final DbFilename comes to someval\\TestDB\\\Schema.Test.mdf
? Notice the extra \
before Schema.Test.mdf
I am not sure of this, but just a general observation suggestion this could be a problem.
Upvotes: 1
Reputation: 237
Try creating your connection string using a layout like this:
<add connectionString="Server=yourServer;Database=yourDB;Integrated Security=true" name="DefaultConnection" PoviderName="System.Data.SqlClient"/>
Here is a link to a list with lots of examples of connection strings: http://msdn.microsoft.com/en-us/library/jj653752%28v=vs.110%29.aspx
Upvotes: 1