C# 如何将通用存储库中的所有实体框架主列重命名为Id?

C# 如何将通用存储库中的所有实体框架主列重命名为Id?,c#,entity-framework,asp.net-core,.net-core,t4,C#,Entity Framework,Asp.net Core,.net Core,T4,以下资源显示如何为通用存储库生成部分类映射。我们的模型没有Id列,它们总是被命名为ProductId、CustomerId等(知道通用存储库有很多争议,但经理告诉我们应用它) 该方法会导致客户端评估,这在EFCore 3中是缓慢的/不允许的 是否有人拥有自动生成的代码或T4来创建此结果?是否希望遍历所有脚手架模型并将类成员重命名为id,将列重命名为CustomerId、SalesId、ProductId等?代码还需要根据需要更新ModelBuilder。EF Core 2有这样的选项吗 它可

以下资源显示如何为通用存储库生成部分类映射。我们的模型没有Id列,它们总是被命名为ProductId、CustomerId等(知道通用存储库有很多争议,但经理告诉我们应用它)

该方法会导致客户端评估,这在EFCore 3中是缓慢的/不允许的

是否有人拥有自动生成的代码或T4来创建此结果?是否希望遍历所有脚手架模型并将类成员重命名为id,将列重命名为CustomerId、SalesId、ProductId等?代码还需要根据需要更新ModelBuilder。EF Core 2有这样的选项吗

它可以通过数据注释或FluentAPI来完成,并对任何选项开放

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");
}
公共类产品
{
[列(“产品ID”)]
公共int Id{get;set;}
公共字符串ProductName{get;set;}
公共字符串ProductDescription{get;set;}
公共浮动成本{get;set;}
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity()
.Property(p=>p.Id)
.HasColumnName(“产品ID”);
}

如果每个实体都有一个分部类,然后在T4模板中的每个实体上都有一个虚拟的
公共虚拟int Id{get;set;}
,则会发生这种情况,然后在类似上面的分部重写中。如果这样做有效,我将作为答案来写。hi@Seabizkit我不确定我是否理解注释,不建议使用第一个方法,公共部分类Product:IEntity{[NotMapped]public int Id{get=>ProductId;set=>ProductId=value;},第二个方法是,如果您为每个实体都有一个分部类,然后在T4模板中的每个实体上都有一个虚拟的
公共虚拟int Id{get;set;}
,则会发生thankshas,然后在类似上面的分部重写中。如果这样做有效,我将作为答案来写。hi@Seabizkit我不确定我是否理解注释,不建议使用第一种方法,公共部分类产品:IEntity{[NotMapped]public int Id{get=>ProductId;set=>ProductId=value;},第二种方法是,谢谢
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");
}