用于共享实体的EF6数据库体系结构:产品映像、类别映像

本文关键字:映像 体系结构 实体 共享 EF6 数据库 用于 | 更新日期: 2023-09-27 17:59:01

我在工作生产应用程序上有三个主要实体:产品、类别和HtmlPage

public class Product {
    [Key]
    public Guid Id {get;set;}
    //and other properties
}
public class Category {
    [Key]
    public int Id {get;set;}
    //and other properties
}
public class HtmlPage {
    [Key]
    public string Id {get;set;}
    //and other properties
}

和一个共享实体:图像

public class Image {
    public Guid Id {get;set;}
    //and other properties
}

所有主要实体都与像这样的共享实体有连接

public class ProductImage{
    public Guid ImageId {get;set;}
    public Guid ProductId {get;set;}
    //list of junction properies: order, isprimary and so on
}

我不喜欢这种方法,因为在有大量主要实体类型的情况下,很难管理代码和所有内容。在大量共享实体类型(评论、标签、投票等)的情况下,我看到了一些麻烦

问题是,我现在确实必须在现有架构中添加新的主实体和共享实体。

所以,我正在考虑数据库架构重构

我看到的ideal solution

public abstract class EntityProxy
{
    [Key]
    public Guid Id { get; set; }
    public virtual ICollection<EntityImageEntityProxy> Images { get; set; }
    public virtual ICollection<EntityTagEntityProxy> Tags { get; set; }
    public virtual ICollection<EntityCommentEntityProxy> Comments { get; set; }
}
// list of Shared Entities
public class EntityImage
{
    [Key]
    public Guid Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<EntityImageEntityProxy> Entities { get; set; }
}
public class EntityTag
{
    [Key]
    public Guid Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<EntityTagEntityProxy> Entities { get; set; }
}
public class EntityComment
{
    [Key]
    public Guid Id { get; set; }
    public string Text { get; set; }
    public virtual ICollection<EntityCommentEntityProxy> Entities { get; set; }
}
// shared entities to primary entities mapping
public class EntityImageEntityProxy
{
    [Key, Column(Order = 0)]
    public Guid ImageId { get; set; }
    [Key, Column(Order = 1)]
    public Guid EntityProxyId { get; set; }
    [ForeignKey("ImageId")]
    public virtual EntityImage Image { get; set; }
    [ForeignKey("EntityProxyId")]
    public virtual EntityProxy Entity { get; set; }
}
public class EntityTagEntityProxy
{
    [Key, Column(Order = 0)]
    public Guid TagId { get; set; }
    [Key, Column(Order = 1)]
    public Guid EntityProxyId { get; set; }
    [ForeignKey("TagId")]
    public virtual EntityTag Tag { get; set; }
    [ForeignKey("EntityProxyId")]
    public virtual EntityProxy Entity { get; set; }
}
public class EntityCommentEntityProxy
{
    [Key, Column(Order = 0)]
    public Guid CommentId { get; set; }
    [Key, Column(Order = 1)]
    public Guid EntityProxyId { get; set; }
    [ForeignKey("CommentId")]
    public virtual EntityComment Comment { get; set; }
    [ForeignKey("EntityProxyId")]
    public virtual EntityProxy Entity { get; set; }
}
// list of primary entities
[Table("EntityProxyProducts")]
public class EntityProxyProduct : EntityProxy
{
    //type properties
}
[Table("EntityProxyCategories")]
public class EntityProxyCategory : EntityProxy
{
    //type properties
}
[Table("EntityProxyHtmlPages")]
public class EntityProxyHtmlPage : EntityProxy
{
    //type properties
}

这意味着由于主实体键类型的变化,将重写整个应用程序,我对其性能表示怀疑
图片-100000-200000个条目
产品-30000-50000条

我看到的第二个解决方案是将ideal solution与现有的模型合并,如

    // lis of exists primary entities
