共享实体的EF6数据库架构:products2images,categories2images

时间:2015-06-22 13:34:26

标签: c# database entity-framework

我在工作生产应用程序上有三个主要实体:Product,Category和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
}

这意味着由于主要实体键类型的变化而重写整个应用程序,我对性能有疑问 图像 - 100 000 - 200 000个条目
产品 - 30 000 - 50 000个条目

我看到的第二个解决方案是将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; }
}

第二种解决方案的不利之处在于,与非主要关键属性建立一对一的关系是不可能的,而我所看到的唯一方法就是创建一对多关注并保持谨慎db安全的代码 - 不好。

所以,我的问题是我必须做什么? •在没有实施任何建筑重构的情况下,采取啤酒并以时尚的方式进行 •必须鼓起勇气实施perfect solution并重写所有内容 •采取两个邪恶中的较小者并实施混合解决方案
•看看更多的绝地解决方案

我可能遇到什么样的表现困难?

任何建议,链接和文档都表示赞赏。

1 个答案:

答案 0 :(得分:0)

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

相关问题