C# 实体框架6自定义关系约定
我已经阅读了实体框架6中有关约定的文档。但它不包含关系的约定 假设我有以下模型:C# 实体框架6自定义关系约定,c#,entity-framework,entity-framework-6,C#,Entity Framework,Entity Framework 6,我已经阅读了实体框架6中有关约定的文档。但它不包含关系的约定 假设我有以下模型: [TablePrefix("mst")] public class Guru { public int Id { get; set; } public int? IdKotaLahir { get; set; } public virtual Kota KotaLahir { get; set; } } 我希望属性IdKotaLahir是导航属性KotaLahir的外键。 外键名为Id
[TablePrefix("mst")]
public class Guru
{
public int Id { get; set; }
public int? IdKotaLahir { get; set; }
public virtual Kota KotaLahir { get; set; }
}
我希望属性IdKotaLahir是导航属性KotaLahir的外键。
外键名为Id+。
是否可以使用当前版本的entity framework EF 6 alpha 3?它只是一个属性,还是您需要全面使用它,即整个模型使用外键名称始终为Id+NavigationPropertyName的约定?如果只需要单个实体的外键,最好只使用ForeignKey属性:
public class Guru
{
public int Id { get; set; }
public int? IdKotaLahir { get; set; }
[ForeignKey("IdKotaLahir")]
public virtual Kota KotaLahir { get; set; }
}
这对EF5和EF6都有效。在EF6中,您可以使用自定义约定来配置外键属性。以下是我提出的定制惯例:
public class NavigationPropertyConfigurationConvention
: IConfigurationConvention<PropertyInfo, NavigationPropertyConfiguration>
{
public void Apply(
PropertyInfo propertyInfo, Func<NavigationPropertyConfiguration> configuration)
{
var foreignKeyProperty =
propertyInfo.DeclaringType.GetProperty("Id" + propertyInfo.Name);
if (foreignKeyProperty != null && configuration().Constraint == null)
{
var fkConstraint = new ForeignKeyConstraintConfiguration();
fkConstraint.AddColumn(foreignKeyProperty);
configuration().Constraint = fkConstraint;
}
}
}
我还写了一篇更详细的文章 它只是一个属性,还是您需要一个全面的属性,即整个模型使用外键名称始终为Id+NavigationPropertyName的约定?如果只需要单个实体的外键,最好只使用ForeignKey属性:
public class Guru
{
public int Id { get; set; }
public int? IdKotaLahir { get; set; }
[ForeignKey("IdKotaLahir")]
public virtual Kota KotaLahir { get; set; }
}
这对EF5和EF6都有效。在EF6中,您可以使用自定义约定来配置外键属性。以下是我提出的定制惯例:
public class NavigationPropertyConfigurationConvention
: IConfigurationConvention<PropertyInfo, NavigationPropertyConfiguration>
{
public void Apply(
PropertyInfo propertyInfo, Func<NavigationPropertyConfiguration> configuration)
{
var foreignKeyProperty =
propertyInfo.DeclaringType.GetProperty("Id" + propertyInfo.Name);
if (foreignKeyProperty != null && configuration().Constraint == null)
{
var fkConstraint = new ForeignKeyConstraintConfiguration();
fkConstraint.AddColumn(foreignKeyProperty);
configuration().Constraint = fkConstraint;
}
}
}
我还写了一篇更详细的文章 在EF6中,接受答案的约定不再有效,因为IConfigurationConvention是内部的。处理此类场景的方法是继承ForeignKeyDiscoveryConvention
public class MyForeignKeyDiscoveryConvention : ForeignKeyDiscoveryConvention
{
protected override bool MatchDependentKeyProperty(AssociationType associationType, AssociationEndMember dependentAssociationEnd,
EdmProperty dependentProperty, EntityType principalEntityType, EdmProperty principalKeyProperty)
{
string navigationPropertyName = ((System.Reflection.PropertyInfo)dependentAssociationEnd.MetadataProperties.GetValue("ClrPropertyInfo", false).Value).Name;
// The standard foreign key property to look for is NavigationProperty_PrimaryKeyName (e.g. "TableName_Id").
// Use the below line to remove that underscore.
//return dependentProperty.Name == navigationPropertyName + principalKeyProperty.Name;
// Use the following for the IdKotaLahir scenario
return dependentProperty.Name == "Id" + navigationPropertyName;
}
}
在EF6中,接受答案的约定不再有效,因为IConfigurationConvention是内部的。处理此类场景的方法是继承ForeignKeyDiscoveryConvention
public class MyForeignKeyDiscoveryConvention : ForeignKeyDiscoveryConvention
{
protected override bool MatchDependentKeyProperty(AssociationType associationType, AssociationEndMember dependentAssociationEnd,
EdmProperty dependentProperty, EntityType principalEntityType, EdmProperty principalKeyProperty)
{
string navigationPropertyName = ((System.Reflection.PropertyInfo)dependentAssociationEnd.MetadataProperties.GetValue("ClrPropertyInfo", false).Value).Name;
// The standard foreign key property to look for is NavigationProperty_PrimaryKeyName (e.g. "TableName_Id").
// Use the below line to remove that underscore.
//return dependentProperty.Name == navigationPropertyName + principalKeyProperty.Name;
// Use the following for the IdKotaLahir scenario
return dependentProperty.Name == "Id" + navigationPropertyName;
}
}
整个模型都使用这种约定。所以,如果可用,最好使用约定,而不是使用属性或fluent API。在这种情况下,使用约定是有意义的。我用自己的约定更新了答案,并写了一篇博文,其中包含了答案中链接的更多细节。顺便说一句,这个答案不再适用于ef-6。他们让IConfigurationConvention在整个模型中使用该约定。所以,如果可用,最好使用约定,而不是使用属性或fluent API。在这种情况下,使用约定是有意义的。我用自己的约定更新了答案,并写了一篇博文,其中包含了答案中链接的更多细节。顺便说一句,这个答案不再适用于ef-6。他们将IConfigurationConvention设置为内部