Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relationship on owned type

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?

like image 935
maxence51 Avatar asked Jan 05 '18 10:01

maxence51


People also ask

What is an owned entity type?

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.

When would you use EF6 vs EF core?

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.

What is IEntityTypeConfiguration?

IEntityTypeConfiguration<TEntity> InterfaceAllows configuration for an entity type to be factored into a separate class, rather than in-line in OnModelCreating(ModelBuilder).

What is OnModelCreating?

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.


1 Answers

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.

like image 198
Suhaib Syed Avatar answered Nov 15 '22 21:11

Suhaib Syed