Relationship on owned type

c# entity-framework-core

Question

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?

1
11
1/5/2018 10:40:17 AM

Popular Answer

You cannot set a list of objects as a owned type, because the columns of your History table are added to the InboundRequest table. The column count of the table must be fixed and not dynamic.

If you write the following:

public RequestHistoryEntry HistoryEntries { get; set; }

Then your InboundRequest table looks like that (or similar):

| Id | RequestHistory_State | RequestHistory_Timestamp | RequestHistory_Id |

If you are using a List of RequestHistoryEntry, then the column count is not clear. Therfore you have to use a separate table for it.

0
1/31/2019 8:12:11 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow