Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

c# - Insert statement conflicted with foreign key EF Core many to many

enter image description here

enter image description here

Service:

 public Cart AddProductToCart(Product product, Cart cart)
        {
            CartProduct cartProduct = new CartProduct();
            cartProduct.CartId = cart.Id;
            cartProduct.ProductId = product.Id;

            _dbContexet.CartProduct.Add(cartProduct);
            _dbContexet.SaveChanges();
            return cart;
        }

I have a cart that can have many product and product that can have many carts, 1 cart belongs to 1 user... When i try to insert this product and cart i get this error:

SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_CartProduct_Carts_ProductId". The conflict occurred in database "ManagerZ", table "dbo.Carts", column 'Id'.
The statement has been terminated

.

question from:https://stackoverflow.com/questions/65871502/insert-statement-conflicted-with-foreign-key-ef-core-many-to-many

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...