Owned type w/ HasMany
See original GitHub issueFirst, I’m assuming Owned Types are basically trying to target https://www.martinfowler.com/bliki/DDD_Aggregate.html
I fully understand why an owned entity cannot be owned by multiple other entities (as is specified in the docs). It simply doesn’t make sense from a conceptual perspective, and very unclear technically. However, I see no reason why there can’t be relationships to the owned entity? Basically, I don’t understand why the following error message exists: The type 'Owned' cannot be configured as non-owned because an owned entity type with the same name already exists.
Below I have two scenarios. One using a DDD Aggregate approach, one without. I assume you can infer the relationships from the types, so no need for the configuration.
The one without works fine, but the one using the aggregate approach doesn’t. I don’t see any technical (or conceptual) reasons (whether one splits up Owner/Owned into two entities or keeps them in one, e.g. FlattenedOwner) shouldn’t make any difference? It’s the same amount of tables, just mapped to either 1 or 2 entities? Or am I just missing how to properly configure such scenario?
Doesn’t work:
public class Owner
{
public int OwnerId { get; set; }
public Owned Owned { get; set; }
}
public class Owned
{
public string SomeProperty { get; set; }
public List<Related> Relateds { get; set; }
}
public class Related
{
public int RelatedId { get; set; }
public Owned Owned { get; set; }
}
Works:
public class FlattenedOwner
{
public int OwnerId { get; set; }
public string SomeProperty { get; set; }
public List<Related> Relateds { get; set; }
}
public class RelatedToFlattened
{
public int RelatedId { get; set; }
public FlattenedOwner FlattenedOwner { get; set; }
}
EF Core version: 5.0 Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer) Target framework: (e.g. .NET 5.0) Operating system: IDE: (e.g. Visual Studio 2019 16.8.5)
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:19 (8 by maintainers)
@cjblomqvist You need to configure the nested parts of the model using the “owned” APIs. For example:
This generates the following model:
I think this issue shouldn’t be closed. Martin Fowler described ideal case which is rarely met in practice. It is often needed to Navigate to owned type over an aggregate having benefits of the owned types simultaneously. My real life example is questionnaires:
With this model we conduct surveys using questions modelled by the class above. Questions could have possible answers which are natively owned type as they have an OrderNumber field. It makes us to introduce a constraint: OrderNumber must not be duplicated inside a question, so Question becomes an Aggregate and PossibleAnswer is its owned type.
But what if we need to perform analysis on questionnaire responses. Say, we have criteria which can contain PossibleAnswerId and we need to
To make query PossibleAnswer’s text I will have to:
Note that without QuestionId in CriterionAnswer I can get PossibleAnswer’s text only by querying all the questions in database and making client-side filtering.
If there would be and opportunity to navigate to owned types, this could be much easier by just using
.PossibleAnswer.Text
on CriterionAnswer.I suggest reopening this issue.