实体框架代码优先 - 外键约束问题

发布于 2024-12-03 22:35:32 字数 3340 浏览 1 评论 0原文

我是 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; }               
}

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

在表上引入外键约束“Problem_User” “问题”可能会导致循环或多个级联路径。指定开启 DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 限制条件。
无法创建约束。查看以前的错误。

这是我的初始化程序:

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 = "[email protected]", Password = "", RegisteredOn = DateTime.Now, SocialHandle = null };
        var user = new User { Name = "user", Surname = "user", Role = userRole, Email = "[email protected]", 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);
    }

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

I'm new to EF code first principal and currently with no clue what to do.. I have 2 POCO classes..

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; }
}

and

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; }               
}

My database creation is ok, but when I try to init some data in it using custom initializer, I'm getting following error :

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.

This is my initializer :

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 = "[email protected]", Password = "", RegisteredOn = DateTime.Now, SocialHandle = null };
        var user = new User { Name = "user", Surname = "user", Role = userRole, Email = "[email protected]", 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);
    }

What I'm doing wrong? Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

厌倦 2024-12-10 22:35:32

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

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

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 看起来很明显。

That will be because of Comments. EF by default uses cascade deletes on references. In your case the cascade delete will be created from User -> Problem, User -> Comment but also from Problem -> Comment. If you deleted User cascading to the same comment record can come from both Problem and User. That is not allowed in SQL Server. Each record can be accessible by only single cascade delete path.

To avoid this you must use fluent mapping to turn of cascade delete on one relation (you must make choice which one). The example for 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);
    }
} 

There can be other entities and relations which can cause this problem in your model but Comment looks obvious from your example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文