Niyoko
Niyoko

Reputation: 7672

Entity Framework 6 Custom Relationship Convention

I have read this documentation about convention in Entity Framework 6. But it does not contain convention for Relationship.

Suppose I have following model:

[TablePrefix("mst")]
public class Guru
{
    public int Id { get; set; }

    public int? IdKotaLahir { get; set; }

    public virtual Kota KotaLahir { get; set; }
}

I want property IdKotaLahir to be foreign key of navigation property KotaLahir. Foreign key name is "Id"+<NavigationPropertyName>. Is it possible using current version of entity framework (EF 6 alpha 3)?

Upvotes: 2

Views: 3577

Answers (2)

kevinpo
kevinpo

Reputation: 1963

In EF6, the accepted answer's convention no longer works because IConfigurationConvention is internal. The way to handle this type of scenario is by inheriting from 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;
    }
}

Upvotes: 3

Pawel
Pawel

Reputation: 31610

Is it just one property or you need this across the board (i.e. the whole model is using a convention where foreign key names are always "Id" + NavigationPropertyName)? If you just want the foreign key for a single entity you will be better off just using the ForeignKey attribute:

public class Guru
{
    public int Id { get; set; }

    public int? IdKotaLahir { get; set; }

    [ForeignKey("IdKotaLahir")]
    public virtual Kota KotaLahir { get; set; }
}

This will work for both EF5 and EF6. In EF6 you can use custom conventions to configure foreign key properties. Here is custom convention I came up with:

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;
        }           
    }
}

I also wrote a more detailed blog post on this.

Upvotes: 2

Related Questions