EF 数据库第一次代码生成缺少 Key 和数据注释

EF database first code generation missing Key and data annotation

对于EF 6.1及以上版本,当我在VS15中添加/逆向工程-实体模型model/code生成 & Sql-Server 2k16数据库我的实体缺少 Id 和自动增量数据注释Saw this and this 关于 SO 的问题,但没有答案,只是它是一个错误,我正在寻找一个选项来生成 PK 密钥,自动增量。

问题:如何确保在代码生成过程中将 Id 键和自动递增选项添加到实体 ?除了外键,没有数据注释!我还可以让 EF 生成复合键吗?

我做了什么: 在数据库中,我在 [Id] col 上添加了 Set primary key as int, 我还设置了Identity trueseed 1auto increment 1

例如缺少主键


//E.g. Reverse Eng. Generated code from ASP table 
public partial class AspNetUsers
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public AspNetUsers()
    {
        this.AspNetUserClaims = new HashSet<AspNetUserClaims>();
        this.AspNetUserLogins = new HashSet<AspNetUserLogins>();
        this.AspNetRoles = new HashSet<AspNetRoles>();
    }
    // Missing Primary Key
    public string Id { get; set; }
    public Nullable<int> IdNumber { get; set; } ...

例如2 缺少主键和自动递增

public partial class AuditNetEvent
{
    //Reverse Generated code missing Primary Key & Auto Increment
    public bigint Id { get; set; }
    public System.DateTime InsertedDate { get; set; }

当默认约定使注释变得多余时,该工具似乎没有添加注释。对于 Id 列,默认是它的 PK 和身份。我尝试使用不符合约定的 table(偏离 PK 列名称且没有标识)并添加了注释:

[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int CstId { get; set; }