OnModelCreating undefined in Entity Framework 7

asp.net asp.net-core entity-framework entity-framework-core

Question

I am trying to setup my models' relations in EF7, but I faced the problem: OnModelCreating method and DbModelBuilder are undefined.

At past, I used EF6, but now I try to migrate to EF7.

Here is my code

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
        //Section -> many Category
        modelBuilder.Entity<Section>()
            .HasMany<Category>(p => p.Categories)
            .WithRequired(p => p.Section);

        //Section -> many PriceCategory
        modelBuilder.Entity<Section>()
            .HasMany<PriceCategory>(p => p.PriceCategories)
            .WithRequired(p => p.Section);

        //Category - many Procedures
        modelBuilder.Entity<Category>()
            .HasMany<Procedure>(p => p.Procedures)
            .WithRequired(p => p.Category);

            //PriceCategory - many PriceProcedures
            modelBuilder.Entity<PriceCategory>()
            .HasMany<PriceProcedure>(p => p.PriceProcedures)
            .WithRequired(p => p.PriceCategory);
}

My imports:

using Microsoft.Data.Entity;
using Domain.Models;

My project.json:

{
  "version": "1.0.0-*",
  "description": "Domain Class Library",
  "authors": [ "Garrus" ],
  "tags": [ "" ],
  "projectUrl": "",
  "licenseUrl": "",

    "dependencies": {
        "EntityFramework.Commands": "7.0.0-rc1-final",
        "EntityFramework.Core": "7.0.0-rc1-final",
        "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
        "Microsoft.CSharp": "4.0.1-beta-23516",
        "System.Collections": "4.0.11-beta-23516",
        "System.Linq": "4.0.1-beta-23516",
        "System.Runtime": "4.0.21-beta-23516",
        "System.Threading": "4.0.11-beta-23516"
    },

  "frameworks": {
    "net451": { },
    "dnxcore50": {}
  }
}

Can you help me? Maybe I forgot some NuGet package or there is another way to setup model relations in EF7?

Accepted Answer

Should be like this:

protected override void OnModelCreating(ModelBuilder modelBuilder)
 {
    ....
 }

I guess DbModelBuilder was renamed to ModelBuilder


Popular Answer

Well... I've got lots of probllems, so I create new ASP.NET 5 MVC project , copy my old models, controllers, viewa etc there, and It all be OK. (I think, this a strange magic)

Here that overrided method, all is OK

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
        }

My usings is same as in question. And project.json of Domain, maybe It can be useful for people, who face same error.

{
  "version": "1.0.0-*",
  "description": "Domain Class Library",
  "authors": [ "Garrus" ],
  "tags": [ "" ],
  "projectUrl": "",
    "licenseUrl": "",

    "dependencies": {
        "Microsoft.CSharp": "4.0.1-beta-23516",
        "System.Collections": "4.0.11-beta-23516",
        "System.Linq": "4.0.1-beta-23516",
        "System.Runtime": "4.0.21-beta-23516",
        "System.Threading": "4.0.11-beta-23516",
        "EntityFramework.Core": "7.0.0-rc1-final",
        "EntityFramework.Commands": "7.0.0-rc1-final",
        "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final"
    },

  "frameworks": {
    "net451": { },
    "dnxcore50": { }
  }
}


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why