msallin / SQLiteCodeFirst

Creates a SQLite Database based on a EdmModel by using Entity Framework CodeFirst.
Apache License 2.0
610 stars 123 forks source link

change database location #123

Closed ghost1372 closed 6 years ago

ghost1372 commented 6 years ago

hi how i can change database location to this place? C:\Users\{PCUsername}\AppData\Roaming\{SomeFolder}\database

i used this code but table dont craete base.Database.Connection.ConnectionString = @"data source=" + AppVariable.myPath + @"database\data.db;";

CADbloke commented 6 years ago

Make sure there are no connections, move the file, change the connection string, carry on. Or do you want to create a new database in the new location?

Make sure the ctor for your DbContext doesn't have the connection string baked in, this is where Entoty Framework gets the connection details from. You can't change the connection string in the middle of a DbContext transaction, that's like swapping out a car while you're driving it.

ghost1372 commented 6 years ago

i fixed issue just needed this little code on OnModelCreating Database.CreateIfNotExists(); but i found a better way to do that i changed app.config to : connectionString="data source=|DataDirectory|\database\data.db" and when app luanched i set DataDirectory to new location:

 string fileName = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            AppDomain.CurrentDomain.SetData("DataDirectory", System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\" + Assembly.GetExecutingAssembly().GetName().Name + @"\");