gpt4 book ai didi

entity-framework - Entity Framework Code first - FOREIGN KEY 约束问题

转载 作者:行者123 更新时间:2023-12-03 16:45:23 25 4
gpt4 key购买 nike

我是 EF 代码第一负责人的新手,目前不知道该怎么做..我有 2 个 POCO 类..

public class Problem
{
public int ProblemID { get; set; }
public int UserID { get; set; }
public int CategoryID { get; set; }
public int RatingID { get; set; }

public string Title { get; set; }
public string Description { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public int State { get; set; }
public DateTime CreatedOn { get; set; }

public virtual Rating Rating { get; set; }
public virtual Category Category { get; set; }
public virtual User User { get; set; }

public virtual ICollection<Picture> Pictures { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
}


public class User
{
public int UserID { get; set; }
public int RoleID { get; set; }

public string Name { get; set; }
public string Surname { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string SocialHandle { get; set; }
public DateTime RegisteredOn { get; set; }

public virtual Role Role { get; set; }

public virtual ICollection<Point> Points { get; set; }
public virtual ICollection<Problem> Problems { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
}

我的数据库创建没问题,但是当我尝试使用自定义初始值设定项在其中初始化一些数据时,出现以下错误:

Introducing FOREIGN KEY constraint 'Problem_User' on table 'Problems' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint. See previous errors.



这是我的初始化程序:
protected override void Seed(CleanStreets context)
{
var adminRole = new Role { Name = "Administrator", RoleID = 0 };
var moderatorRole = new Role { Name = "Moderator", RoleID = 1 };
var userRole = new Role { Name = "User", RoleID = 2 };

context.Roles.Add(adminRole);
context.Roles.Add(userRole);
context.Roles.Add(moderatorRole);

var admin = new User { Name = "admin", Surname = "admin", Role = adminRole, Email = "fake@fake.com", Password = "", RegisteredOn = DateTime.Now, SocialHandle = null };
var user = new User { Name = "user", Surname = "user", Role = userRole, Email = "fake@fake2.com", Password = "", RegisteredOn = DateTime.Now, SocialHandle = null };

context.Users.Add(admin);
context.Users.Add(user);

var categoryOne = new Category { Title = "Komunalni problemi", CategoryID = 0 };
var categoryTwo = new Category { Title = "Ostali problemi", CategoryID = 1 };

context.Categories.Add(categoryOne);
context.Categories.Add(categoryTwo);

var problem = new Problem { Category = categoryOne, Title = "Prvi testni problem", Description = "Ovo je testni opis", Latitude = 45.5, Longitude = 15.5, State = 0, CreatedOn = DateTime.Now, User = user };

context.Problems.Add(problem);

base.Seed(context);
}

我做错了什么?先感谢您!

最佳答案

那将是因为 Comments .默认情况下,EF 对引用使用级联删除。在您的情况下,级联删除将从用户 -> 问题、用户 -> 评论以及问题 -> 评论创建。如果删除用户级联到同一评论记录可以来自 ProblemUser .这在 SQL Server 中是不允许的。每个记录只能通过单个级联删除路径访问。

为了避免这种情况,您必须使用流畅的映射来关闭一个关系的级联删除(您必须选择哪一个)。 User -> Comment 的示例

public class Context : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Comment> Comments { get; set; }
public DbSet<Problem> Problems { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.HasMany(u => u.Comments)
.HasRequired(c => c.User)
.HasForeignKey(c => c.UserId)
.WillCascadeOnDelete(false);

base.OnModelCreating(modelBuilder);
}
}

在您的模型中可能还有其他实体和关系会导致此问题,但 Comment从您的示例中看起来很明显。

关于entity-framework - Entity Framework Code first - FOREIGN KEY 约束问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7363762/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com