Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EntityTypeBuilder does not contain a definition for ToTable in EF Core

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?

like image 496
g_b Avatar asked Apr 04 '17 06:04

g_b


People also ask

What is ModelBuilder entity?

The ModelBuilder is the class which is responsible for building the Model. The ModelBuilder builds the initial model from the entity classes that have DbSet Property in the context class, that we derive from the DbContext class. It then uses the conventions to create primary keys, Foreign keys, relationships etc.

Which inheritance hierarchy is support in EF core?

By default, EF maps the inheritance using the table-per-hierarchy (TPH) pattern.

What is the difference between EF6 and EF core?

Keep using EF6 if the data access code is stable and not likely to evolve or need new features. Port to EF Core if the data access code is evolving or if the app needs new features only available in EF Core. Porting to EF Core is also often done for performance.

Is EF core faster than EF6?

EF Core 6.0 itself is 31% faster executing queries. Heap allocations have been reduced by 43%.


Video Answer


2 Answers

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

like image 104
Mardoxx Avatar answered Oct 02 '22 07:10

Mardoxx


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

like image 36
Aaron Avatar answered Oct 02 '22 07:10

Aaron