通过ID C#APi从三个表中获取数据
我正在构建一个编辑现有配方的项目 请注意-——每个配方可以有多个成分组 (-这是我在建设这个项目中的观点)通过ID C#APi从三个表中获取数据,c#,sql,entity-framework,linq,linq-to-entities,C#,Sql,Entity Framework,Linq,Linq To Entities,我正在构建一个编辑现有配方的项目 请注意-——每个配方可以有多个成分组 (-这是我在建设这个项目中的观点) 配方表(配方ID、名称等) 每种配方的配料组链接表Chartsforenerecipe(组ID,组名称示例:(至基础,至酱汁)) 有配料表(ID和名称) 包含所有成分组的成分列表的表格InCreditInChart(每个成分的ID、它所属的成分组的ID、成分ID、数量) 有没有人知道如何找出与一个成分组相关的成分的名称? 在C#MVC中当然而不是在SQL中 我的问题是: 如何在一个函数中实
mydata db = new data()
private List<ChartsForOneRecipe> lchart;
[HttpGet]//return all ingredients in a chart by getting chartid
public IHttpActionResult GetAlltheIngredientsInChart(int ChartId)
{
var q = from ing in db.Ingredients
from ingch in db.IngredientsInChart
where (ing.ingredientID == ingch.ingredientsInChartId && ingch.chartId == ChartId)
select new
{ ing.ingredientID, ing.name };
return Ok(q);
}考虑到您首先使用的是EF6代码,并且您使用各自的代码定义了模型来设置密钥,如下所示:
public class Ingredients
{
[Key]
public int IngredientId { get; set; }
public string Name { get; set; }
//Begin Other irrelevant attributes
//...
//End irrelevant attributes
//Navigation property - THIS IS IMPORTANT
[ForeignKey("IngredientsInChartId")]
public ICollection<IngredientsInChart> IngredientsInChart { get; set; }
}
public class IngredientsInChart
{
[Key]
public int ChartId { get; set; }
public string Name { get; set; }
//Begin Other irrelevant attributes
//...
//End irrelevant attributes
//Optional, on using FLUENT-API for seeding data (Migration)
public int IngredientsInChartId { get; set; }
//Navigation property - THIS IS IMPORTANT
[ForeignKey("IngredientsInChartId")]
public Ingredients Ingredient { get; set; }
}
public void ConfigureServices(IServiceCollection services)
{
#region Database configuration
// Database configuration
services.AddDbContext<MyData>(options =>
options.UseLazyLoadingProxies()
.UseSqlServer(Configuration.GetConnectionString("MyConnectionString")));
#endregion Database configuration
}
延迟加载
您不需要加载配料(删除var-contracents=db.contracents.ToList();
和Include(ingch=>ingch.contracents)
)。但是,您可能需要在启动项目中安装和安装,如下所示:
public class Ingredients
{
[Key]
public int IngredientId { get; set; }
public string Name { get; set; }
//Begin Other irrelevant attributes
//...
//End irrelevant attributes
//Navigation property - THIS IS IMPORTANT
[ForeignKey("IngredientsInChartId")]
public ICollection<IngredientsInChart> IngredientsInChart { get; set; }
}
public class IngredientsInChart
{
[Key]
public int ChartId { get; set; }
public string Name { get; set; }
//Begin Other irrelevant attributes
//...
//End irrelevant attributes
//Optional, on using FLUENT-API for seeding data (Migration)
public int IngredientsInChartId { get; set; }
//Navigation property - THIS IS IMPORTANT
[ForeignKey("IngredientsInChartId")]
public Ingredients Ingredient { get; set; }
}
public void ConfigureServices(IServiceCollection services)
{
#region Database configuration
// Database configuration
services.AddDbContext<MyData>(options =>
options.UseLazyLoadingProxies()
.UseSqlServer(Configuration.GetConnectionString("MyConnectionString")));
#endregion Database configuration
}
public void配置服务(IServiceCollection服务)
{
#区域数据库配置
//数据库配置
services.AddDbContext(选项=>
options.UseLazyLoadingProxies()
.UseSqlServer(Configuration.GetConnectionString(“MyConnectionString”));
#端域数据库配置
}
为了进一步阅读,我强烈建议阅读:
var q = db.IngredientsInChart.Where(inc => inc.chartId == ChartId)
.Include(inc => inc.Ingredient)
.Select(inc => new {
inc.ingredientsInChartId,
inc.Ingredient.name
}).ToList();
“在C#MVC中,当然不是在SQL中”……我想你的意思是“在实体框架中”。您仍在尝试从数据库获取信息。C#linq/EF代码在表面下转换为SQL。实体框架实际上完成了这项任务。您可以在任何.NET程序中这样做,而不仅仅是在MVC中。我已经修改了你们的标签,使之更具相关性(希望这能让你们的问题摆在正确的听众面前)。查询是不返回结果还是给出异常?如果没有得到任何结果,请删除WHERE并查看得到的结果。对于测试,我会添加到结果中的ingch值,以便您可以确定根本原因。
var q = db.IngredientsInChart.Where(inc => inc.chartId == ChartId)
.Include(inc => inc.Ingredient)
.Select(inc => new {
inc.ingredientsInChartId,
inc.Ingredient.name
}).ToList();