Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migrations Error - cannot scaffold literals of type DirectConstructorBinding

Tags:

ef-core-2.0

I'm following the Pluralsight Course by Julie Lerman -- EntityFramework Core 2: Getting Started. In this course, she has 3 projects. The first two Data and Domain are based on the .NET Standard library. The third project Web is a .NET Core Web Application.

I've followed this structure. In Data I added one POCO class called Client.

In Domain I added one class called TestDbContext like so:

public class TestDbContext : DbContext
{
    public DbSet<Client> Clients { get; set; }

    public TestDbContext(DbContextOptions<TestDbContext> options) : base(options)
    {

    }
}

Following her example, I did the following in the Startup.cs of my Web project to inject the provider and connection string into the DbContext.

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

Lastly, I attempt to add a migration to this context. I have the Web project set as the startup project. From the Package Manager Console, I type add-migration initial.

I then get the following error: The current CSharpHelper cannot scaffold literals of type 'Microsoft.EntityFrameworkCore.Metadata.Internal.DirectConstructorBinding'. Configure your services to use one that can.

In Julie's video, this all worked for her and the migration package was created. However, for me -- just the error. Any clues as to what may be going on?

like image 862
RHarris Avatar asked Jun 08 '18 14:06

RHarris


2 Answers

Check packages version of your project in '.csproj' file. Previously I have the same problem for version mitch match of AspNet Core and EntityFramework Core (I guess). Making those same works for me fine. Found solution Here

For me Now.

<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.0-rc1-final" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.0-rc1-final" PrivateAssets="All" /> 
like image 69
Jaggesher Mondal Avatar answered Oct 12 '22 23:10

Jaggesher Mondal


Add 3 packages with same version from nuget package manager

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.2" />
like image 5
Alamgir Avatar answered Oct 13 '22 00:10

Alamgir