This is based on a previous question I had: EF Code First implemented interface property
I have something like this.
interface IKeywordedEntity
{
ICollection<Keyword> Keywords { get; }
}
class Foo : EntityBase, IKeywordedEntity
{
public virtual ICollection<Keyword> Keywords { get { ... } }
}
The details of EntityBase
aren't important.
Initially I wrote this extension method to keep things DRY:
public static void WithKeywords<TEntityType>(this EntityTypeConfiguration<TEntityType>
entityTypeConfiguration) where TEntityType : EntityBase, IKeywordedEntity
{
entityTypeConfiguration.HasMany(e => e.Keywords).WithMany();
}
I would invoke it like so:
modelBuilder.Entity<Foo>.WithKeywords();
However, Entity Framework or the C# compiler is treating e
in the lambda as IKeywordedEntity
not TEntityType
. This freaks out Entity Framework.
Knowing this, I experimented with manually writing the lambda as an Expression
for HasMany
. I came up with the following:
public static void WithKeywords<TEntityType>(this EntityTypeConfiguration<TEntityType>
entityTypeConfiguration) where TEntityType : EntityBase, IKeywordedEntity
{
var rootExpression = Expression.Parameter(typeof (TEntityType));
var expression = Expression.Property(rootExpression, "Keywords");
entityTypeConfiguration.HasMany(Expression.Lambda<Func<TEntityType, ICollection<Keyword>>>(expression, rootExpression)).WithMany();
}
Now the IntelliSense is showing me the combination of EntityBase
and IKeywordedEntity
correctly and telling me that e
is TEntityType
.
My question is: why does the passed/compiled expression tree from the lambda treat e
as IKeywordedEntity
, not TEntityType
?