© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
1 reply
bribri

Ef insert many to many

How do I insert an new object to my database?
right now objects are like this:
class order{ 
int id
List<product>
}
class order{ 
int id
List<product>
}

class orderproduct{
int orderid
int productId
}
class orderproduct{
int orderid
int productId
}


class Product{ 
int id
List<order>}
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
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Querying Many to Many EF Relationships
C#CC# / help
3y ago
❔ EF Core duplicating many-to-many?
C#CC# / help
4y ago
❔ EF Core many-to-many migration problems
C#CC# / help
3y ago
EF Core many-to-many relation error
C#CC# / help
4y ago