【问题标题】:Cannot create the foreign key "FK_ApplicationInfos_UserProfiles_UserProfileId" with the SET NULL referential action无法使用 SET NULL 引用操作创建外键“FK_ApplicationInfos_UserProfiles_UserProfileId”
【发布时间】:2019-12-24 16:16:55
【问题描述】:

如何解决问题? Error Message 在 Entity Framework Core 中,我正在尝试创建一个具有 4 个 Db 模型的系统 - 用户、用户配置文件、评论、应用程序信息。 试了很多东西,嗯,没看懂怎么解决的概念,在modelOnCreate里明明给了cascade

模型用户

 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.net entity-framework-core


【解决方案1】:

只需更改 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);


        }

【讨论】:

    猜你喜欢
    • 2019-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多