diegosasw
diegosasw

Reputation: 15634

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

I have a solution with 2 projects

src
|-ProjectA
|-ProjectB

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 ./

Upvotes: 0

Views: 1659

Answers (2)

BeSa
BeSa

Reputation: 11

I know its kinda late for this response but maybe this helps, i haven't tried it. Have you tried choosing a different default project in Visual Studio's Package Manager Console?

Package Manager Console - Default Project select

Upvotes: 0

diegosasw
diegosasw

Reputation: 15634

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 https://github.com/aspnet/EntityFrameworkCore/issues/10750

Upvotes: 2

Related Questions