14

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?

1
  • I've just known ,that OnConfiguring method is undefined too. Commented Nov 19, 2015 at 18:27

2 Answers 2

39

Should be like this:

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

I guess DbModelBuilder was renamed to ModelBuilder

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

Comments

1

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": { }
  }
}

Comments

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.