【发布时间】:2017-08-17 11:31:48
【问题描述】:
我有几个实体,我想提供存储文件的可能性。就像对于给定的客户,存储其身份证或产品的副本,存储其进口文件和质量证书。
我不希望将这些文件存储在相同的实体行中,并且我为附件创建了一个单独的表。解耦它还可以让我稍后决定是否要直接存储文件、存储位置或某些 CMS 访问令牌。
那么,我如何在 EF 中建立此附件表和可能具有该功能的所有其他表之间的这种关系?
架构示例:
public class Customer
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
public class Product
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
public class Attachment
{
[Key]
public int Id { get; set; }
//Relation property
public int ParentId { get; set; }
public string Name { get; set; }
public byte[] Content { get; set; }
}
【问题讨论】:
-
添加
public Attachment Attachment { get; set;}到你的班级,你有什么困惑? -
你的意思是
public virtual ICollection<Attachment> Attachments { get; set; }?这样做的问题是它没有通过“ParentId”连接表,然后它确实在附件表中为每个链接表创建了一个 CustomerId、ProductId...一个字段。 -
作为跨表唯一的 guid,应该是一种解决与目标表中的一个字段的关系的方法。还是我错了?
标签: c# mysql entity-framework ef-code-first entity-framework-core