8
votes

I have a .NET Core 2.2 project and am trying to set up EF migrations.

When running:

dotnet ef migrations add init --verbose

I get the following errors:

Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'... Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'. Finding design-time services referenced by assembly 'myproject-api'. No referenced design-time services were found. Finding IDesignTimeServices implementations in assembly 'myproject-api'... No design-time services were found. System.IO.FileNotFoundException: Could not load file or assembly 'myproject, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

This is my csproj file:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Folder Include="wwwroot\" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.4" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.2.4" />
  </ItemGroup>

</Project>

Edit, here is my Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
          WebHost.CreateDefaultBuilder(args)
            .UseKestrel()
            .UseUrls("http://localhost:5004")
            .UseStartup<Startup>();

}
3
Are you able to build your project or solution?Bijay Yadav
how have you set up your app to use the dbcontext? personally i only use two commands ever: with nuget package manager: "add-migration" and "update-database"Johan Herstad
Yes I am able to build my project @BijayYadavRobbie Mills
I've not used migrations before for this project @JohanHerstad, I've been doing the database updates manuallyRobbie Mills
To get a better understanding of your project setup, can you please post your databasecontext definition as well as the startup file. In the beginning try to specify you database context in the migration command: dotnet ef migrations add init --context <dbcontextfile>DTeuchert

3 Answers

2
votes

EF Core's design time resolution heavily relies on naming conventions and having your configuration in the right place. Your Program class looks right though.

Can you check if your Startup class looks similar to this and has the required AddDbContext call in the ConfigureServices method?

public class Startup
{
    public IConfiguration Configuration { get; }

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    }
}
2
votes

Try;

Add context name to options constructor.

 public MyDbContext(DbContextOptions<**MyDbContext**> options)
      : base(options)
    {
    }

Migration command.

Add-Migration MyMig -Context MyDbContext

and install the "Microsoft.EntityFrameworkCore.SqlServer" package on the "myproject-api" layer if it is not installed.

2
votes

You need to start to configure the database context

public class MyDbContext : DbContext
{
    public DbSet<MyEntity> Entities { get; set; }

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

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // Apply configurations
    }
}

and configure the sqlserver in the Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<MyDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}

As I already mentioned in the comments, than you need to run the migration and specify the context:

$ dotnet ef migrations add init --context MyDbContext

Optional you can also define the startup project --startup-project and the output directory --output-dir