Давлен следующий абстрактный класс
Код: Выделить всё
public abstract class Bridge where Q : Entity where S : Entity
{
protected internal Q Left { get; set; }
protected internal R LeftId { get; set; }
protected internal S Right { get; set; }
protected internal T RightId { get; set; }
}
Код: Выделить всё
public class TestBridgeEntity : Bridge
{
public LeftSideEntity MyLeftSideEntity { get { return Left; } set { Left = value; } }
public Guid LeftSideEntityId { get { return LeftId; } set { LeftId = value; } }
public RightSideEntity MyRightSideEntity { get { return Right; } set { Right = value; } }
public Guid RightSideEntityId { get { return RightId; } set { RightId = value; } }
public double MyTestData { get; set; }
}
Код: Выделить всё
public static EntityTypeBuilder ConfigureBridge(this EntityTypeBuilder builder, string leftIdName, string rightIdName)
where T : Bridge where A: Entity where C:Entity
{
builder.HasKey(x => new { x.LeftId, x.RightId } );
builder.HasOne(x => x.Left).WithMany().HasForeignKey(x => x.LeftId);
builder.HasOne(x => x.Right).WithMany().HasForeignKey(x => x.RightId);
builder.Property(e => e.LeftId).HasColumnName(leftIdName);
builder.Property(e => e.RightId).HasColumnName(rightIdName);
return builder;
}
Код: Выделить всё
internal class MyTestBirdgeEntityConfiguration : IEntityTypeConfiguration
{
public void Configure(EntityTypeBuilder builder)
{
builder.ToTable("test_bridge");
builder.ConfigureBridge("left_side_id", "right_side_id");
builder.Property(e => e.MyTestData ).IsRequired();
}
}
Код: Выделить всё
public class LeftSideEntity
{
...
public List RightSideEntities { get; }
public List TestBridgeEntities { get; }
...
}
Когда я пытаюсь создать миграцию базы данных, я получаю следующую ошибку.
Невозможно создать «DbContext» типа «TestDbContext». Исключение «TestBridgeEntity.LeftId» и «TestBridgeEntity.LeftSideEntityId» сопоставляются со столбцом «left_side_id» в «test_bridge», но свойства содержатся в одной и той же иерархии. Все свойства типа сущности должны быть сопоставлены с уникальными столбцами.
Есть ли способ заставить работать такую абстракцию моста сущности? п>
Подробнее здесь: https://stackoverflow.com/questions/792 ... -in-efcore
Мобильная версия