Following is the starting code structure
<code>public abstract class ChildBase;
public class Parent1Child : ChildBase;
public class Parent2Child : ChildBase;
public class ParentBase<T1>
public List<ChildBase> Children = new List<ChildBase>();
public class Parent1 : ParentBase<Parent1Child>;
<code>public abstract class ChildBase;
public class Parent1Child : ChildBase;
public class Parent2Child : ChildBase;
public class ParentBase<T1>
where T1 : ChildBase
{
public List<ChildBase> Children = new List<ChildBase>();
}
public class Parent1 : ParentBase<Parent1Child>;
</code>
public abstract class ChildBase;
public class Parent1Child : ChildBase;
public class Parent2Child : ChildBase;
public class ParentBase<T1>
where T1 : ChildBase
{
public List<ChildBase> Children = new List<ChildBase>();
}
public class Parent1 : ParentBase<Parent1Child>;
I would like to create an extension method for ef core’s EntityTypeBuilder
so I am able to extend the entity in a generic way
Following are the attempts:
<code>public static class ParentExtensions
public static void Test1<T>(this ParentBase<T> parent)
var children = parent.Children;
public static class EfExtensions
public static void Test2<TEntity>(this EntityTypeBuilder<TEntity> builder)
where TEntity : ParentBase<ChildBase>
builder.HasMany(x => x.Children).WithOne();
public static void Test3<TEntity, TChild>(this EntityTypeBuilder<TEntity> builder)
where TEntity : ParentBase<TChild>
builder.HasMany(x => x.Children).WithOne();
<code>public static class ParentExtensions
{
public static void Test1<T>(this ParentBase<T> parent)
where T : ChildBase
{
var children = parent.Children;
}
}
public static class EfExtensions
{
public static void Test2<TEntity>(this EntityTypeBuilder<TEntity> builder)
where TEntity : ParentBase<ChildBase>
{
builder.HasMany(x => x.Children).WithOne();
}
public static void Test3<TEntity, TChild>(this EntityTypeBuilder<TEntity> builder)
where TEntity : ParentBase<TChild>
where TChild : ChildBase
{
builder.HasMany(x => x.Children).WithOne();
}
}
</code>
public static class ParentExtensions
{
public static void Test1<T>(this ParentBase<T> parent)
where T : ChildBase
{
var children = parent.Children;
}
}
public static class EfExtensions
{
public static void Test2<TEntity>(this EntityTypeBuilder<TEntity> builder)
where TEntity : ParentBase<ChildBase>
{
builder.HasMany(x => x.Children).WithOne();
}
public static void Test3<TEntity, TChild>(this EntityTypeBuilder<TEntity> builder)
where TEntity : ParentBase<TChild>
where TChild : ChildBase
{
builder.HasMany(x => x.Children).WithOne();
}
}
for the scenario when I want to invoke the extension method directly on the object – the generics are implied (omitted) and compiler does not complain:
<code>var parent = new Parent1();
<code>var parent = new Parent1();
parent.Test1(); // works
</code>
var parent = new Parent1();
parent.Test1(); // works
but when I try to invoke the extension method on a builder – the compiler starts to complain unless I explicitly provide the generic types!
<code>EntityTypeBuilder<Parent1> builder;
builder.Test2(); // wrong
builder.Test3(); // wrong
builder.Test3<Parent1, Parent1Child>(); // works!
<code>EntityTypeBuilder<Parent1> builder;
builder.Test2(); // wrong
builder.Test3(); // wrong
builder.Test3<Parent1, Parent1Child>(); // works!
</code>
EntityTypeBuilder<Parent1> builder;
builder.Test2(); // wrong
builder.Test3(); // wrong
builder.Test3<Parent1, Parent1Child>(); // works!
Why is it required to provide the generic explicitly when invoking the builder extension method? I think that passing the child class type is redundant and therefore should be omitted.. Any way to fix it?