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.