我正在尝试设置我的模型' EF7中的关系,但我遇到了问题:DbModelBuilder
方法和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);
}
未定义。
过去,我使用的是EF6,但现在我尝试迁移到EF7。
这是我的代码
using Microsoft.Data.Entity;
using Domain.Models;
我的进口商品:
{
"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": {}
}
}
我的project.json:
<?xml version="1.0"?>
<catalog>
<book id="bk101" type="hardcover">
<info author="Gambardella, Matthew" title="XML Developer's Guide" genre="Computer" price="44.95" publish_date="2000-10-01" description="An in-depth look at creating applications
with XML." />
</book>
<book id="bk102" type="softcover">
<info author="Ralls, Kim" title="Midnight Rain" genre="Fantasy" price="5.95" publish_date="2000-10-01" description="A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world." />
</book>
<book id="bk101" type="softcover">
<info author="Corets, Eva" title="Maeve Ascendant" genre="Fantasy" price="5.95" publish_date="2000-11-17" description="After the collapse of a nanotechnology
society in England, the young survivors lay the
foundation for a new society." />
</book>
</catalog>
你能帮帮我吗?也许我忘记了一些NuGet包或者还有另一种在EF7中设置模型关系的方法吗?
答案 0 :(得分:36)
应该是这样的:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
....
}
我猜DbModelBuilder已重命名为ModelBuilder
答案 1 :(得分:1)
嗯......我有很多问题,所以我创建了新的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": { }
}
}