Tom Tom - 1 year ago 100
C# Question

Fluent Api in Entity Framework 6 not compatible Entity Framework Core

I had implemented Fluent API using Entity Framework 6. Have problems when implementing the same using EnityFrameworkCore.

Below is the code using Fluent API using EntityFramework 6

public class CustomerConfiguration : EntityTypeConfiguration<Customers>
public CustomerConfiguration()
Property(c => c.FirstName).IsRequired().HasMaxLength(50);
Property(c => c.LastName).IsRequired().HasMaxLength(50);
Property(c => c.Gender).IsRequired().HasMaxLength(10);
Property(c => c.Email).IsRequired().HasMaxLength(25);
Property(c => c.Address).IsRequired().HasMaxLength(50);
Property(c => c.City).IsRequired().HasMaxLength(25);
Property(c => c.State).IsOptional().HasMaxLength(15);


protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Configurations.Add(new CustomerConfiguration());
modelBuilder.Configurations.Add(new OrderConfiguration());
modelBuilder.Configurations.Add(new ProductConfiguration());

.HasRequired(c => c.Customers)
.WithMany(o => o.Orders)
.HasForeignKey(f => f.CustomerId);

.HasMany<Products>(s => s.Products)
.WithMany(c => c.Orders)
.Map(cs =>


The issues that I am getting in EntityFrameworkCore are

  1. It dosent recognize ToTable and Property keywords in CustomerConfiguration()

  2. It dosent recognize Configurations, HasRequired,MapLeftKey,MapRightKey,ToTable keyword in OnModelCreating method

Could somebody tell me how to achieve this using Fluent API in EntityFrameWork Core

Answer Source

EF core uses completely different APIs.So you must learn it first.

As an example : This is the way how it sets the ToTable().

protected override void OnModelCreating(ModelBuilder modelBuilder)

To learn you have to read these links :

Table Mapping


Creating a Model

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download