EntityFrameworkCore.Generator icon indicating copy to clipboard operation
EntityFrameworkCore.Generator copied to clipboard

[Feature] Add an option to generate a Guid primary key value on the entity class

Open ArnaudB88 opened this issue 11 months ago • 0 comments

In our company, we use your generator for many years now with many developers. Over the years, we added some features so it works better with our solutions. As a sign of appreciation, I want to suggest some features because I think it will help some other developers as well.

Feature description:

Add an option to generate a new Guid value for the primary key property in the generated entity classes.

Feature motivation & example usage:

This allows using the primary key property before changes are saved on the dbContext. We use the CQRS pattern with MediatR and database changes are saved automatically in the 'code pipeline'. When we have functionality which add for example a 'Project' entity, we also return the Id of the created entity to the requester (in our case we return it as a web api result). We contruct the response before database changes are done, so we already need the Id. This feature allows us to use the Id before the 'database save changes'.

Another example: when adding entities, this feature allows using the Id of the 'entities to be added' for other method calls. Sometimes we are not allowed to pass entities to other layers of the application (by architectural guidelines, seperation of concern). But the primary key value (Guid), is allowed to be passed as a value. This feature allows using the already generated value.

The option is a boolean, true enables the feature.

YAML usage:

data:
  entity:
    generatePkValue: true

Code generation result:

using System;
using System.Collections.Generic;

namespace Company.Project.Domain.Entities;

public partial class Project
{
    public Project()
    {
        #region Generated Constructor
        Id = Guid.NewGuid();
        #endregion
    }

    #region Generated Properties
    public Guid Id { get; set; }
    #endregion
}

Please review the feature request and evaluate if you think this is an addition to your repo. (pull request will be added)

Thanks

ArnaudB88 avatar Mar 22 '24 10:03 ArnaudB88