EntityTypeBuilder does not contain a definition for ToTable in EF Core

耗尽温柔 提交于 2019-12-05 08:26:48

问题


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:


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




回答2:


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



回答3:


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



回答4:


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);



回答5:


need to add package (nuget) Microsoft.EntityFrameworkCore.SqlServer as this is a MS Sql method




回答6:


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

If your .Net Core 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.



来源:https://stackoverflow.com/questions/43200184/entitytypebuilder-does-not-contain-a-definition-for-totable-in-ef-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!