Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 如何更改自动生成的“;阴影属性“;所有实体的外键名称?_Entity Framework - Fatal编程技术网

Entity framework 如何更改自动生成的“;阴影属性“;所有实体的外键名称?

Entity framework 如何更改自动生成的“;阴影属性“;所有实体的外键名称?,entity-framework,Entity Framework,我有一个这样的模特 public class Class1 { public int identifier {get;set;} } public class Class2 { public int identifier {get;set;} public List<Class1> holders {get;set;} public List<Class1> users{get;set;} } 公共类1{ 公共int标识符{get;set

我有一个这样的模特

public class Class1 {
    public int identifier {get;set;}
}
public class Class2 {
    public int identifier {get;set;}
    public List<Class1> holders {get;set;}
    public List<Class1> users{get;set;}
}
公共类1{
公共int标识符{get;set;}
}
公共课2{
公共int标识符{get;set;}
公共列表持有者{get;set;}
公共列表用户{get;set;}
}
我的问题是Class1名称中生成的外键是“Class2\u标识符”和“Class2\u标识符1”,而我想要的是“Class2\u持有者\u标识符”和“Class2\u用户\u标识符”


真正的模型非常庞大,因此我要寻找的是在“添加迁移”步骤中覆盖名称的生成方式,这不是一个完整的实现,只是一个提示:如果您使用EntityFramework 6,则可以定义自定义模型约定:

public class ForeignKeyNamingConvention : IStoreModelConvention<AssociationType>
{
    public void Apply(AssociationType association, DbModel model)
    {
        if (association.IsForeignKey)
        {
            var constraint = association.Constraint;
            // Implement your renaming code.
            // The data you need is inside association.Constraint.
        }
    }
}
包含一些可以重用的代码(在本例中,约定用于删除列名中的下划线)

编辑:OP在此处包含了他们的最终解决方案:

ef-core“这与ef6中的问题相同,但没有消息”控制台中提到的问题

“Class1”和“Class2”之间存在多个关系,但没有配置外键属性,这会导致EF在“Organization”上创建阴影属性,其名称取决于发现顺序

公共类ForeignKeyNamingConvention:IStoreModelConvention
{
公共无效应用(AssociationType关联,DbModel模型)
{
if(association.IsForeignKey)
{
var constraint=association.constraint;
//因为我只需要fk列名更清晰
//“{entityName}{propertyName}”中提供
//{association.Name}
association.Constraint.TopProperties[0]。名称=association.Name;
}
}
}

不是一个完整的实现,只是一个提示:如果您使用的是EntityFramework 6,则可以定义自定义模型约定:

public class ForeignKeyNamingConvention : IStoreModelConvention<AssociationType>
{
    public void Apply(AssociationType association, DbModel model)
    {
        if (association.IsForeignKey)
        {
            var constraint = association.Constraint;
            // Implement your renaming code.
            // The data you need is inside association.Constraint.
        }
    }
}
包含一些可以重用的代码(在本例中,约定用于删除列名中的下划线)

编辑:OP在此处包含了他们的最终解决方案:

ef-core“这与ef6中的问题相同,但没有消息”控制台中提到的问题

“Class1”和“Class2”之间存在多个关系,但没有配置外键属性,这会导致EF在“Organization”上创建阴影属性,其名称取决于发现顺序

公共类ForeignKeyNamingConvention:IStoreModelConvention
{
公共无效应用(AssociationType关联,DbModel模型)
{
if(association.IsForeignKey)
{
var constraint=association.constraint;
//因为我只需要fk列名更清晰
//“{entityName}{propertyName}”中提供
//{association.Name}
association.Constraint.TopProperties[0]。名称=association.Name;
}
}
}

使用实体框架的映射。@GertArnold我需要一些全局配置,而不是单独处理每个实体。。。。我尝试使用DbModelBuilder.Properties,但无法使用,因为生成的列在模型中没有属性。感谢您将最终代码添加到我的响应中!我希望你不介意我的编辑。@Diana:没问题,编辑得很好,谢谢使用实体框架的映射。@GertArnold我需要一些全局配置,而不是单独处理每个实体。。。。我尝试使用DbModelBuilder.Properties,但无法使用,因为生成的列在模型中没有属性。感谢您将最终代码添加到我的响应中!我希望你不介意我编辑。@Diana:没问题,编辑得好,谢谢
public class ForeignKeyNamingConvention : IStoreModelConvention<AssociationType>
{
    public void Apply(AssociationType association, DbModel model)
    {
        if (association.IsForeignKey)
        {
            var constraint = association.Constraint;
            // as i just needed the fk column name to be more clear 
            // "{entityName}_{propertyName}" which is provided in 
            // {association.Name}
            association.Constraint.ToProperties[0].Name = association.Name;
        }
    }
}