我有以下 3 个表:
public class Project
{
// other fields...
public virtual ICollection<Attachment> Attachments { get; set; }
}
public class Experiment
{
// other fields...
public virtual ICollection<Attachment> Attachments { get; set; }
}
public class Attachment
{
// ...
}
如果我创建此结构,EF 将创建包含两列的表Attachments : ProjectId和ExperimentId。
我如何告诉 EF 我的关系 Experiment->Attachment 和 Project->Attachment 必须在 Attachment 上“共享”相同的键?
就像是:
public class Attachment
{
// other fields...
// can be a Guid from either Experiment or Project
public Guid BelongingModelId { get; set; }
// I will set this manually in order to know from which table the Guid is coming
public String BelongingModelType { get; set; }
}
有可能做这样的事情吗?
我在 DbContext / OnModelCreating 中尝试过,但没有找到解决方案。
谢谢,圭多