最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - ASP.NET Core 6 .Include not working on getAll method - Stack Overflow

programmeradmin0浏览0评论

There is no relationships on database but we created rule on API like this:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<Authority>()
        .HasOne(a => a.user)
        .WithMany(u => u.authorities)
        .HasPrincipalKey(u => u.ProfileId)
        .HasForeignKey(a => a.ProfileID)
        .OnDelete(DeleteBehavior.NoAction)
        .IsRequired();

    modelBuilder.Entity<Department>()
        .HasOne(d => d.user)
        .WithOne(u => u.department)        
        .HasForeignKey<User>(u => u.DepartmentID)
        .OnDelete(DeleteBehavior.NoAction)
        .IsRequired();

    modelBuilder.Entity<Team>()
        .HasOne(a => a.user)
        .WithMany(u => u.teams)
        .HasPrincipalKey(u => u.TeamID)
        .HasForeignKey(a => a.ID)
        .OnDelete(DeleteBehavior.NoAction)
        .IsRequired();

    modelBuilder.Entity<Group>()
        .HasOne(a => a.user)
        .WithMany(u => u.groups)
        .HasPrincipalKey(u => u.GroupID)
        .HasForeignKey(a => a.ID)
        .OnDelete(DeleteBehavior.NoAction)
        .IsRequired();
}

GetById returns all includes. However GetAll doesn't seem to noor any of the includes. Some models have include, some models haven't:

public async Task<List<User>> GetAllUsers()
{
    var users = await _context.Users
            .Include(u => u.authorities)
            .Include(u => u.department)
            .Include(u => u.teams)
            .Include(u => u.groups).Take(5)
            .ToListAsync();

    return users;
}

I'm using ASP.NET Core 6 and EF Core 7.

I want to get to respect all .Include calls in the GetAll method.

发布评论

评论列表(0)

  1. 暂无评论