[英]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.