[英]N:N Entity Framework C# Relationship
I'm having a problem with many-to-many relationships in a pizzeria system I'm developing.我正在开发的比萨店系统中的多对多关系存在问题。
I have an entity called "Payament" that has a list of Pizzas and a list of Drinks.我有一个名为“Payament”的实体,其中包含比萨饼列表和饮料列表。
ex:前任:
public class Payament
{
public string? PayamentId { get; set; }
public virtual List<Pizza> Pizzas { get; set; }
public virtual List<Drink> Drinks { get; set; }
public string? CPFId { get; set; }
[JsonIgnore]
public virtual Client? Client { get; set; }
public double? TotalPay { get; set; }
public DateTime DateTransaction { get; set; }
public virtual StatusOrder StatusOrder { get; set; }
public Payament()
{
Pizzas = new List<Pizza>();
Drinks = new List<Drink>();
DateTransaction = DateTime.Now;
StatusOrder = StatusOrder.CARRINHO;
}
}
Also, I have two entities Pizza and Drink.另外,我有两个实体 Pizza 和 Drink。
ex:前任:
public class Pizza : IItem
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int Quantity { get; set; }
public double Value { get; set; }
[JsonIgnore]
public virtual List<Payament> Payament { get; set; }
public Pizza()
{
Payament = new List<Payament>();
}
}
public class Drink : IItem
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int Quantity { get; set; }
public double Value { get; set; }
[JsonIgnore]
public virtual List<Payament> Payament { get; set; }
public Drink()
{
Payament = new List<Payament>();
}
}
These classes are configured as many-to-many in OnModelCreating()... Drink:这些类在 OnModelCreating() 中配置为多对多...喝:
builder.HasMany(h => h.Payament)
.WithMany(d => d.Drinks);
Pizza:比萨:
builder.HasMany(h => h.Payament)
.WithMany(p => p.Pizzas);
Payament:付款:
builder.HasMany(p => p.Pizzas)
.WithMany(p => p.Payament);
builder.HasMany(d => d.Drinks)
.WithMany(d => d.Payament);
Migrations were generated, all right.产生了迁移,好吧。 But when I go to send a payment, I get this error:
但是当我去发送付款时,我收到了这个错误:
MySqlException: Cannot add or update a child row: a foreign key constraint fails ( pizzaroller
. drinkpayament
, CONSTRAINT FK_DrinkPayament_Payament_PayamentId
FOREIGN KEY ( PayamentId
) REFERENCES payament
( PayamentId
) ON DELETE CASCADE) MySqlException:无法添加或更新子行:外键约束失败(
pizzaroller
,CONSTRAINT drinkpayament
FOREIGN KEY( FK_DrinkPayament_Payament_PayamentId
)参考payament
PayamentId
PayamentId
ON DELETE CASCADE)
What I believe it could be: If I'm not mistaken, a drinkpayament table and a pizzapayament table are created where the primary keys between the relations are joined.我相信它可能是:如果我没记错的话,会创建一个drinkpayament 表和一个pizzapayament 表,其中连接了关系之间的主键。
And in this case, "Payament" has a primary key of type string, while items "Pizza" and "Drink" have a primary key of type int.在这种情况下,“Payament”有一个字符串类型的主键,而“Pizza”和“Drink”项目有一个 int 类型的主键。
The conflict is likely to be found in this divergence.在这种分歧中可能会发现冲突。 I could be wrong, of course.
当然,我可能是错的。 So I would like to know how I can solve this problem.
所以我想知道如何解决这个问题。 And also, if possible, tips on how I can work with existing items for sale and relate them to a payment.
如果可能的话,还有关于如何处理现有待售物品并将它们与付款相关联的提示。
Project is on github if you want to take a look: https://github.com/newhobbye/pizza-roller-api如果您想看一下,项目在 github 上: https ://github.com/newhobbye/pizza-roller-api
This project is for the exercise of knowledge and some patterns that I will implement to practice.这个项目是为了练习知识和一些我将实施到实践中的模式。 I accept any kind of criticism!
我接受任何形式的批评! A big hug and many thanks!
一个大大的拥抱,非常感谢!
I managed to solve.我设法解决了。
I was resending the client entity in update.我正在重新发送更新中的客户端实体。 And I hadn't turned off the AsNoTracking() option;
而且我没有关闭 AsNoTracking() 选项;
In addition to this problem, I was also resending the items instead of associating the existing ones by id.除了这个问题,我还重新发送了这些项目,而不是通过 id 关联现有的项目。
Thank you all for your help!谢谢大家的帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.