1

The following resource displays how to generate Partial Class mapping for Generic repository. Our Models don't have Id column, they are always named ProductId, CustomerId, etc. (Know generic repository is under lot of debate, however manager told us to apply it)

The method results in client-side evaluation, which is slow/not allowed in EFCore 3.

Net Core: Create Generic Repository Interface Id Mapping for All Tables Auto Code Generation

public partial class Product: IEntity
{
    [NotMapped]
    public int Id { get => ProductId; set => ProductId= value; }
}

Does anyone have auto generated code or T4 to create this result? Would like to go through all scaffolded models and rename class member to id, and Column to CustomerId, SalesId, ProductId, etc? Code also needs to update ModelBuilder as need. Does EF Core 2 have option like this?

How to map table names and column name different from model in onmodelcreating method in Entity Framework -6?

It can be done with Data Annotation or FluentAPI, open to any option.

public class Product
{
    [Column("ProductId")]
    public int Id { get; set; }

    public string ProductName { get; set; }
    public string ProductDescription { get; set; }
    public float Cost { get; set; } 
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Product>()
        .Property(p => p.Id)
        .HasColumnName("ProductId");
}
  • has happens if you have a partial class for each entity and then have a virtual `public virtual int Id {get; set;}` on each in the T4 template, then in the partial override like above. if this works ill write as an answer. – Seabizkit Aug 13 '19 at 07:41
  • hi @Seabizkit not sure I understand comment , first method is not recommended, public partial class Product: IEntity { [NotMapped] public int Id { get => ProductId; set => ProductId= value; } }, second method is, thanks –  Aug 13 '19 at 10:43

0 Answers0