小编Tra*_*er1的帖子

使用 MS SQL Server 生成的约束名称是否可以预测?

当您在 CREATE TABLE 脚本中创建主键和外键约束时,会创建命名约束,例如 FK__RecentlyVi__ScId__2764765D ...这些约束是否可预测?IE:如果你在另一台服务器上运行相同的创建脚本,约束名称是否相同?

我问是因为使用实体框架,当您对辅助表有多个引用时,您会获得诸如... Foreign、Foreign1、Foreign2 等属性...有时在重新生成实体模型时,顺序会有所不同...我想出了以下方法来解决这个问题,但想知道“默认”约束名称是否有效,即使我现在使用的是命名约束。

我的解决方法包括在下面。如果我将来再次需要这个,可以重构基于外键和对象类型名称获取实体的属性。

private Contact GetContactMatchForForeignKey(string foreignKeyName)
{
  var props = typeof(Order).GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(EdmRelationshipNavigationPropertyAttribute)));
  foreach (var prop in props) {
    var attrs = prop.GetCustomAttributes(typeof(EdmRelationshipNavigationPropertyAttribute), true);
    foreach (var attr in attrs) { 
      var a = (EdmRelationshipNavigationPropertyAttribute)attr;
      if (a.RelationshipName == foreignKeyName && a.TargetRoleName == "Contact") { 
        return (Contact)prop.GetValue(this, null);
      }
    }
  }
  return null;
}

private void SetContactMatchForForeignKey(string foreignKeyName, Contact value)
{
  var props = typeof(Contact).GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(EdmRelationshipNavigationPropertyAttribute)));
  foreach (var prop in props) …
Run Code Online (Sandbox Code Playgroud)

foreign-key database-design sql-server constraint entity-framework

4
推荐指数
1
解决办法
1289
查看次数