EntityTypeBuilder does not contain a definition for ToTable in EF Core

前端 未结 12 1450
南笙
南笙 2021-02-06 20:22

I have this sample code:

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Models;

namespace MySampleNamespace
{         


        
相关标签:
12条回答
  • 2021-02-06 20:47

    Porting from EF6 to EFCore, we had this issue. Our cause was .HasKey now returns a KeyBuilder and the .ToTable doesn't operate on it. So reversing that worked.

    Ie. Was:

    mp.HasKey(m => m.Id)
      .ToTable("Table")
    

    Became:

    mp.ToTable("Table")
      .HasKey(m => m.Id);
    
    0 讨论(0)
  • 2021-02-06 20:47

    For net core 3.1, need to install these packages:

    Microsoft.EntityFrameworkCore
    Microsoft.EntityFrameworkCore.Relational
    
    0 讨论(0)
  • 2021-02-06 20:50

    I had this problem, but did not need to install:

    Microsoft.EntityFrameworkCore.Relational
    

    I simply exited VS 2017 and re-opened my solution. I had the following NuGet packages installed:

    Microsoft.EntityFrameworkCore
    Microsoft.EntityFrameworkCore.Tools
    

    And the following CLI Tool Reference:

    Microsoft.EntityFrameworkCore.Tools.DotNet
    
    0 讨论(0)
  • 2021-02-06 20:51

    Install

    • Microsoft.EntityFrameworkCore,
    • MicrosoftEntityFramworkCore.Tools, and finally
    • Microsoft.EntityFrameworkCore.SqlServer
    0 讨论(0)
  • 2021-02-06 20:57

    For me, my issue was that I trying to call ToView() incorrectly.

    I was doing:

     modelBuilder.Query<Vendor>(entity =>
            {
                entity.Property(v => v.VendorId).HasColumnName("VendorID");
                entity.Property(v => v.Name).HasColumnName("Vendor Name");                
            }).ToView("vwVendors");
    

    instead of:

    modelBuilder.Query<Vendor>(entity =>
            {
                entity.ToView("vwVendors");
                entity.Property(v => v.VendorId).HasColumnName("VendorID");
                entity.Property(v => v.Name).HasColumnName("Vendor Name");                
            });
    
    0 讨论(0)
  • 2021-02-06 20:57

    It's also possible you have version discrepancies in your solution, so for example, if one of the projects in your dependency chain has a 3.1 version of EF Core and another project has a 2.1, then you will also see this error, and no matter what you install it won't work, instead make sure they are the same version in the entire solution.

    0 讨论(0)
提交回复
热议问题