Voglio fare semplicemente un'APP .Net Core MVC APP.
Ho due modelli:
public class Farming
{
[Key]
public int Id { get; set; }
public string Localization { get; set; }
public List<Animal> Animals { get; set; } //i want to get into it
public List<Worker> Workers { get; set; } //from cshtml file
}
public class Animal
{
[Key]
public int Id { get; set; }
public Species Species { get; set; }
public DateTime BirthDate { get; set; }
public bool Sex { get; set; }
public DateTime ImpregnationDate { get; set; }
}
public class Worker
{
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Position { get; set; }
public float Salary { get; set; }
public DateTime EmploymentDate { get; set; }
public DateTime EndOfEmploymentDate { get; set; }
}
Sto configurando il mio database, funziona bene. In dbo.Animals
(anche dbo.Workers works too)
in SQL Object explorer tutto funziona bene:
Quello che voglio fare, è quello di ottenere accesso in List<Animal> Animals
e l'elenco dei Workers
da Index.cshtml
pagina genereted internamente in ASP .Net MVC core in un altro List.cshtml
file. Il controller è generato anche da Scaffolding.
Il mio Index.cshtml
(lo abbrevia un po ')
@model IEnumerable<Farm.Models.Farming>
@{
ViewData["Title"] = "Index";
}
//foreach loop here with "item"
<a asp-action="List" asp-route-id="@item.Id">@Html.DisplayFor(modelItem => item.Localization)</a>
Metodo per andare a List.cshtml
da FarmingController.cs
public async Task<IActionResult> List(int? id)
{
if (id == null)
{
return NotFound();
}
var farming = await _context.Farms.FirstOrDefaultAsync(m => m.Id == id);
if (farming == null)
{
return NotFound();
}
return View(farming);
}
Quindi, come ottenere l'accesso a Models.Farming.Animal
e Models.Farming.Worker
da importato @SolutinName.Models.Farmin
in List.cshtml
?
Edit1:
Ho cercato di ottenere l'accesso ad Animal in questo modo:
var animal = await _context.Animals.FirstOrDefaultAsync(a => a.FarmingId == id);
//i get `id` poperty from func `<IActionResult>` that i mentioned higher in this question
Ma ottengo informazioni da Visual, che Animal
non ha definizione FarmingId
(è vero, guarda il modello Animal
) ma esiste in dbo.Animals
. Sono nuovo in Core, qualche idea?
EDIT2:
Ho modificato il database in questo modulo, ora funziona meglio:
public class Farming
{
[Key]
public int Id { get; set; }
public string Localization { get; set; }
public List<Animal> Animals { get; set; }//
public List<Worker> Workers { get; set; }//
}
public class Animal
{
[Key]
public int Id { get; set; }
public DateTime BirthDate { get; set; }
public bool Sex { get; set; }
public DateTime ImpregnationDate { get; set; }
[ForeignKey("Farming")]
public int FarmingId { get; set; }
public Farming Farming { get; set; }
}
public class Worker //pracownik
{
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Position { get; set; }
public float Salary { get; set; }
public DateTime EmploymentDate { get; set; }
public DateTime EndOfEmploymentDate { get; set; }
[ForeignKey("Farming")]
public int FarmingId { get; set; }
public Farming Farming { get; set; }
}
Tutto sembra essere a posto, tuttavia cerca di includere esplicitamente gli animali
Farm theFarm = _context.Farms.Include(f => f.Animals).Where(f => f.Id == farmId);
l'oggetto theFarm conterrà tutti i campi della fattoria e anche l'elenco con gli animali. e farmId è l'id della fattoria che vuoi ottenere.
usa questo per ottenere tutte le fattorie con i loro animali:
var theFarms = _context.Farms.Include(f => f.Animals).ToList();