由于我正在尝试更改代码第一个应用程序以适应多租户demads,我基本上为所有表添加了额外的键列(并创建了复合主键):
[Key, Column(Order = 1)]
public int CompanyID { get; set; }
public class Worker
{
[Key, Column(Order = 0)]
public int WorkerID{ get; set; }
[Key, Column(Order = 1)]
public int CompanyID { get; set; }
[Required]
public string Name { get; set; }
public virtual int ActivityId { get; set; }
public virtual Activity SomeActivity { get; set; }
}
public class Activity
{
[Key, Column(Order = 0)]
public int ActivityID { get; set; }
[Key, Column(Order = 1)]
public int CompanyID { get; set; }
[Required]
[Display(Name = "Desc.")]
public virtual string Description{ get; set; }
}
当EF尝试生成数据库时,我收到错误:
*从表Worker(CompanyID,ActivityId)到表Activity(ActivityID,CompanyID)的外键约束'Activity_ActActivityt'::映射不足:外键必须映射到参与概念上的外键关联的某些AssociationSet或EntitySets侧。*
您能否告诉我如何映射这两个表,并考虑到每个用户都要选择一个活动。
谢谢, tihi
答案 0 :(得分:2)
我相信你必须使用数据注释显式设置外键映射(而不是依赖于约定),但是我不完全确定这是否有效,因为你有一个FK与PK的重叠。理论上它应该是:)
public class Worker
{
[Key, Column(Order = 0)]
public int WorkerID{ get; set; }
[Key, Column(Order = 1)]
[ForeignKey("SomeActivity")]
public int CompanyID { get; set; }
[Required]
public string Name { get; set; }
[ForeignKey("SomeActivity")]
public virtual int ActivityId { get; set; }
public virtual Activity SomeActivity { get; set; }
}