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

后端 未结 5 657
眼角桃花
眼角桃花 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:17

    (This is a complement to the answer/comments by @Jeff Sivers and @cubski.)

    As far as I know you can't specify the PK name with Data Attribute. Sometimes I need to get rid of the dbo. part of the name and I then use a manually edited code first migration to change the name, like this:

    public partial class ChangeNameOnPKInCustomers : DbMigration
    {
        private static string fromName = "PK_dbo.Customers";    // Name to change
    
        private static string toName = fromName.Replace("dbo.", "");
    
        public override void Up()
        {
            Sql($"exec sp_rename @objname=N'[dbo].[{fromName}]', @newname=N'{toName}'");
            // Now the PK name is "PK_Customers".
        }
    
        public override void Down()
        {
            Sql($"exec sp_rename @objname=N'[dbo].[{toName}]', @newname=N'{fromName}'");
            // Back to "PK_dbo.Customers".
        }
    }
    

提交回复
热议问题