简体   繁体   中英

EFCore : duplication of objects

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM