Base solution for your next web application
Open Closed

Suppressing Id generation in EF code first. #492


User avatar
0
lauriep created

Hi,

Is there anything in the framework that might get in the way of overriding the default name and type for an entities PK please?

Marking a property like this results in the right primary key being defined:

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid SupplierId { get; set; }

However, the EF default of "Id" is still created. Naming a property "<EntityClassName>Id" should suppress this:

[attachment=0:ji9x8gu7]Capture.JPG[/attachment:ji9x8gu7] Thanks for any guidance.

Cheers,

Laurie


2 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    You can derive your entity from Entity<TKey> and override Id as shown below:

    public class Supplier : Entity<Guid>
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public override Guid Id { get; set; }
    
        //...your other properties
    }
    

    Entity's primary key must be Id in ABP framework. If you want to map it to a different field in db, you can add a [Column("SupplierId")] to the Id property. But the entity model's id should be Id.

  • User Avatar
    0
    lauriep created

    Thanks hikalkan.

    For those who are interested, to override Id with guid datatype and also implement the full audit log pattern, do this:

    [Table("Supplier")]
    public class Supplier : **FullAuditedEntity&lt;Guid&gt;**
    
    {
    
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public override Guid Id { get; set; }