简体   繁体   English

可能检测到 Object 周期

[英]Possible Object Cycle Detected

I am trying to add a ProductGateway to a database and assign it a user.我正在尝试将ProductGateway添加到数据库并为其分配用户。 I am getting an A possible object cycle was detected .我得到一个A possible object cycle was detected error but I am unsure how I can overcome this.错误,但我不确定如何克服这个问题。

This is a 1:M relationship using Entity Framework Core.这是一个使用 Entity Framework Core 的 1:M 关系。 I am also in .NET 6.我也在.NET 6。

The idea is that a user can exist in the database without any products but a product must be assigned a user.这个想法是,用户可以在没有任何产品的情况下存在于数据库中,但产品必须分配给用户。

var user = _repository.GetAll<UserGateway>().FirstOrDefault(u => u.Id == userId);

        if (user == null) throw new InvalidUserException();

        var productGateways = productDtos.Select(productDto => new ProductGateway
            {
                DateCreated = DateTime.Now,
                DateLastModified = DateTime.Now,
                Description = productDto.Description,
                Quantity = productDto.Quantity,
                Title = productDto.Title,
                Gsku = productDto.Sku,
                ImageUrl = "",
                UserId = userId
            })
            .ToList();

        user.Products = productGateways;
        foreach (var product in user.Products)
        {
            product.User = user;
        }

        _repository.AddRange(productGateways);
        _repository.Update(user);
        _repository.Commit();

        return productGateways;

Here is the properties on the UserGateway model这是 UserGateway model 上的属性

public string Auth0Id { get; set; }
public List<ProductGateway> Products { get; set; }

And the ProductGateway model和产品网关 model

public string Title { get; set; }
public string Gsku { get; set; }
public string Description { get; set; }
public int Quantity { get; set; }
public string ImageUrl { get; set; }
public UserGateway User { get; set; }
public int UserId { get; set; }

I appreciate any help in advance!我提前感谢任何帮助! Cheers干杯

EDIT: Here is my context class编辑:这是我的背景 class

public class Context : DbContext
{
    private readonly IConfiguration _configuration;
    private DbSet<ProductGateway> Products { get; set; }
    private DbSet<UserGateway> Users { get; set; }

    public Context(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        var connectionString = _configuration.GetConnectionString("Context");
        optionsBuilder.UseSqlServer(connectionString);
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
        {
            relationship.DeleteBehavior = DeleteBehavior.Restrict;
        }
    }
}

You have userId in productGateways, so it already has a reference to the user.您在 productGateways 中有 userId,因此它已经有对用户的引用。 Then you put one more reference to User In productGateway.然后在 productGateway 中再放一个对 User 的引用。 After that, you put a list of productGateways to a user list.之后,您将 productGateways 列表放入用户列表。 And here the cycle User -> ProductGateway -> user -> productGateway -> infinite.这里循环用户 -> ProductGateway -> 用户 -> productGateway -> 无限。

Try to remove foreach and _repository.Update(user);尝试删除foreach_repository.Update(user); calls, maybe it will help.打电话,也许会有帮助。 Any way you should leave only one statement (add or update)无论如何你应该只留下一个声明(添加或更新)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检测到不支持的可能的对象循环 - A possible object cycle was detected which is not supported System.Text.Json.JsonException:检测到可能的 object 循环 - System.Text.Json.JsonException: A possible object cycle was detected 将实体模型序列化为 JSON 时检测到可能的对象循环 - A possible object cycle was detected when serializing entity-model to JSON .Net Core 3.0 可能的 object 周期被检测到不支持 - .Net Core 3.0 possible object cycle was detected which is not supported JsonException: 检测到不支持的可能的对象循环。 这可能是由于循环或对象深度大于 - JsonException: A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than 检测到可能的对象循环。 这可能是由于循环或对象深度大于最大允许深度 32 - A possible object cycle was detected. This can be due to a cycle or if the object depth is larger than the maximum allowed depth of 32 C# 循环引用。 System.Text.Json.JsonException:检测到可能的对象循环 .NET 5 - C# Circular reference. System.Text.Json.JsonException: A possible object cycle was detected .NET 5 如何解决 System.Text.Json.JsonException:在实体框架中检测到可能的 object 循环? - How to resolve System.Text.Json.JsonException: A possible object cycle was detected in Entity Framework? 反序列化 Json .NET CORE 5 - JsonException 时出错:检测到可能的 object 循环不支持 - Error when deserialize Json .NET CORE 5 - JsonException: A possible object cycle was detected which is not supported Azure CosmosDB + NetCore 3.1:System.Text.Json.JsonException:检测到可能的 object 循环不支持 - Azure CosmosDB + NetCore 3.1 : System.Text.Json.JsonException: A possible object cycle was detected which is not supported
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM