Не уверен в подробностях взаимосвязи между вашими тремя таблицами. Вот рабочая демонстрация с отношениями Agents
и AgentQueue
один-ко-многим, с Agents
и AgentsNodes
отношениями один-ко-многим. AgentViewModel для отображения данных:
Модель:
public class Agents
{
[Key]
public int AgentId { get; set; }
public string AgentName { get; set; }
public AgentQueue AgentQueue { get; set; }
public List<AgentNodes> AgentNodes { get; set; }
}
public class AgentQueue
{
public int Id { get; set; }
public string QueueName { get; set; }
public int AgentId { get; set; }
public Agents Agents { get; set; }
}
public class AgentNodes
{
public int Id { get; set; }
public string NodeName { get; set; }
public Agents Agents { get; set; }
}
public class AgentViewModel
{
public int AgentId { get; set; }
public string AgentName { get; set; }
public string QueueName { get; set; }
public List<string> NodeName { get; set; }
}
Просмотр:
@model IEnumerable<AgentViewModel>
<h1>Index</h1>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.AgentId)
</th>
<th>
@Html.DisplayNameFor(model => model.AgentName)
</th>
<th>
@Html.DisplayNameFor(model => model.NodeName)
</th>
<th>
@Html.DisplayNameFor(model => model.QueueName)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.AgentId)
</td>
<td>
@Html.DisplayFor(modelItem => item.AgentName)
</td>
<td>
@foreach (var node in item.NodeName)
{
@Html.DisplayFor(modelItem => node)
<br />
}
</td>
<td>
@Html.DisplayFor(model => item.QueueName)
</td>
</tr>
}
</tbody>
</table>
Контроллер:
public class AgentsController : Controller
{
private readonly MvcProjContext _context;
public AgentsController(MvcProjContext context)
{
_context = context;
}
// GET: Agents
public async Task<IActionResult> Index()
{
var model = await _context.Agents.Include(a => a.AgentQueue).Include(a => a.AgentNodes)
.Select(a => new AgentViewModel()
{
AgentId=a.AgentId,
AgentName = a.AgentName,
NodeName = a.AgentNodes.Select(an=>an.NodeName).ToList(),
QueueName = a.AgentQueue.QueueName
}).ToListAsync();
return View(model);
}
}
DbContext:
public class MvcProjContext : DbContext
{
public MvcProjContext (DbContextOptions<MvcProjContext> options)
: base(options)
{
}
public DbSet<Agents> Agents { get; set; }
public DbSet<AgentNodes> AgentNodes { get; set; }
public DbSet<AgentQueue> AgentQueue { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//configure one-to-one relationship
modelBuilder.Entity<Agents>()
.HasOne(a => a.AgentQueue)
.WithOne(b => b.Agents)
.HasForeignKey<AgentQueue>(b => b.AgentId);
}
}
Результат: