jeudi 19 janvier 2017

Generic Entity Framework mapping

To beginning I'm novice with Expression and Func.

I'm trying to avoid duplicate code in my EF mapping class, and I'm stuck with bad database.

Take the following example map class:

public class EntityMap : EntityTypeConfiguration<Entity>
{
    public EntityMap()
    {
        Property(x => x.PropertyA.Property);
        Property(x => x.PropertyB.Property);
    }
}

Where PropertyA and PropertyB are same type, and with many property Is it possible to refactor this with a simple method, pass x => x.PropertyA or PropertyB in parameters and do something like Property(x => x. methodParemeter Property); ? And how ? The method could be something like that:

private void SubMap(Expression<Func<Entity, SubEntity>> propertyExpression, string prefix)
{
    Property(x => x.propertyExpression.Property)
            .HasColumnName(string.Format("{0}{1}", prefix,"columnName"));
}





Aucun commentaire:

Enregistrer un commentaire