I have the following entity which has a collection of itself and a parent property which I want to be null if it is the root widget:
public class CanvasWidgetQuery
{
public int CanvasWidgetId { get; set; }
public int? ParentCanvasWidgetId { get; set; }
public virtual CanvasWidgetQuery ParentCanvasWidget { get; set; }
public virtual ICollection<CanvasWidgetQuery> InnerCanvasWidgets { get; set; }
}
Because CanvasWidgetQuery
is actually just a base class (might as well be abstract but I'm not putting abstract until I see it work without it). I wanted to create a generic mapping configuration to map derived types of CanvasWidgetQuery
:
public class CanvasWidgetQueryMap<TCanvasWidgetQuery> : EntityTypeConfiguration<TCanvasWidgetQuery>
where TCanvasWidgetQuery : CanvasWidgetQuery
{
public CanvasWidgetQueryMap()
{
this.HasKey(q => q.CanvasWidgetId);
this.HasMany(w => w.InnerCanvasWidgets)
.WithOptional(w => w.ParentCanvasWidget)
.HasForeignKey(w => w.ParentCanvasWidgetId);
}
}
Regardless of the derived type, they all have those properties but unfortunately it complains about the WithOptional
call (2 errors at once):
Error 1 Cannot implicitly convert type 'CanvasWidgetQuery' to 'TCanvasWidgetQuery'. An explicit conversion exists (are you missing a cast?)
Error 2 Cannot convert lambda expression to delegate type 'System.Func<CanvasWidgetQuery,TCanvasWidgetQuery>' because some of the return types in the block are not implicitly convertible to the delegate return type
If I move this up a level to the mapping of the actual derived type:
public class SearchCanvasWidgetQueryMap : CanvasWidgetQueryMap<SearchCanvasWidgetQuery>
{
public SearchCanvasWidgetQueryMap()
{
this.HasMany(w => w.InnerCanvasWidgets)
.WithOptional(w => w.ParentCanvasWidget)
.HasForeignKey(w => w.ParentCanvasWidgetId);
}
}
Now it complains that it cannot implicity convert CanvasWidgetQuery
into SearchCanvasWidgetQuery
meanwhile:
public class SearchCanvasWidgetQuery : CanvasWidgetQuery
{
}
How can I accomplish what I'm after
Have you tried defining the map for the base class which holds the properties which define the hierarchy?
e.g:
public class CanvasWidgetQueryMap<CanvasWidgetQuery> : EntityTypeConfiguration<CanvasWidgetQuery>
{
public CanvasWidgetQueryMap()
{
this.HasKey(q => q.CanvasWidgetId);
this.HasMany(w => w.InnerCanvasWidgets)
.WithOptional(w => w.ParentCanvasWidget)
.HasForeignKey(w => w.ParentCanvasWidgetId);
}
}
There's some good additional information in the answer here: CTP 4 doesn't consider base class properties which might be helpful if the above doesn't achieve what you're after.