我使用代码优先方法。我想在ObjectId
中仅使用ObjectScore
属性作为与Professor
(Id),College
(Id)和EducationalGroup
(Id)三种关系的外键,就像在SQL上发生的那样
CONSTRAINT [FK_ObjectScore_Colege] FOREIGN KEY([ObjectId])
CONSTRAINT [FK_ObjectScore_Professer] FOREIGN KEY([ObjectId])
CONSTRAINT [FK_ObjectScore_EducationGroup] FOREIGN KEY([ObjectId])
如何在Entity Framework中建立这种关系(数据注释或流畅的api)?
全部谢谢
public class Professor : Person
{
public int ProfessorCode { get; set; }
public virtual ICollection<EducationalGroup> EducationalGroups { get; set; }
public virtual ICollection<College> Colleges { get; set; }
public virtual ICollection<ObjectScore> ObjectScores { get; set; }
}
public class College : BaseClass
{
public int CollegeCode { get; set; }
public virtual ICollection<Professor> Professors { get; set; }
public virtual ICollection<EducationalGroup> EducationalGroups { get; set; }
public virtual ICollection<ObjectScore> ObjectScores { get; set; }
}
public class EducationalGroup : BaseClass
{
public int CollegeCode { get; set; }
public virtual ICollection<Professor> Professors { get; set; }
public virtual College College { get; set; }
public virtual ICollection<ObjectScore> ObjectScores { get; set; }
}
public class ObjectScore
{
public Guid Id { get; set; }
public int CurrentScore { get; set; }
public virtual Term Term { get; set; }
public virtual Score Score { get; set; }
public int ObjectId { get; set; }
}
以及我的BaseClass
和Person
类:
public class Person : BaseClass
{
public string Family { get; set; }
}
public class BaseClass
{
public BaseClass()
{
IsActive = false;
}
public int Id { get; set; }
public string Name { get; set; }
public DateTime? CreationDate { get; set; }
public DateTime? LastModifiedDate { get; set; }
public bool IsActive { get; set; }
}