【问题标题】:How impliment one entity set that save images information for all other entites如何实现一个实体集来保存所有其他实体的图像信息
【发布时间】:2014-07-02 11:51:24
【问题描述】:

我有这样的事情:

    public class Product{ 
       [some properties] 
       public ICollection<ProductImage> ProductImages {set;get;}
    }
    public class ProductImages {
         string fileName,
         string size, ... 
         public Product Product{set;get}
    }

public class Poeple{ 
  [some properties] 
  public virtual ICollection<PersonImage> PersonImages {set;get;}
}
public class PersonImages{ 
  string fileName, string size, ... public Person Person{set;get} 
}

... 和其他实体有一个更多的表作为自己的图像的孩子;

我想从所有 ProductImages、PersonImages 和 ... 中获取摘要,并获取一个实体(表),如下所示:

public Images {
int size{set;get;} , 
string FileName{set; get;}, ... othes prop
}

但是将图像表的每条记录引用到其父级的最佳方法是什么

枚举类型作为图像实体的属性是否是一个很好的解决方案枚举类型,例如:

Enum BelongTo 
{
ProductTable,
GalleryTabl,
PersonTable,
}

public Images { 
   BelongTo belong {get;set;}
   int size {set; get;}
   string FileName {set; get;}
}

使用枚举我不能在父母和孩子之间拥有导航属性 什么是关注 DDD 的便捷解决方案

【问题讨论】:

  • 您可以为此在实体框架中使用复杂类型
  • 嗨@Aju Mon 我知道它适合一些像地址这样的道具。你是什​​么意思?你能解释更多吗?

标签: c# .net entity-framework entity domain-driven-design


【解决方案1】:

如果我理解得很好,我会说你有(至少)2 个选择

  • TPH
  • 一个图像表+多个链接表

TPH 会导致很多空列。

public class BaseImage { 
   public int size {set; get;}
   public string FileName {set; get;}
}

public class PersonImage : BaseIMage {
   [Required]
   public virtual Person Person {get; set;}
}

public class ProductImage : BaseIMage {
   [Required]
   public virtual Product Product {get; set;}
}

public class GalleryImage : BaseIMage {
   [Required]
   public virtual Gallery Gallery {get; set;}
}

这几乎等同于

public class Image {
   public int size {set; get;}
   public string FileName {set; get;}

   [Optional]
   public virtual Person Person {get; set;}
   [Optional]
   public virtual Product Product {get; set;}
   [Optional]
   public virtual Gallery Gallery {get; set;}
}

===== =====

或者你可以使用一个 Image 类和 ManyToMany 关系

public class Image {
   public int size {set; get;}
   public string FileName {set; get;}

   public virtual ICollection<Person> Persons {get; set;}
   public virtual ICollection<Product> Products {get; set;}
   public virtual ICollection<Gallery> Galleries {get; set;}
}

public class Person {
    ....
    public virtual ICollection<Image> Images {get; set;}
}

在我看来,这似乎是处理一个图像绑定到多个人员和多个产品的最干净的方案...

【讨论】:

  • 是否有可能有一个带有“可变”父级的子表;例如,在我的场景中,imageTable 中的每个图像记录都必须知道 parentId 和 tableId;可以在 EF 中实现吗?
  • AFAIK,答案是否定的。
猜你喜欢
  • 1970-01-01
  • 2017-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-14
  • 1970-01-01
  • 2015-05-23
相关资源
最近更新 更多