[英]Entity Framework How mapping with condition?
我有三個班級的學生,教師和文檔。 學生和老師可能有很多文件
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; }
}
我使用實體框架(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");
}
就像我非常確定您在大多數DBMS(例如sql server)中沒有一個一樣,您在EF中不能有條件外鍵。 您有2個選擇:
document
實體中為每個關系創建nullable
為nullable
列。 您可以在數據庫中添加檢查約束,以確保恰好一個關系鍵具有值(每個記錄)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.