Creating Primary Key field on MVC class

Description

Entity Framework CodeFirst recognize the key, by default, by name. Valid names are Id or <YourClassName>Id.

Your property should named Id or AccountTypesId

Another way is to use the ModelBuilder to specify the key.

Sample

public class MyDbContext : DbContext
{
    public DbSet<AccountTypes> AccountTypes { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<AccountTypes>.HasKey(x => x.AccountTypeID);
        base.OnModelCreating(modelBuilder);
    }
}

Mode Information

  • Entity Framework Code First Tutorial

Try using [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)] property to indicate the key field.

The regular field would go with EntityKeyPropert=false.