141

I have this sample code:

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

namespace MySampleNamespace
{
    public class MyDbContext : DbContext
    {
        public MyDbContext(DbContextOptions<MyDbContext> options)
            : base(options)
        {
        }

        public DbSet<User> Users { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            new UserMap(modelBuilder.Entity<User>());
        }

        public class UserMap
        {
            public UserMap(EntityTypeBuilder<User> entityBuilder)
            {
                entityBuilder.ToTable("User");
                entityBuilder.Property(s => s.Username).HasMaxLength(15).IsRequired();
            }
        }
    }
}

I was testing out some example from MS website, but I can't find ToTable method. In the example, I checked what are the Usings and the only Using the example had is Microsoft.EntityFrameworkCore aside from the class project for the model he was using. Was this changed? How do I do this now?

1
  • 48
    Make sure you have reference to Microsoft.EntityFrameworkCore.Relational assembly. The usings are ok - the method in question is extension method defined in RelationalEntityTypeBuilderExtensions class under Microsoft.EntityFrameworkCore namespace in that assembly.
    – Ivan Stoev
    Apr 4, 2017 at 7:33

13 Answers 13

294

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

3
  • 1
    @GertArnold fixed
    – Mardoxx
    May 24, 2018 at 13:45
  • 1
    HasColumnName is also in this nuget package. I was migrating some DotNet Framework code and thought I was going crazy. Thanks! nuget.org/packages/Microsoft.EntityFrameworkCore.Relational Jan 6, 2020 at 19:31
  • I have no idea why intellisense is failing to suggest this missing nuget package. Either let users add all packages manually, or work perfectly. Bad intellisense. May 18 at 7:25
29

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

14

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
13

For net core 3.1, need to install these packages:

Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Relational
9

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
1
  • 1
    I also followed your steps and upgraded <TargetFramework>netcoreapp3.0</TargetFramework> from netcoreapp2.1 now Microsoft.EntityFrameworkCore.Tools and Microsoft.EntityFrameworkCore.Relational are successfully installed but the using statement doesnot show a "Relational" namespace after Microsoft.EntityFrameworkCore and therefore i am unable to use MapToStoredProcedures :S
    – Samra
    Oct 21, 2019 at 0:08
8

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);
1
  • Working now, this was it for me.
    – MDave
    Sep 23, 2018 at 17:35
7

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
4

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

install-package microsoft.entityframeworkcore.sqlserver
3

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");                
        });
3

Install

  • Microsoft.EntityFrameworkCore,
  • MicrosoftEntityFramworkCore.Tools, and finally
  • Microsoft.EntityFrameworkCore.SqlServer
1

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.

1

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

Please installing Microsoft.EntityFrameworkCore.Tools package and write it:

builder.ToTable("tblName");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.