gpt4 book ai didi

c# - 无法确定相关操作的有效顺序?

转载 作者:行者123 更新时间:2023-11-30 22:10:35 25 4
gpt4 key购买 nike

我有这个模型:

public class ContentType
{
public int ContentTypeId{get;set;}
public string Name{get;set;}
public Lang Lang{get;set;}
public bool IsPublished{get;set;}
public int? ParentId { get; set; }
public int UserId { get; set; }
public virtual User User { get; set; }
public virtual ContentType Parent { get; set; }
public virtual List<ContentType> Children { get; set; }
}

它与自身具有一对多的关系。

在上下文中我有这个:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ContentType>().HasMany(c => c.Children).WithOptional(c => c.Parent).HasForeignKey(c => c.ParentId);
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}

但是当我使用 ParentId=0 保存记录时,我看到了这个错误:

Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.

请注意,由于以下错误,数据库中的关系不存在:

'ContentType' table - Unable to create relationship 'FK_ContentType_ContentType'. The ALTER TABLE statement conflicted with the FOREIGN KEY SAME TABLE constraint "FK_ContentType_ContentType". The conflict occurred in database "CMS", table "dbo.ContentType", column 'ContentTypeId'.

但我不认为问题出在此处。我不知道。

这里有什么问题吗?

最佳答案

看来这两个错误都是由于表ContentType中的数据不正确导致的。

'ContentType' table - Unable to create relationship 'FK_ContentType_ContentType'. The ALTER TABLE statement conflicted with the FOREIGN KEY SAME TABLE constraint "FK_ContentType_ContentType". The conflict occurred in database "CMS", table "dbo.ContentType", column 'ContentTypeId'.

这指向 ParentId 字段中的错误值。验证此字段中的值在其他记录中是否确实是正确的 ContentTypeId

Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.

此错误指向循环依赖。验证您的数据中没有循环。

关于c# - 无法确定相关操作的有效顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20760311/

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