INSERT 语句与 FOREIGN KEY 约束“FK_Users_Agencies_UserID”冲突

问题描述

我使用实体框架核心 5.0,并使用 fluent api 创建了我的一对多关系。 当我尝试在我的项目中创建一个新用户时,我收到了那个错误。 让我向你展示我的 User 班级:

    public class User : Base
    {
        [Key]
        public int UserID { get; set; }
        public string UserName { get; set; }
        public string UserSurname { get; set; }
        public string UserPassword { get; set; }
        public string UserEMail { get; set; }
        public int? AgencyID { get; set; }
   
        public virtual Agency Agency { get; set; }
    }
    public class UserConfiguration : IEntityTypeConfiguration<User>
    {
        public void Configure(EntityTypeBuilder<User> builder)
        {
            builder.HasKey(user => user.UserID);

        }
    }

这里是一个Agency 类相关的 User 类:

    public class Agency : Base
    {
        [Key]
        public int AgencyID { get; set; }
        public string AgencyName { get; set; }
        public string AgencyPhoto { get; set; }
        public string AgencyEMail { get; set; }
        public string AgencyPhone { get; set; }
        public int AgencyExportArea { get; set; } 
        public virtual ICollection<User> Users { get; set; }
    }
    public class AgencyConfiguration : IEntityTypeConfiguration<Agency>
    {
        public void Configure(EntityTypeBuilder<Agency> builder)
        {
            builder.HasKey(agency => agency.AgencyID);
            builder.HasMany(us => us.Users)
                .WithOne(us => us.Agency)
                .HasForeignKey(au => au.UserID)
                .Isrequired(false)
        }
    }

我知道,我收到错误 sqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Users_Agencies_UserID". The conflict occurred in database "anan",table "dbo.Agencies",column 'AgencyID'.,因为 Agency 表中没有数据。我试图做的事情是使 AgencyID 外键可选为可空值。在 User 类中,您可以看到我将 AgencyID 定义为可空值。

我真的需要将这种关系定义为一对一或零,还是有其他方法可以做到这一点?

如果我必须将这种关系定义为一对一或零,你能告诉我我该怎么做吗。

解决方法

由于您使用的是 EF core 5,因此您不需要:

public class UserConfiguration : IEntityTypeConfiguration<User>
and 
public class AgencyConfiguration : IEntityTypeConfiguration<Agency>

所有这些代码都是冗余的。默认情况下,您有一个标准的一对多关系,EF 核心会识别和配置该关系。删除所有这些代码,一切都会好起来的。

但如果你是学生并且需要努力做每件事,你可以添加这个冗余代码:

protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>(entity =>
            {
                entity.HasOne(d => d.Agency)
                   .WithMany(p => p.Users)
                   .HasForeignKey(d => d.AgencyId)
                   .OnDelete(DeleteBehavior.ClientSetNull);
         });

        }

并且由于您对配置感兴趣,因此这些是另一个冗余属性:

public class User : Base
    {
        [Key]
        public int UserID { get; set; }
        .....

        public int? AgencyID { get; set; }
        [ForeignKey(nameof(AgencyId))]
        [InverseProperty("Users")]
        public virtual Agency Agency { get; set; }
    }

    public class Agency : Base
    {
        [Key]
        public int AgencyID { get; set; }
         .....
        [InverseProperty(nameof(User.Agency))]
        public virtual ICollection<User> Users { get; set; }
    }