【问题标题】:EF6 database architecture for shared entities: products2images, categories2images共享实体的 EF6 数据库架构:products2images、categories2images
【发布时间】:2015-06-22 23:26:46
【问题描述】:

我在工作生产应用程序上有三个主要实体:产品、类别和 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,重写所有内容
• 取其轻而易举,实施混合解决方案
• 看看更绝地解决方案

我可能面临什么样的性能困难?

感谢任何建议、链接和文档。

【问题讨论】:

    标签: c# database entity-framework


    【解决方案1】:

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

    【讨论】:

    • 不,先生。我当前的数据库是 mssql 2012,我现在不打算更改它,但也许我应该
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    • 2012-08-27
    • 2014-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多