C# 无法创建级联外键
我有一个文件表:C# 无法创建级联外键,c#,entity-framework,ef-code-first,C#,Entity Framework,Ef Code First,我有一个文件表: public class Document { [Key] public int DocumentId {get; set;} public string Title {get; set;} } public class DepthDocument { [Key] public int DepthDocumentId {get; set;} public int DocumentId {get; set;} public int Depth {get; set;} } 我还
public class Document
{
[Key]
public int DocumentId {get; set;}
public string Title {get; set;}
}
public class DepthDocument
{
[Key]
public int DepthDocumentId {get; set;}
public int DocumentId {get; set;}
public int Depth {get; set;}
}
我还有一个深度文件表:
public class Document
{
[Key]
public int DocumentId {get; set;}
public string Title {get; set;}
}
public class DepthDocument
{
[Key]
public int DepthDocumentId {get; set;}
public int DocumentId {get; set;}
public int Depth {get; set;}
}
每个文档都有相应的DepthDocument。两个表的行数相同。我试图告诉EF,当我删除一个文档时,我也想删除相应的DepthDocument。我认为这其中的一部分是创建1-1关系,我已经尝试将其添加到Document类中:
[Required]
public virtual DepthDocument DepthDocument { get; set; }
然后这个:
modelBuilder.Entity<Document>()
.HasRequired(c => c.DepthDocument).WithRequiredPrincipal()
.WillCascadeOnDelete(true);
将
DepthDocument
更改为如下所示:
public class DepthDocument
{
[Key]
public int DocumentId {get; set;}
public int Depth {get; set;}
}
由于它是一个1:1的关系,
DepthDocument
如果没有匹配的Document
就不可能存在,因此DepthDocument
没有任何理由使用不同的键。我给了它自己的Id,因为我刚刚添加了DepthDocument表,所以为了给数据种子和指定每个条目的Id(为了匹配每一个现有文档),我添加了该列。是否可以在新表的种子数据中指定DocumentId?我将更新我的卡片以向您展示我的意思