如何设置一个外键引用EF中的多个主键

时间:2018-01-02 10:56:51

标签: sql entity-framework data-annotations code-first ef-fluent-api

我使用代码优先方法。我想在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; }    
}

以及我的BaseClassPerson类:

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; }
}

0 个答案:

没有答案