实体未通过多对多关系传递

时间:2019-05-16 15:33:17

标签: c# asp.net entity-framework-core aspnetboilerplate

我创建了一个多对多关系EventUser,其中包含userIdEventIdUserEvent对象,以及一个isPresent布尔值,我用来管理该事件的用户状态。

我通过设置ID和对象来创建EventUser,然后将其添加到Event's EventUser列表中。但是,当我从数据库中获得Events时,EventUsers的列表仅返回UserIdEventIdIsPresent字段中的数据,{{1 }}和User字段返回Event

这些是我的课程:

用户实体

null

事件实体

public class User : AbpUser<User>
    {
        public const string DefaultPassword = "123qwe";
        public string Instrument { get; set; }
        public virtual ICollection<EventUser> EventUsers { get; set; }

        public static string CreateRandomPassword()
        {
            return Guid.NewGuid().ToString("N").Truncate(16);
        }

        public static User CreateTenantAdminUser(int tenantId, string emailAddress)
        {
            var user = new User
            {
                TenantId = tenantId,
                UserName = AdminUserName,
                Name = AdminUserName,
                Surname = AdminUserName,
                EmailAddress = emailAddress,
                Roles = new List<UserRole>(),
            };

            user.SetNormalizedNames();

            return user;
        }
    }

EventUser类

    public class Event : Entity
    {
        [Required]
        public string Name { get; set; }

        [Required]
        public string Location { get; set; }
        public string Address { get; set; }

        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }

        public bool IsConfirmed { get; set; }

        public int GroupId { get; set; }

        public virtual ICollection<EventUser> EventUsers { get; set; }

        public Event()
        {
            EventUsers = new List<EventUser>();
        }
    }

我在DbContext中创建多对多关系的OnModelCreating方法

    public class EventUser : Entity
    {
        public int EventId { get; set; }
        public Event Event { get; set; }
        public long UserId { get; set; }
        public User User { get; set; }

        public bool IsPresent { get; set; }
    }
我在其中创建和添加组的所有用户作为EventUsers的EventAppService中的

AddGroupUsersToEvent方法。

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<EventUser>().HasKey(eu => new { eu.EventId, eu.UserId }); //.isUnique()?
            modelBuilder.Entity<EventUser>().HasOne(eu => eu.Event)
                .WithMany(e => e.EventUsers)
                .HasForeignKey(eu => eu.EventId);
            modelBuilder.Entity<EventUser>().HasOne(eu => eu.User)
                .WithMany(u => u.EventUsers)
                .HasForeignKey(eu => eu.UserId);
        }
我在其中列出所有事件的EventAppService中的

ListAll方法

        public void AddAllEventUsersFromGroup(AddAllEventUsersFromGroupDto dto)
        {
            var e = _eventManager.GetEventById(dto.EventId);
            var g = _groupManager.QueryGroups().FirstOrDefault(x => x.Id == dto.GroupId);

            foreach (var user in g.Users)
            {
                var eu = new EventUser
                {
                    EventId = e.Id,
                    Event = e,
                    UserId = user.Id,
                    User = user
                };
                e.EventUsers.Add(eu);
            }
            _eventManager.Update(e);
        }

如果我在Swagger中运行ListAll方法,它将从这样的事件中返回EventUsers列表:

        public IEnumerable<EventDto> ListAll()
        {
            var result = _eventManager.QueryEvents().Select(e => new EventDto
            {
                Id = e.Id,
                Name = e.Name,
                Location = e.Location,
                Address = e.Address,
                StartTime = e.StartTime,
                EndTime = e.EndTime,
                GroupId = e.GroupId,
                EventUsers = e.EventUsers
            }).OrderBy(e => e.StartTime).ToList();
            return result;
        }

获取事件时,我是否必须在某个地方包含事件和用户对象?调试时,我发现使用Event和User对象创建EventUser很好,但是一旦获得事件,它们就会以 "eventUsers": [ { "eventId": 3, "event": null, "userId": 4, "user": null, "isPresent": true, "id": 0 } ] 的形式返回。

2 个答案:

答案 0 :(得分:0)

从存储库获取相关数据时,您需要显式加载相关数据,

Repository.GetAll()
                .Include(x => x.Event)
                .Include(x => x.User)

答案 1 :(得分:0)

亚伦的答案对我有用!我唯一要做的就是在语句之​​后调用ToList()方法,因为它期望一个ICollection而不是一个IEnumerable。