Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否可以对多对多关系使用自有类型?_C#_.net Core_Entity Framework Core_Ef Core 2.2 - Fatal编程技术网

C# 是否可以对多对多关系使用自有类型?

C# 是否可以对多对多关系使用自有类型?,c#,.net-core,entity-framework-core,ef-core-2.2,C#,.net Core,Entity Framework Core,Ef Core 2.2,在Entity Framework核心版本2.2或3.0中,是否可以使用自有/复杂类型,从而实现此类配置: public class Product { public int Id { get; set; } public string Name { get; set; } public ProductProperties Properties { get; set; } } public class ProductProperties { public L

在Entity Framework核心版本2.2或3.0中,是否可以使用自有/复杂类型,从而实现此类配置:

public class Product {

   public int Id { get; set; }
   public string Name { get; set; }
   public ProductProperties Properties { get; set; }
}

public class ProductProperties {

       public List<ProductSize> Sizes { get; set; }
}

public class Size {

       public int Id { get; set; }
       public string Name { get; set; }
}

public class ProductSize {

       public int ProductId { get; set; }
       public Product Product { get; set; }

       public int SizeId { get; set; }
       public Size Size { get; set; }
}

modelBuilder.Entity<ProductSize>()
                .HasOne(x => x.Product)
                .WithMany(x => x.Properties.Sizes)
                .HasForeignKey(x => x.ProductId);

modelBuilder.Entity<ProductSize>()
                .HasOne(x => x.Size)
                .WithMany()
                .HasForeignKey(x => x.SizeId);
早些时候找到的答案几乎与我的问题完全吻合,但这是2013年发布的。到那时,这几乎肯定是不可能的


Microsoft上的源代码仅提供了创建具有复杂类型本身的实体的示例,而不是创建它们之间的关系的示例。

您可以查看2019年发布的自有实体类型

链接中的示例如下所示:

public class Distributor
{
    public int Id { get; set; }
    public ICollection<StreetAddress> ShippingCenters { get; set; }
}
公共类分发服务器
{
公共int Id{get;set;}
公共ICollection发货中心{get;set;}
}
owns many函数应该可以帮助您做到以下几点:

modelBuilder.Entity<Distributor>().OwnsMany(p => p.ShippingCenters, a =>
{
    a.WithOwner().HasForeignKey("OwnerId");
    a.Property<int>("Id");
    a.HasKey("Id");
});
modelBuilder.Entity().OwnsMany(p=>p.ShippingCenters,a=>
{
a、 WithOwner().HasForeignKey(“OwnerId”);
a、 财产(“Id”);
a、 HasKey(“Id”);
});
如果我误解了您的问题,请告诉我。

问题的原因 在您的示例代码中,很明显没有特定的多对多关系。为了使我的论点更具说服力,以下是你们实体及其关系的模型:

新的阶级结构 对于要在EF中工作的多对多关系,乘积表和大小表需要通过单个连接表彼此具有隐式关系。在我提出的解决方案中,我选择了ProductProperty表。在这里,我添加了productsize连接表中的字段:

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }

    public ICollection<ProductProperty> Properties { get; set; }
}

public class ProductProperty
{
    public int ProductId { get; set; }
    public Product Product { get; set; }

    public int SizeId { get; set; }
    public Size Size { get; set; }
}

public class Size 
{
    public int Id { get; set; }
    public string Name { get; set; }

    public ICollection<ProductProperty> Properties { get; set; }
}
作为最后一个参数,此更改将使更改现有属性或添加新属性更容易

来源

  • 感谢您的回答,我已经阅读了此文档,但不幸的是,它没有涵盖多对多关系的情况。因为对于EF core,我们需要创建一个连接表。您描述的情况是我为“产品”实体本身所做的。谢谢您的回答。这确实是我删除复杂类型“ProductProperties”的结果。但我之所以创建这个复杂类型,是因为其中有5种不同的产品属性。我试图清理/重构我的代码,以避免在产品类中使用长构造函数。也许现在更清楚了?@Faegav在回答中查看我的建议,尽管我感谢你的努力,但这并不是对我问题的直接回答。因为我有我的理由按照设计的方式来设计它。只是为了你的信息;如果我遵循你的方法,我将无法处理所有不同的属性。案例相当复杂,大小是我命名的最简单的属性。因此,我最感兴趣的是在efcore中包含一个复杂类型(1对1),然后在关系中使用该类型内的属性。(并非所有属性都适用于所有产品“类型”)
    public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
    
        public ICollection<ProductProperty> Properties { get; set; }
    }
    
    public class ProductProperty
    {
        public int ProductId { get; set; }
        public Product Product { get; set; }
    
        public int SizeId { get; set; }
        public Size Size { get; set; }
    }
    
    public class Size 
    {
        public int Id { get; set; }
        public string Name { get; set; }
    
        public ICollection<ProductProperty> Properties { get; set; }
    }
    
    modelBuilder.Entity<ProductProperty>()
        .HasKey(pp => new { pp.ProductId, pp.SizeId });
    
    modelBuilder.Entity<ProductProperty>()
        .HasOne(pp => pp.Product)
        .WithMany(p => p.Properties)
        .HasForeignKey(pp => pp.ProductId);
    
    modelBuilder.Entity<ProductProperty>()
        .HasOne(pp => pp.Size)
        .WithMany(p => p.Properties)
        .HasForeignKey(pp => pp.SizeId);
    
    public class Property
    {
        public int Id { get; set; }
        public PropertyType propType { get; set; }
        public string propValue { get; set; }
    }
    
    public enum PropertyType
    {
        Size,
        Fontsize,
        ...
    }