Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnModelCreating undefined in Entity Framework 7

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?

like image 876
Alexey Markov Avatar asked Nov 19 '15 18:11

Alexey Markov


People also ask

What is OnModelCreating in Entity Framework?

The DbContext class has a method called OnModelCreating that takes an instance of ModelBuilder as a parameter. This method is called by the framework when your context is first created to build the model and its mappings in memory.

Is OnModelCreating required?

OnModelCreating is not necessary, but at the same time will not hurt if called - that's why Sometimes it's there, sometimes not. Sometimes at the beginning of the method, other times at the end.

Where do you override OnModelCreating?

You can override the OnModelCreating method in your derived context and use the ModelBuilder API to configure your model. This is the most powerful method of configuration and allows configuration to be specified without modifying your entity classes.

How often is OnModelCreating called?

OnModelCreating will be called only once that's default behaviour. According to OnModelCreating documentation. Typically, this method is called only once when the first instance of a derived context is created. The model for that context is then cached and is for all further instances of the context in the app domain.


1 Answers

Should be like this:

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

I guess DbModelBuilder was renamed to ModelBuilder

like image 73
Joe Audette Avatar answered Sep 20 '22 13:09

Joe Audette