首先使用EF代码不正确地创建外键

时间:2018-02-25 16:36:14

标签: c# entity-framework entity-framework-core

我设置了几个表来创建多对多关系,但其中一个表没有正确设置外键。 据我所知,它们的设置与其他表格相同。

所以,首先我要展示工作台。 我有问题模型,它们都具有 QuestionGroups 的导航属性。模型看起来像这样:

public class Question : IEntity<int>
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; }
    public int CampaignId { get; set; }
    [Required, MaxLength(255)] public string Text { get; set; }
    public bool MultipleChoice { get; set; }
    [MaxLength(255)] public string Description { get; set; }

    public Campaign Campaign { get; set; }
    public IEnumerable<Answer> Answers { get; set; }
    public IEnumerable<QuestionGroup> Groups { get; set; }
}

集团看起来像这样:

public class Group : IEntity<int>
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; }
    public int CampaignId { get; set; }
    [Required, MaxLength(255)] public string Name { get; set; }
    [MaxLength(255)] public string Description { get; set; }

    public Campaign Campaign { get; set; }
    public IEnumerable<OutcomeGroup> Outcomes { get; set; }
    public IEnumerable<QuestionGroup> Questions { get; set; }
}

在我的OnModelCreating方法中,我建立了这种关系:

modelBuilder.Entity<QuestionGroup>().ToTable("QuestionGroups").HasKey(m => new { m.QuestionId, m.GroupId });

modelBuilder.Entity<Question>().HasMany(m => m.Groups).WithOne().HasForeignKey(m => m.QuestionId).OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Group>().HasMany(m => m.Questions).WithOne().HasForeignKey(m => m.GroupId).OnDelete(DeleteBehavior.Restrict);

如果我查看我的服务器资源管理器并编辑创建的两个外键,它们看起来像这样:

ALTER TABLE [dbo].[QuestionGroups]
ADD CONSTRAINT [FK_QuestionGroups_Groups_GroupId] FOREIGN KEY ([GroupId]) REFERENCES [dbo].[Groups] ([Id]);

ALTER TABLE [dbo].[QuestionGroups]
ADD CONSTRAINT [FK_QuestionGroups_Questions_QuestionId] FOREIGN KEY ([QuestionId]) REFERENCES [dbo].[Questions] ([Id]);

哪个是对的。 现在是奇怪的。 我还有另一张名为结果的表格,它与群组有很多关系。 结果看起来像这样:

public class Outcome : IEntity<int>
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; }
    [Required, MaxLength(255)] public string Name { get; set; }
    public string Description { get; set; }
    public string ImageUrl { get; set; }
    [MaxLength(2083)] public string Url { get; set; }
    public int Type { get; set; }

    public IEnumerable<OutcomeTag> Tags { get; set; }
    public IEnumerable<OutcomeGroup> Groups { get; set; }
}

我已经建立了这样的关系:

modelBuilder.Entity<OutcomeGroup>().ToTable("OutcomeGroups").HasKey(m => new { m.OutcomeId, m.GroupId });

modelBuilder.Entity<Group>().HasMany(m => m.Outcomes).WithOne().HasForeignKey(m => m.GroupId).OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Outcome>().HasMany(m => m.Groups).WithOne().HasForeignKey(m => m.OutcomeId).OnDelete(DeleteBehavior.Restrict);

然而,当我查看我的服务器资源管理器并检查创建的外键时,它们是这样的:

ALTER TABLE [dbo].[OutcomeGroups]
ADD CONSTRAINT [FK_OutcomeGroups_Groups_OutcomeId] FOREIGN KEY ([OutcomeId]) REFERENCES [dbo].[Groups] ([Id]);

ALTER TABLE [dbo].[OutcomeGroups]
ADD CONSTRAINT [FK_OutcomeGroups_Outcomes_OutcomeId] FOREIGN KEY ([OutcomeId]) REFERENCES [dbo].[Outcomes] ([Id]);

第二个键是正确的,但第一个键不是。 谁能发现原因?

1 个答案:

答案 0 :(得分:0)

我设法通过向Join表类添加导航属性来解决这个问题:

public class OutcomeGroup
{
    public int OutcomeId { get; set; }
    public int GroupId { get; set; }

    public Group Group { get; set; }
    public Outcome Outcome { get; set; }
}

然后我将modelBuilder更改为:

// Many to many
modelBuilder.Entity<AnswerTag>().HasOne(m => m.Answer).WithMany(m => m.Tags).HasForeignKey(m => m.AnswerId).OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<AnswerTag>().HasOne(m => m.Tag).WithMany(m => m.Answers).HasForeignKey(m => m.TagId).OnDelete(DeleteBehavior.Restrict);

modelBuilder.Entity<OutcomeTag>().HasOne(m => m.Outcome).WithMany(m => m.Tags).HasForeignKey(m => m.OutcomeId).OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<OutcomeTag>().HasOne(m => m.Tag).WithMany(m => m.Outcomes).HasForeignKey(m => m.TagId).OnDelete(DeleteBehavior.Restrict);

modelBuilder.Entity<OutcomeGroup>().HasOne(m => m.Outcome).WithMany(m => m.Groups).HasForeignKey(m => m.OutcomeId).OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<OutcomeGroup>().HasOne(m => m.Group).WithMany(m => m.Outcomes).HasForeignKey(m => m.GroupId).OnDelete(DeleteBehavior.Restrict);

modelBuilder.Entity<QuestionGroup>().HasOne(m => m.Question).WithMany(m => m.Groups).HasForeignKey(m => m.QuestionId).OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<QuestionGroup>().HasOne(m => m.Group).WithMany(m => m.Questions).HasForeignKey(m => m.GroupId).OnDelete(DeleteBehavior.Restrict);

这很有效。