public class Product
{
    [Key]
    public Guid Id { get; set; }
    public Guid? EntityProxyId { get; set; }
    [ForeignKey("EntityProxyId")]
    public virtual EntityProxyProduct EntityProxy { get; set; }
    //and other properties
}
public class Category
{
    [Key]
    public int Id { get; set; }
    public Guid? EntityProxyId { get; set; }
    [ForeignKey("EntityProxyId")]
    public virtual EntityProxyCategory EntityProxy { get; set; }
    //and other properties
}
public class HtmlPage
{
    [Key]
    public string Id { get; set; }
    public Guid? EntityProxyId { get; set; }
    [ForeignKey("EntityProxyId")]
    public virtual EntityProxyHtmlPage EntityProxy { get; set; }
    //and other properties
}

public abstract class EntityProxy
{
    [Key]
    public Guid Id { get; set; }
    public virtual ICollection<EntityImageEntityProxy> Images { get; set; }
    public virtual ICollection<EntityTagEntityProxy> Tags { get; set; }
    public virtual ICollection<EntityCommentEntityProxy> Comments { get; set; }
}
// list of Shared Entities
public class EntityImage
{
    [Key]
    public Guid Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<EntityImageEntityProxy> Entities { get; set; }
}
public class EntityTag
{
    [Key]
    public Guid Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<EntityTagEntityProxy> Entities { get; set; }
}
public class EntityComment
{
    [Key]
    public Guid Id { get; set; }
    public string Text { get; set; }
    public virtual ICollection<EntityCommentEntityProxy> Entities { get; set; }
}
// shared entities to primary entities mapping
public class EntityImageEntityProxy
{
    [Key, Column(Order = 0)]
    public Guid ImageId { get; set; }
    [Key, Column(Order = 1)]
    public Guid EntityProxyId { get; set; }
    [ForeignKey("ImageId")]
    public virtual EntityImage Image { get; set; }
    [ForeignKey("EntityProxyId")]
    public virtual EntityProxy Entity { get; set; }
}
public class EntityTagEntityProxy
{
    [Key, Column(Order = 0)]
    public Guid TagId { get; set; }
    [Key, Column(Order = 1)]
    public Guid EntityProxyId { get; set; }
    [ForeignKey("TagId")]
    public virtual EntityTag Tag { get; set; }
    [ForeignKey("EntityProxyId")]
    public virtual EntityProxy Entity { get; set; }
}
public class EntityCommentEntityProxy
{
    [Key, Column(Order = 0)]
    public Guid CommentId { get; set; }
    [Key, Column(Order = 1)]
    public Guid EntityProxyId { get; set; }
    [ForeignKey("CommentId")]
    public virtual EntityComment Comment { get; set; }
    [ForeignKey("EntityProxyId")]
    public virtual EntityProxy Entity { get; set; }
}
// list of primary entities proxies
[Table("EntityProxyProducts")]
public class EntityProxyProduct : EntityProxy
{
    public virtual ICollection<Product> Products { get; set; }
}
[Table("EntityProxyCategories")]
public class EntityProxyCategory : EntityProxy
{
    public virtual ICollection<Category> Categories { get; set; }
}
[Table("EntityProxyHtmlPages")]
public class EntityProxyHtmlPage : EntityProxy
{
    public virtual ICollection<HtmlPage> HtmlPages { get; set; }
}

第二个解决方案中的负数是不可能创建1到0。。一种与非主键属性的关系,我看到的唯一方法是像一对多一样创建它,并保护数据库不受代码的影响——这不好。

所以,我的问题是我必须做什么
•在不实施任何架构重构的情况下,喝一杯啤酒,按照自己的风格行事
•必须鼓起勇气实施perfect solution,重写一切
•两害相权取其轻,实行混合解决方案
•查看更多jedi解决方案

我可能会面临什么样的表现困难?

欢迎提供任何建议、链接和文档。

用于共享实体的EF6数据库体系结构:产品映像、类别映像

您似乎更喜欢使用以文档为中心的数据视图,您是否考虑过使用文档数据库而不是关系sql?