Entity Framework 4.1: Name constraints

后端 未结 2 1214
独厮守ぢ
独厮守ぢ 2021-01-15 10:35

We are working with entity framework 4.1 - code first. We need to be able to name our constraints and not have their names auto-generated by SQL Server. Is this possible?<

相关标签:
2条回答
  • 2021-01-15 10:58

    Could you run some raw sql to rename them after creation? You may need to disable the EdmMetadata convention.

    context.Database.ExecuteSqlCommand(
      @"DECLARE @pk sysname
        SELECT @pk = name FROM sysobjects WHERE parent_obj = object_id('users') and xtype = 'pk'
        EXEC sp_rename @pk, 'pk_users_UserId'
    ");
    
    0 讨论(0)
  • 2021-01-15 11:18

    The short answer is no. The longer answer is about meaning of the code first. Code-first means you are not interested in the database - you just let EF to create some and that is all what you need. It allows you defining names for tables and columns (it is useful especially when working with existing databases) but that is all.

    If you need to work with database in such level you need database-first approach. Abusing code first to name constraints is possible but it is terribly hard and complex (it requires dropping old constraints after creation and create a new ones - example is here).

    0 讨论(0)
提交回复
热议问题