gpt4 book ai didi

entity-framework - 具有有效负载的 Entity Framework 代码优先自引用父子

转载 作者:行者123 更新时间:2023-12-04 07:24:50 25 4
gpt4 key购买 nike

我正在尝试首先在 Entity Framework 中使用代码进行设置,但遇到了困难。描述我想要完成的事情:

有一个 Product 实体。该产品可选地可以具有一个或多个相关的“子”产品。一个产品可以是一个或多个父产品的子产品。

当我去生成一个与模型类“产品”相关的 Controller 时,我收到一个错误:(更新,更具体,匹配下面的代码)

 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'.


这是不工作的模型类:
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

}
}
}

最佳答案

通过复数 DbSet 来修复

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

}
}
}

关于entity-framework - 具有有效负载的 Entity Framework 代码优先自引用父子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20916873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com