omitting generics in extension methods

Following is the starting code structure

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>var parent = new Parent1();
parent.Test1(); // works
</code>
<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!

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>EntityTypeBuilder<Parent1> builder;
builder.Test2(); // wrong
builder.Test3(); // wrong
builder.Test3<Parent1, Parent1Child>(); // works!
</code>
<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?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật