【发布时间】:2018-03-13 11:48:19
【问题描述】:
我有这样的 CodeFirst 设计:
public class Email
{
public string Address { get; set; }
public Company Company { get; set; }
public int? CompanyId { get; set; }
public User User { get; set; }
public int? UserId { get; set; }
//many other props
}
public class Company
{
public List<Email> Emails { get; set; }
}
public class User
{
public List<Email> Emails { get; set; }
}
在一个好的方面,Email 可以属于只有一个外键:CompanyId 或 UserId。但现在它允许 CompanyId 和 UserId。这是不对的。无论如何,带有可空值的设计是丑陋的。例如,要获取链接到我需要的公司的所有电子邮件,请执行以下操作:
var companyEmails = _context.Emails.Where(x => x.CompanyId.HasValue);
我觉得有一种更好的方法可以用 OR 逻辑定义乘法外键。请帮我想办法。
【问题讨论】:
标签: entity-framework database-design orm ef-code-first