HenkKin / EntityCloner.Microsoft.EntityFrameworkCore

Cloning entities using Microsoft.EntityFrameworkCore
21 stars 3 forks source link

ForeignKey Entity #5

Closed Toso82 closed 1 year ago

Toso82 commented 1 year ago

I don't find if it is possible to clone entities with ForeignKey without reset key I mean I use: wait dbContext.CloneAsync and I need to include the entity to load but I don't want to clone this entity but just keep this relationship

Toso82 commented 1 year ago

@HenkKin Any news about my request to mantain relation (not reset id) of include entity ?

HenkKin commented 1 year ago

The purpose of the entity cloner is to clone entities. It is not intended to retrieve entities.

You can clone the entity and then use SaveChanges and then retrieve it from the database.

Toso82 commented 1 year ago

@HenkKin Hi maybe not explain well. I mean undestand the clone and work well for this regenerate key and foreingn key and this ok but i have this problem now. I have a relection i want not lost so if not include not load entity and if i load change key. Any suggestion for this use your library?


public class A{
     public  Guid Id { get; set; }
     public  List<B> ListaB { get; set; }
}

public class B {
     public  Guid Id { get; set; }
     public string Name { get; set; }
     public C {get; set;}
}

public class C {
  public  Guid Id { get; set; }   
  public  string Name { get; set; }
}

I have something similar and I want mantein C relation and clone other

Toso82 commented 1 year ago

@HenkKin Not response so ok

HenkKin commented 1 year ago

@HenkKin Hi maybe not explain well. I mean undestand the clone and work well for this regenerate key and foreingn key and this ok but i have this problem now. I have a relection i want not lost so if not include not load entity and if i load change key. Any suggestion for this use your library?

public class A{
     public  Guid Id { get; set; }
     public  List<B> ListaB { get; set; }
}

public class B {
     public  Guid Id { get; set; }
     public string Name { get; set; }
     public C {get; set;}
}

public class C {
  public  Guid Id { get; set; }   
  public  string Name { get; set; }
}

I have something similar and I want mantein C relation and clone other

If you clone A => B then relation between B and C will remain same (id).

So only relationships which are included are reset and cloned. For not included relationships the id is remaining the same, The relationship is not loaded from database. In this example on entity B there is not CId property. If the example is not working, maybe adding 'public Guid CId {get; set;}' will help you.

HenkKin commented 1 year ago

@HenkKin Not response so ok

I did not see your response, sorry