当前正在使用.NET Core 2.1和EF Core 2.1进行DDD项目,并尝试将我的域事件存储在SQL Server中。我想使用新的owned entity types
将我的TripEventData
类“展平”到一个TripEvents
表中
我的域事件类层次结构如下:
public class TripEventData
{
public TripEventName EventName { get; set; }//enum
public byte[] Data { get; set; }
}
public abstract class DomainEvent
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public string EventId { get; protected set; }
public Guid TripId { get; private set; }
public DateTimeOffset EventTime { get; protected set; }
public abstract TripEventData SerializedData { get; protected set; }
}
public class AddTripPassengerEvent
{
[NotMapped]
public int UniqueUserId { get; protected set; }
[NotMapped]
public string FirstName { get; protected set; }
[NotMapped]
public string DestStopId { get; protected set; }
public override LiveTripEventData SerializedData
{
get
{
return new LiveTripEventData
{
EventName = LiveTripEventName.AddLiveTripPassenger,
Data = ProtobufSerializer.Serialize(new TripPassengerEventData()
{
UniqueUserId = this.UniqueUserId,
FirstName = this.FirstName,
DestStopId = this.DestStopId
})
};
}
protected set { }
}
}
internal class TripEventTypeConfiguration : IEntityTypeConfiguration<TripEvent>
{
public void Configure(EntityTypeBuilder<TripEvent> tripEventConfiguration)
{
tripEventConfiguration.ToTable("TripEvents");
tripEventConfiguration.HasKey(o => new { o.EventId});
//Treat as a value object to flatten into one table
tripEventConfiguration.OwnsOne(o => o.SerializedData);
}
}
每当我尝试使用SaveChanges()保留代码时,都会出现以下错误消息:
AddTripPassengerEvent”与“ TripEvents”表共享 类型为“ TripEventData”的实体,但没有此实体 键入与标记为“已添加”的键值相同的键值。
在对此错误消息进行研究之后,我只能找到以下答案github issue,其中基本上指出:
拥有的实体仍然是实体。使用表拆分(默认设置)时,拥有实体不是可选的。
换句话说,拥有的实体不能为null。在我的测试案例中,我确保所有拥有的实体字段都具有值,并且当前没有空值,但我仍然遇到此问题