EntityTypeBuilder does not contain a definition for ToTable in EF Core
Entity FrameworkEntity Framework-CoreEntity Framework Problem Overview
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?
Entity Framework Solutions
Solution 1 - Entity Framework
Installing Microsoft.EntityFrameworkCore.Relational
is the correct solution, as Ivan says.
Solution 2 - Entity Framework
You should add the nuget package Microsoft.EntityFrameworkCore.SqlServer
, since this is a Microsoft SQL method.
Solution 3 - Entity Framework
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
Solution 4 - Entity Framework
For net core 3.1, need to install these packages:
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Relational
Solution 5 - Entity Framework
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);
Solution 6 - Entity Framework
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)
- I upgraded Microsoft.EntityFrameworkCore via nuget
- I did install Microsoft.EntityFrameworkCore.Tools which didn't work for
ToTable
, unknown if this is even needed - I did then install the
Microsoft.EntityFrameworkCore.Relational
and it now resolves
Solution 7 - Entity Framework
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.
Solution 8 - Entity Framework
Ensure that the correct project is selected in the "Default Project" dropdown, and then type the command below :
install-package microsoft.entityframeworkcore.sqlserver
Solution 9 - Entity Framework
Install
Microsoft.EntityFrameworkCore
,MicrosoftEntityFramworkCore.Tools
, and finallyMicrosoft.EntityFrameworkCore.SqlServer
Solution 10 - Entity Framework
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.
Solution 11 - Entity Framework
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");
});
Solution 12 - Entity Framework
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.
Solution 13 - Entity Framework
Install this package - "Microsoft.EntityFrameworkCore.Relational" package, I was facing the same and it's been resolved by installing this package.
Find this with the most suitable version of your projects and install it. An error will automatically be resolved.
For more about it is : It's a part of assembly "Microsoft.EntityFrameworkCore.Relational" > Microsoft.EntityFrameworkCore > public static class RelationalEntityTypeBuilderExtensions > EntityTypeBuilder.ToTable