6

This code is in MVC, I need to do something like this but in ASP.net Core, using Entity Framework Core, or with DataAnnotations (I've changed the parameter already from DbModelBuilder(MVC Entity Framework) to ModelBuilder(Entity Framework Core))

 protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>(); //error: Conventions is not recognized
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<tbl_Line>()
                .HasMany(d => d.tbl_Policy)
                .WithRequired(c => c.tbl_Line) //error WithRequired not recognized
                .HasForeignKey(c => c.int_lineID);
        }

There are some errors when I'm trying to use it in Entity Framework Core:

1-'ModelBuilder' does not contain a definition for 'Conventions' and no extension method 'Conventions' accepting a first argument of type 'ModelBuilder' could be found (are you missing a using directive or an assembly reference?)

2-'CollectionNavigationBuilder<tbl_Line, tbl_Policy>' does not contain a definition for 'WithRequired' and no extension method 'WithRequired' accepting a first argument of type 'CollectionNavigationBuilder<tbl_Line, tbl_Policy>' could be found (are you missing a using directive or an assembly reference?)

4
  • 1
    A good starting point - EF Core vs. EF6.x Commented Nov 9, 2016 at 17:54
  • @IvanStoev page not found. Even I want to use configurations to add a map in my ef core context file. Writing the mappings in the same file looks messy. Is there a way, or need to add it the same way AlexGh has done it. Commented Mar 30, 2017 at 23:00
  • @nakulchawla09 The recent documentation is here. Shortly, there is still no support for Conventions and/or Configurations in EF Core like in EF6 (AFAIK there are plans to be added, but I don't know when), so for now you are stuck with modelBuilder. Commented Mar 30, 2017 at 23:10
  • Thanks @IvanStoev. Will use the ModelBuilder for now then. Sigh... Commented Mar 30, 2017 at 23:13

1 Answer 1

2

Until there is support for ModelBuilder.Configurations I emulate the older Entity Framework construction with extension methods:

public static EntityTypeBuilder<Project> Map(this EntityTypeBuilder<Project> cfg)
{
    cfg.ToTable("sql_Projects");

    // Primary Key
    cfg.HasKey(p => p.Id);

    cfg.Property(p => p.Id)
        .IsRequired()
        .HasColumnName("ProjectId");

    return cfg;
}

and then call it from OnModelCreating like this...

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Project>().Map();
    base.OnModelCreating(modelBuilder);
}

It is a bit clumsy, but cleaner, I think, than trying to configure dozens of entities within the main DbContext.

Sign up to request clarification or add additional context in comments.

4 Comments

This method doesn't seem to exist.
Which method are you referring to?
@Nathan McKaskle: A few years late but I assume you mean ToTable. You need to add the Microsoft.EntityFrameworkCore.Relational package. I just had the same issue with ToView. Just adding this note here so others can save some time.
Wow I vaguely remember C#

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.