I want to define a one to many relationship on an owned type.
Here in my example,
InboundRequest: principal entity
RequestHistory: owned type
RequestHistoryEntry : Dependency entity
public class InboundRequest : IAggregateRoot
{
public int Id { get; private set; }
public RequestHistory History { get; private set; }
}
public class RequestHistory
{
public IList<RequestHistoryEntry> HistoryEntries { get; set; }
}
public class RequestHistoryEntry
{
public RequestState State { get; private set; }
public DateTime Timestamp { get; private set; }
public int Id { get; set; }
}
builder.Entity<InboundRequest>().OwnsOne(x => x.History);
Unfortunately EF Core gives me the following error:
The relationship from 'RequestHistoryEntry' to 'InboundRequest.History#RequestHistory.HistoryEntries' is not supported because the owned entity type 'InboundRequest.History#RequestHistory' cannot be on the principal side.
Is there a way to have a one to many relationship between an owned type and a list of dependencies?
The entity containing an owned entity type is its owner. Owned entities are essentially a part of the owner and cannot exist without it, they are conceptually similar to aggregates. This means that the owned entity is by definition on the dependent side of the relationship with the owner.
Keep using EF6 if the data access code is stable and not likely to evolve or need new features. Port to EF Core if the data access code is evolving or if the app needs new features only available in EF Core. Porting to EF Core is also often done for performance.
IEntityTypeConfiguration<TEntity> InterfaceAllows configuration for an entity type to be factored into a separate class, rather than in-line in OnModelCreating(ModelBuilder).
The DbContext class has a method called OnModelCreating that takes an instance of ModelBuilder as a parameter. This method is called by the framework when your context is first created to build the model and its mappings in memory.
The only way to have a one to many relationship with owned types is if the owned entity is on the many side of the relationship, in which case ef core will map it to a separate table by convention. What you are attempting is per se not possible by design. However, in your particular case, why don't you get rid of RequestHistory
altogether and make the list of RequestHistoryEntry
as a collection of owned type to your InboundRequest
as follows:
public class InboundRequest : IAggregateRoot
{
public int Id { get; private set; }
public IList<RequestHistoryEntry> HistoryEntries { get; set; }
}
public class RequestHistoryEntry
{
public RequestState State { get; private set; }
public DateTime Timestamp { get; private set; }
public int Id { get; set; }
}
builder.Entity<InboundRequest>().OwnsMany(x => x.HistoryEntries);
That should get rid of the error as well as achieve your desired relationship to your dependent entity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With