Why is Entity continuing to create new databases?

I am executing a project using the Entity Framework Code-First approach. Everything went well until we changed the location of the database:

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 *is a random number (I'm sure it is not random, but I did not understand how it was generated). Also, I'm not sure what causes the creation of another database, not the first. Why does he create other databases and why can't he just be called Schema.Test, as I mentioned in connectionString?

UPDATE

When I delete the AttachDbFilenameconnection string section , everything seems to return to normal no matter what I do. Although, admittedly, I'm still not sure how and why additional databases are created. Although, it seems, this is done every time a circuit change occurs. What should not happen since then, we use migration.

+4
source share
3 answers

Try creating a connection string using this layout:

<add connectionString="Server=yourServer;Database=yourDB;Integrated Security=true" name="DefaultConnection" PoviderName="System.Data.SqlClient"/>

Here is a link to a list with many examples of connection strings: http://msdn.microsoft.com/en-us/library/jj653752%28v=vs.110%29.aspx

+1
source

You installed your DataDirectory using the following code:

AppDomain.CurrentDomain.SetData("DataDirectory", 
     Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) 
     + "\\TestDB\\");

, DataDirectory someval\\TestDB\\

AttachDbFilename=|DataDirectory|\Schema.Test.mdf;

, DbFilename someval\\TestDB\\\Schema.Test.mdf? \ Schema.Test.mdf

, , .

+1

- . , .

: http://msdn.microsoft.com/en-us/data/jj556205.aspx

Entity Framework

 DbContext = new DataContext();
 DbContext.Configuration.AutoDetectChangesEnabled = false;
+1

Source: https://habr.com/ru/post/1538474/


All Articles