1
votes

I'm trying to make an initial migration of SQLite database with Entity Framework Core.

My solution consists of two projects: Console Application and Class Library (that contains DbContext and all models).

DbContext looks like that:

public class SQLite_DbContext : DbContext
{
    public DbSet<DeviceRepresentation> Devices { get; set; }
    public DbSet<FileInfoDatabaseRepresentation> Files { get; set; }

    public SQLite_DbContext(DbContextOptions<SQLite_DbContext> options)
        : base(options)
    {
    }
}

Being in class library (where I keep my DbContext), I'm trying to create migration:

dotnet ef migrations add InitialCreate

As a result, dotnet cli returns an error:

Unable to create an object of type 'SQLite_DbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

Can you see what could be a problem here?

3
I once had a little test-balloon with ef core and sqlite, using mirgations and it worked. I followed these instructions: docs.microsoft.com/en-us/ef/core/get-started/…Fildor
@Fildor I was following those instructions too. The difference between those instructions and mine project is that I have DbContext in separate project (class library) which cannot be deployed alone. Maybe that's a problemPiotrek

3 Answers

5
votes

I've probably found the solution. There is a nice explanation of what's happening under-the-hood while generating migrations in docs [here].

In my case, according to documentation, EF Core Tools are trying to create an instance of SQLite_DbContext passing no parameters to the constructor. Such constructor wasn't present in my code, because there is only one taking one parameter (options). That's the reason why it was "unable to create an object". When I removed my custom constructor from code, it started generating migrations properly.

Alternatively, as I see, there is also an option to take control of how DbContext is being created. There is possibility to create "factory" which EF tools will use to generate an instance of this class:

public class SQLite_DbContextFactory : IDesignTimeDbContextFactory<SQLite_DbContext>
    {
        public SQLite_DbContext CreateDbContext(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder<SQLite_DbContext>();
            optionsBuilder.UseSqlite("Data Source=blog.db");

            return new SQLite_DbContext(optionsBuilder.Options);
        }
    }
1
votes

If I remember good, SQLite does not supports migrations. You can follow this post if the workaround can fit your needs https://medium.com/@hoekje/simple-migrations-with-c-and-sqlite-9942e1863536

0
votes

I've had this error and I also have my solution split in two projects: a console app MyProject.Api and a class library MyProject, but since I'm getting my options through IConfiguration, I wasn't able to use the IDesignTimeDbContextFactory solution as it only gets a string[] args.

Turns out, it is possible to fix the problem without having to create a factory, the solution is to give a bit more context to the dotnet ef command line, like so:

dotnet ef migrations add InitialCreate --project src/MyProject --startup-project src/MyProject.Api

This will create a Migrations directory at the root of src/MyProject. If you want to create the classes somewhere else (e.g. in Data/Migrations), add this argument to the command line: --output-dir Data/Migrations.