How to output SQLite database in a different project in EF Core 3

.net-core .net-core-3.1 ef-migrations entity-framework-core sqlite


I have a solution with 2 projects


ProjectA is a class library targeting netstandard2.1, it contains the DbContext (with "Data Source=blog.db"), the entities and it has a dependency on Microsoft.EntityFrameworkCore.Sqlite

ProjectB targets a netcoreapp3.1 and has a dependency on Microsoft.EntityFrameworkCore.Design. I just use it to manage migrations because migrations need a framework.

The SQLite database does not exist, so I create the first migration with dotnet tools from the ProjectB

dotnet ef migrations add InitialCreate --project ../ProjectA

and it successfully creates the Migrations folder with the initial one.

Now I want to update the db so that the SQLite *.db is actually created. If I do this:

dotnet ef database update --project ../ProjectA

the *.db file is created but in the ProjectB. I want the file to be output in the ProjectA but I cannot find a way to do so.

The doc does not seem to mention anything and neither the ef database update --help

UPDATE 1: I have tried to create and update migrations from the ProjectA instead and point to the executable project like this:

dotnet ef migrations add InitialCreate --startup-project ../ProjectB

and it creates properly the Migrations in ProjectA. Now I try to apply them to generate the *.db like this:

dotnet ef database update --startup-project ../ProjectB

and it generates the *.db file... but again in ProjectB! not in ProjectA where I want.

It seems wherever the Microsoft.EntityFrameworkCore.Design package is, the generated database is placed there.

UPDATE 2: Specifying the project when updating database to point to where the DbContext is, didn't work either. It keeps generating the *.db in Project B

dotnet ef database update --startup-project ../Sasw.SimpleBlog.Storage.SQLite.Migrator/ --project ./
12/8/2019 11:33:46 AM

Popular Answer

I fixed it by specifying a full path at my connection string. It's sufficient because this path should be configurable in settings.

public class DatabaseContext
    : DbContext
    public DbSet<Thing> Things{ get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder options)
        options.UseSqlite("Data Source=D:\\database.db");

The only problem left here is how to pass this connection string at runtime by using the dotnet ef CLI. But that seems an open question atm

12/8/2019 11:57:09 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow