首页 > 解决方案 > 无法使用 SET NULL 引用操作创建外键“FK_ApplicationInfos_UserProfiles_UserProfileId”

问题描述

如何解决问题? 错误消息 在 Entity Framework Core 中,我正在尝试创建一个具有 4 个 Db 模型的系统 - 用户、用户配置文件、评论、应用程序信息。我尝试了很多东西,好吧,我不明白如何解决这种情况的概念,我在modelOnCreate中明确给出了级联

模型用户

 public class User
    {
        public int Id { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string Token { get; set; }
        public UserProfile UserProfile { get; set; }
    }

模型用户资料

 public class UserProfile 
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Phone { get; set; }

        public int UserId { get; set; }
        public User User { get; set; }

        public IEnumerable<ApplicationInfo> ApplicationInfos { get; set; }
        public IEnumerable<Review> Reviews { get; set; }

    }

模型审查

public class Review
    {
        public int Id { get; set; }
        public string Text { get; set; }
        public string ImageLocation { get; set; }
        public string Version { get; set; }
        public string Rate { get; set; }
        //One(AppInfo) to Many(Review)
        public int ApplicationInfoId { get; set; }
        public ApplicationInfo ApplicationInfo { get; set; }
        //One(UserProfile) to Many(Review)

        public int UserProfileId { get; set; }
        [Required]
        public UserProfile UserProfile { get; set; }


    } 

模型应用信息

public class ApplicationInfo
    {
        [Key]
        public int Id { get; set; }
        [Required]
        public string ApplicationId { get; set; }
        [Required]
        public string AppName { get; set; }
        [Required]
        public string PublisherEmail { get; set; }
        public string Genre { get; set; }
        public string Price { get; set; }
        public string Description { get; set; }
        public string Version { get; set; }
        public string IconUrl { get; set; }
        public string PublisherName { get; set; }
        public string AllRatingCount { get; set; }
        public string AllRating { get; set; }

        //One(UserProfile) To Many(AppInfo)
        public int UserProfileId { get; set; }
        public UserProfile UserProfile { get; set; }

        public IEnumerable<Review> Reviews { get; set; }

    }

OnModelCreating

     protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<User>().
                    HasOne<UserProfile>(s=>s.UserProfile).
                    WithOne(s=>s.User).
                    HasForeignKey<UserProfile>(s=>s.UserId)
                    .OnDelete(DeleteBehavior.Cascade);
                modelBuilder.Entity<ApplicationInfo>()
                    .HasOne<UserProfile>(s => s.UserProfile)
                    .WithMany(s => s.ApplicationInfos)
                    .OnDelete(DeleteBehavior.Cascade);
                modelBuilder.Entity<Review>()
                    .HasOne<UserProfile>(s => s.UserProfile)
                    .WithMany(s => s.Reviews)
                    .OnDelete(DeleteBehavior.Cascade);
                modelBuilder.Entity<Review>()
                    .HasOne<ApplicationInfo>(s => s.ApplicationInfo)
                    .WithMany(s => s.Reviews)
                    .OnDelete(DeleteBehavior.Cascade);


            }

标签: c#asp.netentity-framework-core

解决方案


只需更改 OnModelCreating 并将 OnDelete 更改为 Review 并将 AppInfo 更改为 Restrict(无操作)

 protected override void OnModelCreating(ModelBuilder modelBuilder)

        {

            modelBuilder.Entity<User>()
                .HasOne<UserProfile>(s => s.Profile)
                .WithOne(s => s.User)
                .HasForeignKey<UserProfile>(s=>s.UserId)
                .OnDelete(DeleteBehavior.Cascade);
            modelBuilder.Entity<ApplicationInfo>()
                .HasOne<UserProfile>(s => s.UserProfile)
                .WithMany(s => s.ApplicationInfos)
                .HasForeignKey(s => s.UserProfileId)
                .OnDelete(DeleteBehavior.Cascade);
            modelBuilder.Entity<Review>()
                .HasOne<UserProfile>(s => s.UserProfile)
                .WithMany(s => s.Reviews)
                .HasForeignKey(s => s.UserProfileId)
                .OnDelete(DeleteBehavior.Restrict);
            modelBuilder.Entity<Review>()
                .HasOne<ApplicationInfo>(s => s.ApplicationInfo)
                .WithMany(s => s.Reviews)
                .HasForeignKey(s => s.ApplicationInfoId)
                .OnDelete(DeleteBehavior.Restrict);

            base.OnModelCreating(modelBuilder);


        }

推荐阅读