Skip to content

Composite key issue #17

@bsgiovanini

Description

@bsgiovanini

Hello!

First of all, it's a great package. very useful

But I'm having an issue when I need to clone a entity with composite key. By default, the package reset the primary key for its default value.
However, when i have a case like that:

ClassA {
    public virtual int AId  {get; set;}  //generated by sequence  
    public virtual IList<ClassB> Blist {get; set;} 
}

ClassB {
   public virtual int AId  // ClassA key
   public virtual int BId  // int generated manually ordened from a counter
   // other properties
}

where AId and BId are the Composite key of ClassB

When I do

var classA = await Context.Set<ClassA>().AsNoTracking().Where(x => x.AId == param).Include(x => x.Blist).FirstOrDefaultAsync();
  
var copy = await Context.CloneAsync(classA);

both AId and BId are reset in ClassB and if Blist has two or more elements, I get an error by including more than one entity with the same Id when trying to add copy to context. Since all elements in Blist have AId and BId set to 0

await Context.Set<ClassA>().AddAsync(copy)

In this case,

Is there any way to deal with this? Maybe an attribute in EFcore configuration saying it should not be reset?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions