How to Specify Primary Key Name in EF-Code-First

后端 未结 5 640
眼角桃花
眼角桃花 2020-12-01 10:37

I\'m using Entity Framework Codefirst to create my Database. The default Primary key with the schema name dbo.pk_Jobs seems to upset access 2007 when I connect to it over OD

5条回答
  •  醉梦人生
    2020-12-01 11:06

    If you want to specify the column name and override the property name, you can try the following:

    Using Annotations

    public class Job
    {
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Column("CustomIdName")]
        public Guid uuid { get; set; }
        public int active { get; set; }
    }
    

    Using Code First

        protected override void OnModelCreating(DbModelBuilder mb)
        {
            base.OnModelCreating(mb);
    
            mb.Entity()
                .HasKey(i => i.uuid);
            mb.Entity()
              .Property(i => i.uuid)
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
              .HasColumnName("CustomIdName");
        }
    

    Inside Migration Configuration

    public partial class ChangePrimaryKey : DbMigration
    {
        public override void Up()
        {
            Sql(@"exec sp_rename 'SchemaName.TableName.IndexName', 'New_IndexName', 'INDEX'");
        }
    
        public override void Down()
        {
            Sql(@"exec sp_rename 'SchemaName.TableName.New_IndexName', 'Old_IndexName', 'INDEX'");
        }
    }
    

提交回复
热议问题