C# 在ASP.Net核心MVC2.1的模型中获取对列表的访问
我想做一个简单的ASP.Net核心MVC应用程序 我有两种型号:C# 在ASP.Net核心MVC2.1的模型中获取对列表的访问,c#,asp.net,razor,entity-framework-core,C#,Asp.net,Razor,Entity Framework Core,我想做一个简单的ASP.Net核心MVC应用程序 我有两种型号: 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 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; }
}
从FarmingController.cs转到List.cshtml的方法
但我从视觉上得到的信息是,动物没有定义FarmingId,这是真的,看看动物模型,但它存在于dbo.Animals中。我是新来的,有什么想法吗
编辑2:
我将数据库编辑为此表单,现在效果更好:
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; }
}
一切似乎都很好,但尽量明确地包括动物
Farm theFarm = _context.Farms.Include(f => f.Animals).Where(f => f.Id == farmId);
农场的对象将包含农场的所有字段以及动物列表。farmId是您想要获取的农场的id
使用此选项获取所有农场及其动物:
var theFarms = _context.Farms.Include(f => f.Animals).ToList();
嗨,谢谢你的回答。我是ASP新手,所以我需要一个更详细的答案:D我试图做的:var farming=\u context.Farms.Includef=>f.anists.Wheref=>f.Id==farmId;但是它没有在FarmingController.cs中找到farmId。我编辑了我的答案,因为我的问题中没有详细说明try id而不是farmId。它是传递给函数的值。尽量使用更具描述性的名字,这会让你的生活更轻松
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; }
}
Farm theFarm = _context.Farms.Include(f => f.Animals).Where(f => f.Id == farmId);
var theFarms = _context.Farms.Include(f => f.Animals).ToList();