У меня есть ApplicationUser
и Group
:
public class ApplicationUser : IdentityUser
{
public int? AdminInGroupId { get; set; }
public int? UserInGroupId { get; set; }
public Group AdminInGroup { get; set; }
public Group UserInGroup { get; set; }
// some more properties
}
public class Group
{
public int Id { get; set; }
public string Name { get; set; }
public string GroupAdminId { get; set; }
public ApplicationUser GroupAdmin { get; set; }
public List<ApplicationUser> GroupUsers { get; set; }
}
Это моя OnModelCreating()
конфигурация для Group
(у меня ее нет для ApplicationUser
):
modelBuilder.Entity<Group>()
.HasOne(a => a.GroupAdmin)
.WithOne(g => g.AdminInGroup)
.HasForeignKey<ApplicationUser>(a => a.AdminInGroupId);
modelBuilder.Entity<Group>()
.HasMany(u => u.GroupUsers)
.WithOne(g => g.UserInGroup)
.OnDelete(DeleteBehavior.NoAction);
При выполнении запроса ниже я не получаю GroupAdmin
:
Group group = await db.Groups
.Include(a => a.GroupAdmin)
.Include(u => u.GroupUsers)
.Where(m => m.Id == id)
.FirstOrDefaultAsync();
![enter image description here](https://i.stack.imgur.com/J1Jqw.png)
Обратите внимание, как GroupAdmin
равно null
, а GroupAdminId
имеет значение.