Hi good people of Internet :)
I am trying to use EF Core 5 entities as domain entities in a sense of DDD.
I have a case of two entities, each with their own identity (meaning they are Entity type of DDD objects, not ValueObjects): Country and Currency.
Currency can belong to many Countries (for example EUR). Country, on the other hand, can have only one 'currently active' Currency, which isn't necessarily the same Currency at all times (such example would be EU country, abandoning their own national currency for the EUR).
In a specific domain bounded context, I would need only:
public class Country : Entity<Guid>
{
public string Name { get; private set; }
// the rest is omitted for readability
public Currency Currency { get; private set; }
}
and
public class Currency : Entity<Guid>
{
public string Name { get; set; }
public string Code { get; set; }
}
I don't want to have navigation property: public ICollection<Country> Countries { get; private set; }
on the Currency entity, just to be able to define 1:N relationship, because this would only pollute my domain.
I tried to add navigation property as EF shadow property, but EF doesn't allow it, with thrown exception:
The navigation 'Countries' cannot be added to the entity type 'Currency' because there is no corresponding CLR property on the underlying type and navigations properties cannot be added in shadow state.
Currency can't be owned (in a EF sense as OwnsOne) by Country, because that would mean that Currency would have to have composite {Id, IdCountry} PK in the database, which would violate the requirement of being able to assign one currency to multiple countries.
Is there a solution to establish a relationship between Currency and Country (or the other way around), which doesn't pollute the domain with navigation property and allows the same CLR object to be used as a domain and EF entity?