OrElse
OrElse

Reputation: 9969

No overload for method 'Entity' takes 1 arguments in Entity framework

Within my DBContext i have overriden the OnModelCreating method.

The same code works like a charm in .Net Core (by using ModelBuilder) but

on .NET Framework 4 it does not compile.

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        modelBuilder.Entity<User>(entity =>
        {
            entity.Property(e => e.CreatedBy)
                .IsRequired()
                .HasMaxLength(500);
        }
    }

The error i get is

No overload for method 'Entity' takes 1 arguments in Entity framework

Unfortunately i cannot figure out what is wrong with this one. Any help would be greatly appreciated.

Upvotes: 0

Views: 2280

Answers (2)

Hamed Moghadasi
Hamed Moghadasi

Reputation: 1673

The same code works like a charm in .Net Core (by using ModelBuilder) but on .NET Framework 4, it does not compile.

In comparison of .net core with .net framework some method implementatios are diffrent. so it's the reason of your compile error. As a solution, just re-write your code as follow:

modelBuilder.Entity<User>()
.Property(e => e.CreatedBy)
.IsRequired()
.HasMaxLength(500);

base your need in the comment, we can manage more than one property in this method:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

    modelBuilder.Entity<User>(entity =>
    {
        entity.Property(e => e.CreatedBy)
            .IsRequired()
            .HasMaxLength(500);

        entity.Property(e => e.ModifiedBy)
        .IsRequired();
    }
}

good luck.

Upvotes: 1

Johnathan Barclay
Johnathan Barclay

Reputation: 20373

EF Core was a complete re-write of Entity Framework; although there are many similarities, much of the entity configuration needs to be done differently.

The above would need to be:

modelBuilder.Entity<User>()
    .Property(e => e.CreatedBy)
    .IsRequired()
    .HasMaxLength(500);

Upvotes: 1

Related Questions