4
votes

I'd like to use SQLite with Entity Framework Core. I'm following this official tutorial:

.NET Core - New Database

It says to modify the project.json like this:

{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "dependencies": {
    "Microsoft.EntityFrameworkCore.Sqlite": "1.1.0",
    "Microsoft.EntityFrameworkCore.Design": {
      "version": "1.1.0",
      "type": "build"
    }
  },
  "frameworks": {
    "netcoreapp1.1": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.1.0"
        }
      },
      "imports": "dnxcore50"
    }
  },
  "tools": {
    "Microsoft.EntityFrameworkCore.Tools.DotNet": "1.1.0-preview4"
  }
}

I have installed the VS 2017 RC and Net Core now uses the csproj file instead of project.json.

I modified my csproj to Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version= "1.1.0-preview4-final" but when I try the command dotnet ef migrations add MyFirstMigration I get the following error:

"no executable found matching command "dotnet-ef""

How can I fix this with the new csproj?

1

1 Answers

4
votes

VS2017 will be officially released on the 7th of March 2017. The doc concerns VS2015, they have not been updated yet to reflect the latest VS 2017 changes. Modifying the csproject like this solves the problem:

<ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="1.1.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="1.1.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.0" />
  </ItemGroup>

  <ItemGroup>
  <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version = "1.1.0-preview4-final"/>
  </ItemGroup>

Even after this, when I tried to run the first migration with the CLI command:

dotnet ef migrations add MyFirstMigration

I got the following error: “No parameterless constructor was found on 'TContext'. Either add a parameterless constructor to 'TContext' or add an implementation of 'IDbContextFactory' in the same assembly as 'TContext'.”

According to this document, this happens because design-time tools attempt to automatically find how your application creates instances of your DbContext type. If EF cannot find a suitable way to initialize your DbContext, you may encounter this error.

I was pretty sure I had setup a parameterless constructor and I was able to see the context with the following CLI command:

dotnet ef database dbContext list

The only way to perform the migration has been to implement the interface IDbContextFactory<TContext> in the project. The documentation and the examples on how to do this are based on VS 2015. In VS 2017 IDbContextFactory<TContext>.Create() wants a DBContextFactoryOptions as parameter:

public class MyContextFactory: IDbContextFactory<MyContext>   
{
    public MyContext Create(DbContextFactoryOptions options)
    {
        var optionsBuilder = new DbContextOptionsBuilder<MyContext>();       
        optionsBuilder.UseSqlite("Filename=./mydatabase.db");

        return new MyContext(optionsBuilder.Options);        
    }
}