0

I have local sqlite db near my *.exe file and I need that the core of migrations will generate (by Update-database command) database in this place. Now I'm using only database name as parameter of DbContext constructor, but db is not generated near my *.exe (I think because it is performed in another process).

How I can do it?

I've found the solution here, but it's not good... How to create dynamic database in entity framework with specified path?

Thanks.

Community
  • 1
  • 1
Serg046
  • 1,043
  • 1
  • 13
  • 42
  • Please let me know if I described my issue unclear. – Serg046 Sep 09 '15 at 11:33
  • Here is a thread on SQLite creation that may help: http://stackoverflow.com/questions/22174212/entity-framework-6-with-sqlite-3-code-first-wont-create-tables – Steve Greene Sep 09 '15 at 12:43
  • If SQLite doesn't support db initialization I can use default local db, but I need have db near *.exe file... – Serg046 Sep 09 '15 at 12:50
  • Ok, I think I need to use default (SqlServer) local db to avoid any issues with EF core but I still don't know how I can generate db near .exe – Serg046 Sep 09 '15 at 13:36
  • If you are going to use localdb you can specify a path in the connection string. Be aware it is intended to only be used for development purposes. Maybe consider SQL Express? http://blogs.msdn.com/b/sqlexpress/archive/2011/07/12/introducing-localdb-a-better-sql-express.aspx – Steve Greene Sep 09 '15 at 14:51
  • The issue is that how I can specify this path. I want to avoid hardcoding... I need have a location near *.exe, not Documents and Settings etc. I can't use sql serv express because I need local integrated db. – Serg046 Sep 09 '15 at 15:06
  • It's not hard coding - it's configuration. IAC, you can build the string however you like: http://stackoverflow.com/questions/20216147/entity-framework-change-connection-at-runtime – Steve Greene Sep 09 '15 at 15:11

1 Answers1

2

In my WindowsForms application with SQL Server CE I do this steps:

1) In app.config configure your connections string like this (add "|DataDirectory|")

.... connectionString="Data Source=|DataDirectory|yourDatabase.sdf" />

2) In constructor of Configuration() class insert path to location with your database

internal sealed class Configuration : DbMigrationsConfiguration<DataContext>
{
    public Configuration()
    {
        AppDomain.CurrentDomain.SetData("DataDirectory", @"your_path");
    } 
...
}

After "update-database" command EF will update database in this path