C# EF代码优先:多重性约束冲突

C# EF代码优先:多重性约束冲突,c#,entity-framework,code-first,fluent-interface,C#,Entity Framework,Code First,Fluent Interface,救命我在导航模型上收到以下错误ArticleType: 发生关系多重性约束冲突 以下是现有的数据库架构: 这是我的密码: public class Article { public int ID { get; set; } public virtual Stage Stage { get; set; } public virtual ArticleType ArticleType { get; set; } // Causes the violation } publ

救命我在导航模型上收到以下错误ArticleType

发生关系多重性约束冲突

以下是现有的数据库架构:

这是我的密码:

public class Article
{
    public int ID { get; set; }
    public virtual Stage Stage { get; set; }
    public virtual ArticleType ArticleType { get; set; } // Causes the violation
}

public class ArticleType
{
    public int ID { get; set; }
    public string Title { get; set; }
}

public class Stage
{
    public int ID { get; set; }
    public string Title { get; set; }
}
我使用fluentapi进行映射,这里是该协会的摘录

// This works
modelBuilder.Entity<Article>
    .HasRequired(t => t.Stage)
    .WithMany() // if turned .WithOptional() then will also cause the error.
    .Map(m => m.MapKey("stage_id"));

// This does not work
modelBuilder.Entity<Article>
    .HasRequired(t => t.ArticleType)
    .WithMany()
    .Map(m => m.MapKey("article_type_id"));

您可以从visualstdio获取.edmx文件,并将数据库映射到该文件,以便根据数据库模式自动创建实体的关系映射

在谷歌搜索特定错误消息a时,导致此错误的原因是使用以下命令将一对多关系配置为一对一关系:

modelBuilder.Entity<Article>
.HasRequired(t => t.ArticleType)
.WithOptional()
.HasForeignKey(...);
modelBuilder.Entity
.HasRequired(t=>t.ArticleType)
.WithOptional()
.HasForeignKey(…);
虽然它应该是:

modelBuilder.Entity<Article>
.HasRequired(t => t.ArticleType)
.WithMany()
.HasForeignKey(...);
modelBuilder.Entity
.HasRequired(t=>t.ArticleType)
.有很多
.HasForeignKey(…);

您已经在自己的代码示例中指出了这一点。由于您为
ArticleType
Stage
显示的代码是相同的,因此没有理由认为此代码是问题的原因。在代码的其他地方,必须在
文章
文章类型
之间定义一对一关系。或者,在某种程度上,您最初的关系是错误的,但实体框架没有找到正确的fluent定义。

这根本不能回答问题,因为他首先使用代码,我建议您明确地将外键添加到代码中。因此,将'ArticleTypeID'和'StageID'属性添加到'Article'类中。这就是说,EntityFramework应该选择这种关系,而不需要使用Fluent API或其他方式进行配置。@Dabblernl谢谢。但我试图避免那个解决方案,所以我发布了这个问题,但我将尝试那个作为我的最终解决方案。你们为什么要试图避免那个?如果你这样做的话,你的生活会变得更加轻松…你能从调试器中发布完整的错误吗?有多个错误输出可以归入这个错误类别。@Dabblernl-最初我打算用一种更面向对象的方法来处理我的关联。但在对“外键协会与独立协会”进行进一步研究后,看来我最终还是要放弃这一努力了。更详细的解释:我决定显式声明外键属性,错误仍然存在,因此问题确实发生在某个地方。见我编辑的帖子。谢谢在添加“外键属性”之后,我添加了
.HasForeignKey(…)
。突然,“文章类型”无法映射。查看我编辑的帖子。好的,我刚刚发现在其他地方,
ArticleType
Recipient
之间存在一对一的关联(其他一些类甚至没有被调用,但导致了错误)
this.Property(t => t.StageID).HasColumnName("article_type_id"); // <-- Switched
this.Property(t => t.ArticleTypeID).HasColumnName("stage_id");  // <-- Switched
class ArticleMap : EntityTypeConfiguration<Article>
{
    public ArticleMap()
    {
        // Primary Key
        this.HasKey(t => t.ID);
        this.Property(t => t.ID)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        // Foreign Key Properties
        this.Property(t => t.StageID)
            .IsRequired();
        this.Property(t => t.JournalID)
            .IsRequired();
        this.Property(t => t.ArticleTypeID)
            .IsRequired();

        // Navigational Models
        this.HasRequired(t => t.Stage); // This works
        this.HasRequired(t => t.ArticleType)
            .WithMany()
            .HasForeignKey(t => t.ArticleTypeID); // Newly added

        // Table & Column Mappings
        this.ToTable("items");
        this.Property(t => t.ID).HasColumnName("item_id");
        this.Property(t => t.ArticleTypeID).HasColumnName("article_type_id");
        this.Property(t => t.StageID).HasColumnName("stage_id");
    }
}

class ArticleTypeMap : EntityTypeConfiguration<ArticleType>
{
    public ArticleTypeMap()
    {
        // Primary Key
        this.HasKey(t => t.ID);

        // Properties
        this.Property(t => t.ID)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        this.Property(t => t.Title)
            .HasMaxLength(100)
            .IsRequired();

        // Table & Column Mappings
        this.ToTable("article_types");
        this.Property(t => t.ID).HasColumnName("article_type_id"); // <-- Apparently, this is no longer mapped.
        this.Property(t => t.Title).HasColumnName("title");
    }
}
modelBuilder.Entity<Article>
.HasRequired(t => t.ArticleType)
.WithOptional()
.HasForeignKey(...);
modelBuilder.Entity<Article>
.HasRequired(t => t.ArticleType)
.WithMany()
.HasForeignKey(...);