本文介绍了实体框架7中未定义的OnModelCreating的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在EF7中设置模型的关系,但是我遇到了问题:OnModelCreating方法和DbModelBuilder未定义.

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

过去,我使用EF6,但是现在我尝试迁移到EF7.

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

这是我的代码

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);
}

我的进口商品:

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

我的project.json:

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

你能帮我吗?也许我忘记了一些NuGet软件包,或者还有另一种方法可以在EF7中设置模型关系?

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

推荐答案

应该是这样的:

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

我猜DbModelBuilder被重命名为ModelBuilder

I guess DbModelBuilder was renamed to ModelBuilder

这篇关于实体框架7中未定义的OnModelCreating的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 06:59