我有以下内容:
public abstract class EntityBase
{
[Key]
public int Id { get; set; }
public DateTime? CreatedTime { get; set; }
public DateTime? ModifiedTime { get; set; }
}
public class AnalysisFile : EntityBase
{
public AnalysisFile()
{
DateAdded = DateTime.Now;
}
public string SourceFolder { get; set; }
public string Filename { get; set; }
public DateTime DateAdded { get; set; }
public long FileSize { get; set; }
public Document Document { get; set; }
}
public class Document : EntityBase
{
public Document()
{
Publish = true;
}
public string Name { get; set; }
public int AreaId { get; set; }
public int SchoolId { get; set; }
public int Year { get; set; }
public long FileSize { get; set; }
public AnalysisFile AnalysisFile { get; set; }
public School School { get; set; }
public Area Area { get; set; }
public bool Publish { get; set; }
}
modelBuilder.Entity<Document>().HasRequired(x => x.AnalysisFile);
modelBuilder.Entity<AnalysisFile>().HasOptional(x => x.Document);
我的问题是 EF 似乎没有为 AnalysisFile 和 Document 之间的关系创建外键,我在这里遗漏了什么还是这是预期的行为?
我还尝试在这两个类上创建 AnalysisFileId 和 DocumentId 属性,但是当我这样做时,它们不会填充任何 id。
还有一个问题:如何确保 Document 对 AnalysisFile 有级联删除?