Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法创建级联外键_C#_Entity Framework_Ef Code First - Fatal编程技术网

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?我将更新我的卡片以向您展示我的意思