Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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 - Fatal编程技术网

C# 实体框架代码首先将表链接到一起外键问题

C# 实体框架代码首先将表链接到一起外键问题,c#,entity-framework,C#,Entity Framework,3个表格(仅为简洁起见显示相关列),以便: [QuestionSet] <int> Id (Pk) ... [Link_Set_to_Question] <int> Id (pk) <int> fk_Question <-- Foreign key to Questions Table <int> fk_SetId <-- Foreign key to Questions Table <int> Display

3个表格(仅为简洁起见显示相关列),以便:

[QuestionSet]
<int>       Id (Pk)
...

[Link_Set_to_Question]
<int> Id (pk)
<int> fk_Question <-- Foreign key to Questions Table
<int> fk_SetId <-- Foreign key to Questions Table
<int> DisplayOrder
...

[Questions]
<int> Id (Pk)
...
[问题集]
Id(主键)
...
[将设置链接到问题]
Id(主键)
fk_问题
modelBuilder
.实体()
.HasMany(set=>set.QuestionSet)
.WithRequired(linkSet=>linkSet.Question)
.HasForeignKey(linkSet=>linkSet.fk\u QuestionId)
.WillCascadeOnDelete(真);

谢谢,所以我需要使用流体api?我在.WithRequired(linkSet->linkSet.Question)“无法隐式将问题转换为问题集”处遇到错误。我在链接类上添加了返回到问题集的导航属性,错误仍然存在,但我遇到了一个异常,无法找到问题\u Id(在任何模型或数据库中都不存在,因此我怀疑这是EF命名约定)如果我删除上面的modelbuilder代码,并在QuestionSet上使用Nav属性来获取链接集数据,然后在链接集上使用foreach来获取问题,那么所有这些都可以工作,所以我知道我的模型是可以的。如果我能解决这个问题的话,在一个数据库中点击101次(100个问题集)是没有效率的。在Link_set和quesitostate之间添加一个导航属性,然后更改WithForeignKey行使它工作起来。现在进入下一个问题:-)
public class QuestionSet
{
    [Key]
    public int Id { get; set; }
    ...

    [ForeignKey("fk_SetId")]
    public virtual ICollection<Link_Set_to_Question> QuestionSet { get; set; }

    public QuestionSetDetails()
    {
        this.QuestionSet = new List<Link_Set_to_Question>();                                  
    }
}

public class Link_Set_to_Question
{
    [Key]
    public int Id { get; set; }
    public int fk_SetId { get; set; }
    public int fk_QuestionId { get; set; }

    [ForeignKey("Id")]
    public virtual Questions Question { get; set; }

    public Relations_Questions_To_Sets()
    {
        this.Question = new Questions();
    }
}
 modelBuilder
            .Entity<QuestionSet>()
            .HasMany<Link_Set_to_Question>(set => set.QuestionSet)
            .WithRequired(linkSet => linkSet.Question)
            .HasForeignKey(linkSet => linkSet.fk_QuestionId)
            .WillCascadeOnDelete(true);