OnModelCreating在Entity Framework 7中未定義

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

我試圖在EF7中設置模型的關係,但我遇到了問題: OnModelCreating方法和DbModelBuilder未定義。

過去,我使用EF6,但現在我嘗試遷移到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:

{
  "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中設置模型關係的方法?

一般承認的答案

應該是這樣的:

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

我想DbModelBuilder已重命名為ModelBuilder


熱門答案

嗯...我有很多問題,所以我創建了新的ASP.NET 5 MVC項目 ,複製我的舊模型,控制器,視圖等等,一切都好。 ( 我想,這是一個奇怪的魔法

這個覆蓋方法,一切都好

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

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

我的用法與問題相同。和Domain的project.json一樣,對於面臨同樣錯誤的人來說,它可能很有用。

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



許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因