Код: Выделить всё
public record ProfileId(string Value);
public record AccountId(string Value);
public record UserId(string Value);
public class Profile
{
private Profile() { }
private Profile(ProfileId id, UserId userId, Account account)
{
Id = id;
UserId = userId;
Account = account;
}
public ProfileId Id { get; private set; }
public UserId UserId { get; private set; }
public Account Account { get; private set; }
}
public class Account
{
private Account() { }
private Account(AccountId id, string name)
{
Id = id;
Name = name;
}
public AccountId Id { get; private set; }
public string Name { get; private set; }
private readonly List
_profiles = new();
public IReadOnlyCollection Profiles =>_profiles.AsReadOnly();
}
Код: Выделить всё
builder.ToTable("profiles");
builder.HasOne(profile => profile.Account)
.WithMany(account => account.Members)
.HasForeignKey("account_id")
.IsRequired();
builder.Property(profile => profile.UserId)
.HasColumnName("user_id")
.IsRequired();
builder.HasKey("user_id", "account_id");
Код: Выделить всё
builder.ToTable("accounts");
builder.HasKey(account => account.Id);
Невозможно создать DbContext типа AppDbContext. Исключение «Свойство user_id» невозможно добавить к типу «Профиль», поскольку тип свойства не указан и нет соответствующего свойства или поля CLR. Чтобы добавить свойство теневого состояния, необходимо указать тип свойства». был выброшен при попытке создать экземпляр.
Я пробовал несколько вещей, но все они имеют странные побочные эффекты, такие как создание дополнительных теневых свойств. с дискриминаторами в именах, например:

.. . когда вы пытаетесь сопоставить свойство UserId следующим образом (в отличие от приведенного выше):
Код: Выделить всё
builder.Property("user_id")
.HasColumnName("user_id")
.IsRequired();
Подробнее здесь: https://stackoverflow.com/questions/783 ... h-relation