【发布时间】:2021-11-17 21:15:34
【问题描述】:
我正在学习 C# & .NET 和 EF(使用 aspnetboilerplate),我想出了创建一些虚拟项目的想法,以便我可以练习。但是过去 4 小时我遇到了这个错误,希望这里有人可以帮助我。
我创建的(至少我认为我正确地创建了它)是 2 个名为“Ingredient”和“Master”的类
我想用它来分类“大师”类的成分。
例如像这样的成分
- 鸡胸肉
- 鸡腿
它们都属于 Meat(女巫在“Master”数据库中输入),这是我的代码
成分.cs
public class Ingrident : Entity
{
public string Name { get; set; }
public Master Master { get; set; }
public int MasterId { get; set; }
}
Master.cs
public class Master : Entity
{
public string Name { get; set; }
public List<Ingrident> Ingridents { get; set; } = new();
}
IngridientAppService.cs
public List<IngridientDto> GetIngWithParent()
{
var result = _ingRepository.GetAllIncluding(x => x.Master);
//Also I try this but doesn`t work
// var result = _ingRepository.GetAll().Where(x => x.MasterId == x.Master.Id);
return ObjectMapper.Map<List<IngridientDto>>(result);
}
IngridientDto.cs
[AutoMap(typeof(IndexIngrident.Entities.Ingrident))]
public class IngridientDto : EntityDto
{
public string Name { get; set; }
public List<MasterDto> Master { get; set; }
public int MasterId { get; set; }
}
MasterDto.cs
[AutoMap(typeof(IndexIngrident.Entities.Master))]
public class MasterDto : EntityDto
{
public string Name { get; set; }
}
当我创建(最后一次练习)M -> M 关系时,这种方法与 .getAllIncluding 工作但现在当我有一个 -> 很多它不会工作。
希望有人能够帮助我,或者至少给我一些好的提示。
祝你有美好的一天!
【问题讨论】:
-
在 GitHub 上创建一个从 aspnetboilerplate/module-zero-core-template 派生的 repro 项目。
标签: c# asp.net entity-framework entity-framework-core aspnetboilerplate