Reputation: 124
I have objects like the following :
Class Job
{
int JobId; // PK
Icollection<Pat> Pats;
}
Class Pat
{
int PatId; // PK
int JobId; // FK
string RelativePath;
}
Once added a Job with a collection of Pat, it is no more possible to add a new Job with the same collection of Pat. I would like to duplicate Pats for each Job.
Here is what i have done :
using (var db = new TransferContext())
{
for (int i = 1; i<=2; i++)
{
var tj = new Job()
{
Pats = listto;
};
await db.TJobs.AddAsync(tj);
}
await db.SaveChangesAsync();
}
listto is the collection that i want to duplicate for each Job.
Thanks for your help.
K
Upvotes: 0
Views: 61
Reputation: 2098
It sounds like you are looking for a Many to Many relationship, where you can have a separate set of both Jobs and Pats that can be linked.
You need a class JobPat
with an Id for each, and then Job
and Pat
get a collection of the JobPat
instead of a direct link.
Configuring Many to Many Relationships with EF Core
Upvotes: 3