C
C#7mo ago
bribri

Ef insert many to many

How do I insert an new object to my database? right now objects are like this:
c#
class order{
int id
List<product>
}
c#
class order{
int id
List<product>
}
c#
class orderproduct{
int orderid
int productId
}
c#
class orderproduct{
int orderid
int productId
}
c#
class Product{
int id
List<order>}
c#
class Product{
int id
List<order>}
so I want to add an order object with an list of products(that contain the id) but I get erros. when I just use 1 of the same product: Cannot insert explicit value for identity column in table 'Products' when IDENTITY_INSERT is set to OFF. when my list contains the same product twice: 'The instance of entity type 'Product' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. Not really sure how to fix it.... Can also use dapper if that's easier
1 Reply
lycian
lycian7mo ago
What's the code you're using? IDENTITY_INSERT usually means you're trying to generate an ID to for a new item locally instead of letting the database do it