一对多关系无法获取与会者列表

One to many relation cannot get the Attendees List

我实际上使用的是 abp 4.4.0,你好,我正在尝试重新编码 https://community.abp.io/articles/creating-an-event-organizer-application-with-the-blazor-ui-wbe0sf2z 但对于 EfCore,这里是表格的代码:

    public class Event : FullAuditedAggregateRoot<Guid>
    {
        [Required] 
        public string Title { get; set; }
        
        public string Description { get; set; }

        public DateTime StartTime { get; set; }
        
        public bool IsFree { get; set; }

        public ICollection<EventAttendee> Attendees { get; set; }

        public Event()
        {
            Attendees = new Collection<EventAttendee>();
        }

    }

    public class EventAttendee : Entity<int>
    {
        public Event Event { get; set; }
        public Guid EventId { get; set; }
        public Guid AttendeeId { get; set; }
    }

这里是 DbContextModelCreatingExtensions:

builder.Entity<Event>(t =>
            {
                t.ToTable("Events");
                t.ConfigureByConvention();
                t.HasMany(x => x.Attendees)
                    .WithOne(x => x.Event)
                    .HasForeignKey(x => x.EventId)
                    .IsRequired(false);
            });
            
            builder.Entity<EventAttendee>(t =>
            {
                t.ToTable("Attendees");
                t.ConfigureByConvention();
            });

DBContext 引用

        public DbSet<Event> Events { get; set; }
        public DbSet<EventAttendee> Attendees { get; set; }

和种子

        public async Task SeedAsync(DataSeedContext context)
        {
            await _eventRepository.InsertAsync(new Event()
                {
                    Title = "First Event", 
                    Description = "This is a test", 
                    IsFree = true,
                    StartTime = DateTime.Now.AddDays(2),
                    Attendees = new List<EventAttendee>()
                    {
                        new EventAttendee(){AttendeeId = Guid.NewGuid()},
                        new EventAttendee(){AttendeeId = Guid.NewGuid()},
                        new EventAttendee(){AttendeeId = Guid.NewGuid()}
                    }
                });
        }

和 EventAppService

    public class EventAppService : ManagerAppService, IEventAppService
    {
        private readonly IRepository<Event, Guid> _eventRepository;
        private readonly IRepository<IdentityUser> _userRepository;

        public EventAppService(IRepository<Event, Guid> eventRepository, IRepository<IdentityUser> userRepository)
        {
            _eventRepository = eventRepository;
            _userRepository = userRepository;
        }

        public async Task<EventDetailDto> GetAsync(Guid id)
        {
            var @event = await _eventRepository.GetAsync(id);
            var attendeeIds = @event.Attendees.Select(a => a.AttendeeId).ToList();

            var queryable = await _userRepository.GetQueryableAsync();
            var query = queryable
                .Where(u => attendeeIds.Contains(u.Id));

            var attendees = (await AsyncExecuter.ToListAsync(query))
                .ToDictionary(x => x.Id);

            var result = ObjectMapper.Map<Event.Event, EventDetailDto>(@event);

            foreach (var attendeeDto in result.Attendees)
            {
                attendeeDto.UserName = attendees[attendeeDto.UserId].UserName;
            }

            return result;
        }

但是我有一个问题,当我执行 DBMigrator 时,种子创建正确,但是当我想获得我的活动时,与会者列表是空的

{
  "title": "First Event",
  "description": "This is a test",
  "isFree": true,
  "startTime": "2021-09-23T07:48:34.663988",
  "attendees": [],
  "creationTime": "2021-09-21T07:48:35.656599",
  "creatorId": null,
  "id": "39ff1912-edee-0d2a-9aca-00a2ff5ed128"
}

而且我不明白为什么他不能让与会者回来,如果我忘记了什么?

提前致谢

对于关系数据库(EF Core),定义DefaultWithDetailsFunc:

Configure<AbpEntityOptions>(options =>
{
    options.Entity<Event>(eventOptions =>
    {
        eventOptions.DefaultWithDetailsFunc = query => query.Include(e => e.Attendees);
    });
});

或者,显式加载集合:

var @event = await _eventRepository.GetAsync(id);
await _eventRepository.EnsureCollectionLoadedAsync(@event, e => e.Attendees); // Add this

var attendeeIds = @event.Attendees.Select(a => a.AttendeeId).ToList();

参考:https://docs.abp.io/en/abp/4.4/Entity-Framework-Core