Неожиданные циклические ссылки в ядре Entity Framework при получении данных с помощью .Include()C#

Место общения программистов C#
Ответить
Гость
 Неожиданные циклические ссылки в ядре Entity Framework при получении данных с помощью .Include()

Сообщение Гость »


I am working on a repository method in Entity Framework Core to retrieve all applications along with their associated application tokens. My method looks like this:

Код: Выделить всё

public async Task GetAllApplications(string? expand = null) {     List applications = await context.Applications.Include(a => a.ApplicationTokens).ToListAsync();     return mapper.Map(applications); } 
I expect the result to be a list of Application objects, each containing a list of ApplicationToken objects. Importantly, I do not expect the ApplicationToken objects to include back references to Application objects since I did not specify

Код: Выделить всё

.ThenInclude(t => t.Application)
in my query. Here are the entity definitions:

Код: Выделить всё

public class Application {     public int Id { get; set; }     public required string Name { get; set; }     public required string Description { get; set; }     public DateTime CreatedAt { get; set; }     public required int CreatedById { get; set; }     public User? CreatedBy { get; set; }     public ICollection? ApplicationTokens { get; set; }     public ICollection? FeatureGates { get; set; } } public class ApplicationToken {     public int Id { get; set; }     public required int ApplicationId { get; set; }     public required string Token { get; set; }     public required bool Enabled { get; set; }     public required DateTime CreatedAt { get; set; }     public required int CreatedById { get; set; }     public required DateTime ExpiresAt { get; set; }     public Application? Application { get; set; }     public User? CreatedBy { get; set; } } 
Before mapping to ApplicationDTO, I observed circular references between Application and ApplicationToken entities, which I aimed to avoid. My expectation was that the application tokens would be populated within each application without back references to the application itself, preventing any circular references. I have no lazy loading or virtual collections that could automatically populate these references.

What am I missing here?


Источник: https://stackoverflow.com/questions/781 ... g-data-wit
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»