EF迁移失败,并告诉我派生类型未定义密钥。
我正在尝试设置TPH,使用属性应该很容易,但是我无法将其与单独的(流畅的)配置文件一起使用。它总是报告未定义派生类型的键。
如果我在派生类型上定义它,那么它说必须为基本类型定义键。
实体类型配置:
public class CoreEntityConfiguration<TBaseModel> : IEntityTypeConfiguration<TBaseModel> where TBaseModel : class, ICoreEntity
{
public virtual void Configure(EntityTypeBuilder<TBaseModel> builder)
{
builder.Property(p => p.Created).IsDateColumn().HasDefaultValueSql("GETDATE()").ValueGeneratedOnAdd();
builder.Property(p => p.Modified).IsDateColumn().HasDefaultValueSql("GETDATE()").ValueGeneratedOnAddOrUpdate();
builder.Property(p => p.Rowversion).IsRowVersion();
}
}
public class BaseEntityConfiguration<TBaseEntity, TKey> : CoreEntityConfiguration<TBaseEntity> where TBaseEntity : BaseEntity<TKey>
{
public override void Configure(EntityTypeBuilder<TBaseEntity> builder)
{
base.Configure(builder);
builder.HasKey(p => p.ID);
}
}
public class EventConfiguration : BaseEntityConfiguration<Event, int>
{
public override void Configure(EntityTypeBuilder<Event> builder)
{
base.Configure(builder);
builder.ApplyConfiguration(new SearchableConfiguration<Event, int>());
builder.Property(p => p.Time).IsDateTimeOffsetColumn();
builder.HasOne(p => p.Player).WithMany(p => p.Events).HasForeignKey(p => p.PlayerID).OnDelete(Microsoft.EntityFrameworkCore.DeleteBehavior.Restrict);
builder.HasDiscriminator(p => p.EventType)
.HasValue<Goal>("Goal")
.HasValue<Card>("Card")
.HasValue<Substitution>("Substitution");
builder.ToTableWithSchema(Constants.SCHEME_OPERATIONS);
}
}
public class EventGoalConfiguration : IEntityTypeConfiguration<Goal>
{
public void Configure(EntityTypeBuilder<Goal> builder)
{
builder.HasBaseType<Event>();
builder.HasOne(p => p.GoalType).WithMany().OnDelete(DeleteBehavior.Restrict);
builder.HasOne(p => p.AssistedByPlayer).WithMany(p => p.Assists).HasForeignKey(p => p.AssistedByPlayerID).OnDelete(DeleteBehavior.SetNull);
}
}
模型类:
public abstract class CoreEntity : ICoreEntity
{
public DateTime Created { get; set; }
public int CreatedByID { get; set; }
public DateTime? Modified { get; set; }
public int? ModifiedByID { get; set; }
public byte[] Rowversion { get; set; }
}
public abstract class BaseEntity<TKey> : CoreEntity, IBaseEntity<TKey>
{
public TKey ID { get; set; }
}
public abstract class Event : BaseEntity<int>
{
...
}
public class Goal : Event
{
public int GoalTypeID { get; set; }
public virtual GoalType GoalType { get; set; }
public int? AssistedByPlayerID { get; set; }
public virtual Player AssistedByPlayer { get; set; }
}
我无法摆脱这个错误:
请帮助我,告诉我我做错了什么。我似乎无法弄清楚...