I'm trying to follow DDD using EF Core and in my model I have the following:
private List<TeamPerson> _personLinks;
public IReadOnlyCollection<TeamPerson> PersonLinks => _personLinks?.ToList().AsReadOnly();
public IReadOnlyCollection<Person> Members => _personLinks?.Select(l => l.Person).ToList().AsReadOnly();
I want to encapsulate the relationship between Team and Person models. I must say right away that there is a mapping for the Person model and it works.
But if I specify property access mode:
builder.HasMany(e => e.PersonLinks)
.WithOne(e => e.Team)
.HasForeignKey(e => e.TeamId)
.Metadata.PrincipalToDependent.SetPropertyAccessMode(PropertyAccessMode.Field);
builder.HasMany(e => e.TeamLinks)
.WithOne(e => e.Person)
.HasForeignKey(e => e.PersonId)
.Metadata.PrincipalToDependent.SetPropertyAccessMode(PropertyAccessMode.Field);
And if I try to get dbContext.Teams.Include(t => t.PersonLinks).ThenInclude(t => t.Person)
, I'm getting an error: "...PersonId1 column doesn't exist...", I have this for TeamPerson model mapping:
builder.Property(e => e.PersonId).IsRequired().HasColumnName("person_id");
What is my mistake or is there any other way to reach the encapsulation for the collection here?
DDD
has nothing to do with this question, just saying... – Goddess