Я пытаюсь реализовать IEntityTypeConfiguration, который я мог бы повторно использовать для настройки расширенной сущности:
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
public partial class Foo : IFoo
{
public long Id { get; set; }
public virtual Bar Bar { get; set; }
}
public interface IFoo
{
long Id { get; set; }
Bar Bar { get; set; }
}
public partial class Bar
{
public long Id { get; set; }
public long FooId { get; set; }
public virtual IFoo Foo { get; set; }
}
public class FooConfig<TEntity> : IEntityTypeConfiguration<TEntity> where TEntity : class, IFoo
{
public void Configure(EntityTypeBuilder<TEntity> builder)
{
builder.HasKey(e => e.Id);
builder.HasOne(e => e.Bar)
.WithOne(e => e.Foo);
}
}
Получение ошибок:
Error CS0266 Cannot implicitly convert type 'IFoo' to 'TEntity'. An explicit conversion exists (are you missing a cast?)
Error CS1662 Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type
Есть ли способ "обмануть" лямбдуили структура FooConfig, чтобы код компилировался?Логически я считаю, что код правильный, и он компилируется и функционирует, как и ожидалось, если я сделаю FooConfig не универсальным:
public class FooConfigNonGeneric : IEntityTypeConfiguration<IFoo>
{
public void Configure(EntityTypeBuilder<IFoo> builder)
{
builder.HasKey(e => e.Id);
builder.HasOne(e => e.Bar)
.WithOne(e => e.Foo);
}
}
Но тогда я не могу переопределить метод Configure при использовании фактической сущности:
public partial class FooExtended : Foo
{
public string AnotherProperty { get; set; }
}
public class FooExtendedConfig: FooConfigNonGeneric
{
public override void Configure(EntityTypeBuilder<FooExtended> builder)
{
base.Configure(builder);
}
}
Ошибка:
Error CS0115 'FooExtendedConfig.Configure(EntityTypeBuilder<FooExtended>)': no suitable method found to override