实体框架代码首先自引用有效负载的父子 [英] Entity Framework Code First Self Referencing Parent Child with Payload
问题描述
我试图在实体框架中首先使用代码进行设置,并遇到困难。描述我想要完成的工作:
拥有一个Product的实体。该产品可选地可以具有一个或多个相关的小孩产品。一个产品可以是一个或多个母产品的孩子。
当我去生成一个绑定到模型类产品的控制器时,我收到一个错误:(更新,更具体地说,匹配下面的代码) p>
运行所选代码生成器时出错:
'无法检索 ProductCatalog.Models.Product。
不支持每种类型的多个对象集。对象集
'Product'和'Products'都可以包含类型为
'ProductCatalog.Models.Product'的实例。
这里是不工作的模型类:
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.ComponentModel.DataAnnotations.Schema;
使用System.Data.Entity;
命名空间ProductCatalog.Models
{
//产品
public class Product
{
[Key]
public int ProductId {get;组; } // ProductID(主键)
public string ProductName {get;组; } // ProductName
public string ProductSku {get;组; } // ProductSKU
public int BaseQuantity {get;组; } // BaseQuantity
public decimal BaseCost {get;组; } // BaseCost
//反向导航
public virtual ICollection< RelatedProduct> ParentProducts {get;组; } // RelatedProduct.FK_RelatedProductChildID
public virtual ICollection< RelatedProduct>儿童产品{get;组; } // RelatedProduct.FK_RelatedProductParentID
public virtual ICollection< RelatedProduct>相关产品{get;组;
//相关产品
public class RelatedProduct
{
[Key,Column(Order = 0)]
public int ParentId {get;组; } // ParentID
[Key,Column(Order = 1)]
public int ChildId {get;组; } // ChildID
public int Quantity {get;组; } //数量
public bool必需{get;组; } //必需
public bool Locked {get;组; } //锁定
//外键
public virtual Product ParentProduct {get;组; } // FK_RelatedProductParentID
public virtual Product ChildProduct {get;组; } // FK_RelatedProductChildID
}
public class ProductDBContext:DbContext
{
public IDbSet< Product>产品{get;组; } // Product
public IDbSet< RelatedProduct>相关产品{get;组; } //相关产品
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity< RelatedProduct>()
.HasRequired(a => a.ParentProduct)
.WithMany(b => b.ChildProducts)
.HasForeignKey(c => c.ParentId)// FK_RelatedProductParentID
.WillCascadeOnDelete(false);
modelBuilder.Entity< RelatedProduct>()
.HasRequired(a => a.ChildProduct)
.WithMany(b => b.ParentProducts)
.HasForeignKey(c => c.ChildId); // FK_RelatedProductChildID
}
}
}
通过使用System.Collections.Generic复用DbSets
使用System.ComponentModel.DataAnnotations;
使用System.ComponentModel.DataAnnotations.Schema;
使用System.Data.Entity;
命名空间ProductCatalog.Models
{
//产品
public class Product
{
[Key]
public int ProductId {get;组; } // ProductID(主键)
public string ProductName {get;组; } // ProductName
public string ProductSku {get;组; } // ProductSKU
public int BaseQuantity {get;组; } // BaseQuantity
public decimal BaseCost {get;组; } // BaseCost
//反向导航
public virtual ICollection< RelatedProduct> ParentProducts {get;组; } // RelatedProduct.FK_RelatedProductChildID
public virtual ICollection< RelatedProduct>儿童产品{get;组; } // RelatedProduct.FK_RelatedProductParentID
public virtual ICollection< RelatedProduct>相关产品{get;组;
//相关产品
public class RelatedProduct
{
[Key,Column(Order = 0)]
public int ParentId {get;组; } // ParentID
[Key,Column(Order = 1)]
public int ChildId {get;组; } // ChildID
public int Quantity {get;组; } //数量
public bool必需{get;组; } //必需
public bool Locked {get;组; } //锁定
//外键
public virtual Product ParentProduct {get;组; } // FK_RelatedProductParentID
public virtual Product ChildProduct {get;组; } // FK_RelatedProductChildID
}
public class ProductDBContext:DbContext
{
public IDbSet< Product>产品{get;组; } // Product
public IDbSet< RelatedProduct>相关产品{get;组; } //相关产品
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity< RelatedProduct>()
.HasRequired(a => a.ParentProduct)
.WithMany(b => b.ChildProducts)
.HasForeignKey(c => c.ParentId)// FK_RelatedProductParentID
.WillCascadeOnDelete(false);
modelBuilder.Entity< RelatedProduct>()
.HasRequired(a => a.ChildProduct)
.WithMany(b => b.ParentProducts)
.HasForeignKey(c => c.ChildId); // FK_RelatedProductChildID
}
}
}
I'm attempting to set this up using code first in entity framework and am running into difficulty. To describe what i'm trying to accomplish:
Have an entity of Product. This product optionally may have one or more related "child" products. A product can be the child to one or more parent products.
when I go to generate a controller tied to the model class "Product", i'm getting an error: (updated, more specific, matches code below)
There was an error running the selected code generator: 'Unable to retrieve metadata for 'ProductCatalog.Models.Product'. Multiple object sets per type are not supported. The object sets 'Product' and 'Products' can both contain instances of type 'ProductCatalog.Models.Product'.
here's the not working model class:
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
namespace ProductCatalog.Models
{
// Product
public class Product
{
[Key]
public int ProductId { get; set; } // ProductID (Primary key)
public string ProductName { get; set; } // ProductName
public string ProductSku { get; set; } // ProductSKU
public int BaseQuantity { get; set; } // BaseQuantity
public decimal BaseCost { get; set; } // BaseCost
// Reverse navigation
public virtual ICollection<RelatedProduct> ParentProducts { get; set; } // RelatedProduct.FK_RelatedProductChildID
public virtual ICollection<RelatedProduct> ChildProducts { get; set; } // RelatedProduct.FK_RelatedProductParentID
public virtual ICollection<RelatedProduct> RelatedProducts { get; set; }
}
// RelatedProduct
public class RelatedProduct
{
[Key, Column(Order = 0)]
public int ParentId { get; set; } // ParentID
[Key, Column(Order = 1)]
public int ChildId { get; set; } // ChildID
public int Quantity { get; set; } // Quantity
public bool Required { get; set; } // Required
public bool Locked { get; set; } // Locked
// Foreign keys
public virtual Product ParentProduct { get; set; } // FK_RelatedProductParentID
public virtual Product ChildProduct { get; set; } // FK_RelatedProductChildID
}
public class ProductDBContext : DbContext
{
public IDbSet<Product> Product { get; set; } // Product
public IDbSet<RelatedProduct> RelatedProduct { get; set; } // RelatedProduct
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<RelatedProduct>()
.HasRequired(a => a.ParentProduct)
.WithMany(b => b.ChildProducts)
.HasForeignKey(c => c.ParentId) // FK_RelatedProductParentID
.WillCascadeOnDelete(false);
modelBuilder.Entity<RelatedProduct>()
.HasRequired(a => a.ChildProduct)
.WithMany(b => b.ParentProducts)
.HasForeignKey(c => c.ChildId); // FK_RelatedProductChildID
}
}
}
fixed by pluralizing the DbSets
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
namespace ProductCatalog.Models
{
// Product
public class Product
{
[Key]
public int ProductId { get; set; } // ProductID (Primary key)
public string ProductName { get; set; } // ProductName
public string ProductSku { get; set; } // ProductSKU
public int BaseQuantity { get; set; } // BaseQuantity
public decimal BaseCost { get; set; } // BaseCost
// Reverse navigation
public virtual ICollection<RelatedProduct> ParentProducts { get; set; } // RelatedProduct.FK_RelatedProductChildID
public virtual ICollection<RelatedProduct> ChildProducts { get; set; } // RelatedProduct.FK_RelatedProductParentID
public virtual ICollection<RelatedProduct> RelatedProducts { get; set; }
}
// RelatedProduct
public class RelatedProduct
{
[Key, Column(Order = 0)]
public int ParentId { get; set; } // ParentID
[Key, Column(Order = 1)]
public int ChildId { get; set; } // ChildID
public int Quantity { get; set; } // Quantity
public bool Required { get; set; } // Required
public bool Locked { get; set; } // Locked
// Foreign keys
public virtual Product ParentProduct { get; set; } // FK_RelatedProductParentID
public virtual Product ChildProduct { get; set; } // FK_RelatedProductChildID
}
public class ProductDBContext : DbContext
{
public IDbSet<Product> Products { get; set; } // Product
public IDbSet<RelatedProduct> RelatedProducts { get; set; } // RelatedProduct
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<RelatedProduct>()
.HasRequired(a => a.ParentProduct)
.WithMany(b => b.ChildProducts)
.HasForeignKey(c => c.ParentId) // FK_RelatedProductParentID
.WillCascadeOnDelete(false);
modelBuilder.Entity<RelatedProduct>()
.HasRequired(a => a.ChildProduct)
.WithMany(b => b.ParentProducts)
.HasForeignKey(c => c.ChildId); // FK_RelatedProductChildID
}
}
}
这篇关于实体框架代码首先自引用有效负载的父子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!