我有三个班级的学生,老师和文件。学生和教师可能有很多文件
public class BaseEntity
{
public int ItemId { get; set; }
public bool IsDeleted { get; set; }
}
//ParentType Student = 1
public class Student : BaseEntity
{
public string Name { get; set; }
public string Surname { get; set; }
public ICollection<Document> Documents { get; set; }
}
//ParentType Teacher = 2
public class Teacher : BaseEntity
{
public string Name { get; set; }
public string Surname { get; set; }
public ICollection<Document> Documents { get; set; }
}
public class Document
{
public int ParentId { get; set; } //Foreign Key
public int ParentTypeId { get; set; }
}
我使用Entity Framework(Fluent API)。例如,我为学生创建地图,我不知道如何在学生中使用两个条件(where parentId = itemId and ParentType = 1)
配置文档?
public class StudentMap : EntityTypeConfiguration<Student>
{
public StudentMap()
{
ToTable("Student", "dbo");
// Primary Key
HasKey(t => new {t.ItemId});
// Properties
Property(t => t.ItemId).HasColumnName("ItemId");
Property(t => t.IsDeleted)
.IsRequired()
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None)
.HasColumnName("IsDeleted");
Property(t => t.Name)
.IsRequired()
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None)
.HasColumnName("Name");
Property(t => t.Surname)
.IsRequired()
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None)
.HasColumnName("Surname");
}
答案 0 :(得分:1)
您不能在EF中拥有条件外键,就像我很确定您在大多数DBMS(如sql server)中都没有。您有两个选择:
nullable
实体中为每个关系创建一个document
列。您可以在数据库中添加检查约束,以确保一个关系密钥只有一个值(每条记录)。