Код: Выделить всё
Agent
Код: Выделить всё
AgentPatient
У меня возникли проблемы с добавлением пациентов к агенту. Потому что, когда я добавляю Patient в список пациентов внутри моей модели Agent, я хочу, чтобы он автоматически обновлял мою таблицу AgentPatient с учетом новых связей между созданными сущностями. Поэтому, когда я извлекаю агента из базы данных, я вижу пациентов внутри свойства списка пациентов.
Я делаю неправильный подход? Вместо того, чтобы пытаться добавить что-то к модели Agent, следует ли мне напрямую изменять таблицу AgentPatient? Нужен ли мне определенный DbSet для моей таблицы соединения, созданной EF Core?
Вот мои модели:
Код: Выделить всё
Patient
Код: Выделить всё
public class Patient
{
public Guid PatientID { get; set; }
// Some other properties not important to the question
public DateTime? CreatedAt { get; set; } = null;
public DateTime? UpdatedAt { get; set; } = null;
public DateTime? DeletedAt { get; set; } = null;
public List? Agents { get; set; } = [];
}
Код: Выделить всё
Agent
Код: Выделить всё
public class AgentModel
{
public Guid AgentId { get; set; }
public DateTime? CreatedAt { get; set; } = null;
public DateTime? UpdatedAt { get; set; } = null;
// Navigation properties
public List
? Patients { get; set; } = [];
public DateTime DeletedAt { get; set; }
}
Код: Выделить всё
{
public void Configure(EntityTypeBuilder builder)
{
// Define table name (optional, defaults to class name)
builder.ToTable("Agents");
// Define primary key
builder.HasKey(a => a.AgentId);
// Configure relationships
builder.HasMany(a => a.Patients)
.WithMany(p => p.Agents);
}
}
Код: Выделить всё
Patient
Код: Выделить всё
public class PatientEntityTypeConfiguration : IEntityTypeConfiguration
{
public void Configure(EntityTypeBuilder builder)
{
builder.ToTable("Patients");
// Primary Key
builder.HasKey(p => p.PatientID);
builder.HasMany(p => p.Agents)
.WithMany(a => a.Patients);
}
}
Подробнее здесь: https://stackoverflow.com/questions/789 ... ework-core