HenkKin / EntityCloner.Microsoft.EntityFrameworkCore

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

Add `options` Parameter for Configurable Primary Key and ValueGenerated Behavior #10

Open william-des opened 11 months ago

william-des commented 11 months ago

Hello 👋,

I've been using this package in my project and have come across a specific need. Currently, the package automatically removes all primary keys when cloning an entity. While this is the desired behavior in most cases, in some scenarios, it would be useful to keep certain primary keys intact.

Proposed Feature:

I propose adding an optional options parameter that would allow the users to configure:

  1. Whether to remove primary keys or not (Boolean).
  2. Whether to remove properties based on their value generation strategy using the ValueGenerated enum.
public class CloneOptions
{
    public bool RemovePrimaryKeys { get; set; } = true;
    public ValueGenerated? RemoveBasedOnValueGenerated { get; set; }
}

Example Usage:

var options = new CloneOptions
{
    RemovePrimaryKeys = false,
    RemoveBasedOnValueGenerated = ValueGenerated.OnAdd
};

var clonedEntity = await context.CloneAsync(options, 123);

Why this feature would be useful:

This feature would make the package more flexible for a wider range of use-cases. Sometimes, not all primary keys should be treated equally, and having this granular control can be extremely beneficial.

If you find this feature useful, I'd be happy to submit a PR to implement it.