EntityTypeBuilder does not contain a definition for ToTable in EF Core

前端 未结 12 1449
南笙
南笙 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:30

    Depending on your .Net version you are using. Microsoft.EntityFrameworkCore.Tools.DotNet only supports .NetStandard >= 2.0.

    If your .Net version is 4.6.1, update Microsoft.EntityFrameworkCore to 2.0.0-preview1-final, along with related EntityFramework DLLs, then close Visual Studio 2017 and re-open.

    0 讨论(0)
  • 2021-02-06 20:38

    Ensure that the correct project is selected in the "Default Project" dropdown, and then type the command below :

    install-package microsoft.entityframeworkcore.sqlserver
    
    0 讨论(0)
  • 2021-02-06 20:39

    Ivan & Mardoxx are correct.

    I tried to just install Microsoft.EntityFrameworkCore.Tools then got this error:

    Detected package downgrade: Microsoft.EntityFrameworkCore from 2.1.4 to 2.1.1. Reference the package directly from the project to select a different version. -> Microsoft.EntityFrameworkCore.Tools 2.1.4 -> Microsoft.EntityFrameworkCore.Design 2.1.4 -> Microsoft.EntityFrameworkCore.Relational 2.1.4 -> Microsoft.EntityFrameworkCore (>= 2.1.4) -> Microsoft.EntityFrameworkCore (>= 2.1.1)

    1. I upgraded Microsoft.EntityFrameworkCore via nuget
    2. I did install Microsoft.EntityFrameworkCore.Tools which didn't work for ToTable , unknown if this is even needed
    3. I did then install the Microsoft.EntityFrameworkCore.Relational and it now resolves
    0 讨论(0)
  • 2021-02-06 20:42

    Installing Microsoft.EntityFrameworkCore.Relational is the correct solution, as Ivan says.

    0 讨论(0)
  • 2021-02-06 20:42

    You should add the nuget package Microsoft.EntityFrameworkCore.SqlServer, since this is a Microsoft SQL method.

    0 讨论(0)
  • 2021-02-06 20:46

    For EFCore 3.1 I needed to use this in my DB context:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
    
            foreach (IMutableEntityType entity in modelBuilder.Model.GetEntityTypes())
            {
                entity.SetTableName("WS_" + entity.GetTableName());
            }
        }
    

    No additional Nuget package was required.

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