C# 如何使用AutoMapper映射配方和配料
我有以下RecipeModel、IngreditModel和RecipePartModel类,它们代表前端用户的DTO类:C# 如何使用AutoMapper映射配方和配料,c#,entity-framework,asp.net-core,automapper,C#,Entity Framework,Asp.net Core,Automapper,我有以下RecipeModel、IngreditModel和RecipePartModel类,它们代表前端用户的DTO类: public class RecipeModel { public Guid Id { get; set; } public string Name { get; set; } public string ImageUrl { get; set; } public string Description { get; set; } pu
public class RecipeModel
{
public Guid Id { get; set; }
public string Name { get; set; }
public string ImageUrl { get; set; }
public string Description { get; set; }
public IEnumerable<RecipePartModel> RecipeParts { get; set; }
}
public class IngredientModel
{
public Guid Id { get; set; }
public string Name { get; set; }
}
public class RecipePartModel
{
public Guid Id { get; set; }
public IngredientModel Ingredient { get; set; }
public string Unit { get; set; }
public decimal Quantity { get; set; }
}
公共类RecipeModel
{
公共Guid Id{get;set;}
公共字符串名称{get;set;}
公共字符串ImageUrl{get;set;}
公共字符串说明{get;set;}
公共IEnumerable RecipeParts{get;set;}
}
公共类IngCreditModel
{
公共Guid Id{get;set;}
公共字符串名称{get;set;}
}
公共类RecipePartModel
{
公共Guid Id{get;set;}
公共IngreditModel成分{get;set;}
公共字符串单元{get;set;}
公共十进制数量{get;set;}
}
以下是我的实体类:
public class Recipe : BaseEntity
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public Guid Id { get; set; }
public string Name { get; set; }
public string ImageUrl { get; set; }
public string Description { get; set; }
public virtual IEnumerable<RecipePart> RecipeParts { get; set; }
}
public class Ingredient : BaseEntity
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public Guid Id { get; set; }
public string Name { get; set; }
public int Amount { get; set; }
public virtual IEnumerable<RecipePart> RecipeParts { get; set; }
}
public class RecipePart : BaseEntity
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public Guid Id { get; set; }
public Ingredient Ingredient { get; set; }
public Recipe Recipe { get; set; }
public string Unit { get; set; }
public decimal Quantity { get; set; }
}
公共类配方:BaseEntity
{
[数据库生成(DatabaseGeneratedOption.Identity)]
[关键]
公共Guid Id{get;set;}
公共字符串名称{get;set;}
公共字符串ImageUrl{get;set;}
公共字符串说明{get;set;}
公共虚拟IEnumerable RecipeParts{get;set;}
}
公共类成分:BaseEntity
{
[数据库生成(DatabaseGeneratedOption.Identity)]
[关键]
公共Guid Id{get;set;}
公共字符串名称{get;set;}
公共整数金额{get;set;}
公共虚拟IEnumerable RecipeParts{get;set;}
}
公共类RecipePart:BaseEntity
{
[数据库生成(DatabaseGeneratedOption.Identity)]
[关键]
公共Guid Id{get;set;}
公共成分成分{get;set;}
公共配方{get;set;}
公共字符串单元{get;set;}
公共十进制数量{get;set;}
}
我的问题是-如何使用AutoMapper将配方映射到RecipeModel?我尝试过类似的方法,但我认为这是不好的,因为它只是加入了整个数据库的所有RecipeParts,对吗
public class DomainProfile : Profile
{
public DomainProfile()
{
CreateMap<Ingredient, IngredientModel>().ReverseMap();
CreateMap<Recipe, RecipeModel>()
.ForMember(x => x.RecipeParts, opt => opt.MapFrom(src => src.RecipeParts));
}
}
公共类DomainProfile:Profile
{
公共域配置文件()
{
CreateMap().ReverseMap();
CreateMap()
.ForMember(x=>x.RecipeParts,opt=>opt.MapFrom(src=>src.RecipeParts));
}
}
这个映射没有什么不好的。事实上,您甚至不需要FormMember调用,因为这是默认约定。映射只需将实体子集合中的每个元素转换为相应的模型对象
当然,是否以有效的方式加载实体是另一回事。如果加载大量配方实体,并延迟加载每个配方实体的RecipeParts集合,则会出现一个严重的“选择N+1”问题。但是这不是AutoMapper的错。要回答您关于如何使用AutoMapper将一个类型映射到另一个类型的问题,有很多方法可以做到这一点。文档位于此处: 我编写了一个控制台应用程序,并使用您的代码以我所知道的最快方式使其工作。当我调试它并检查recipeModel内部时,它引用了一个带有单个RecipePartModel的RecipePartModels列表。在RecipePartModel内部,它引用了IngCreditModel
static void Main(string[] args)
{
var profile = new DomainProfile();
Mapper.Initialize(cfg => cfg.AddProfile(profile));
var recipe = new Recipe
{
RecipeParts = new List<RecipePart>
{
new RecipePart()
{
Ingredient = new Ingredient()
}
}
};
var recipeModel = Mapper.Map<Recipe, RecipeModel>(recipe);
Console.ReadKey();
}
static void Main(字符串[]args)
{
var profile=new DomainProfile();
初始化(cfg=>cfg.AddProfile(profile));
var配方=新配方
{
RecipeParts=新列表
{
新RecipePart()
{
配料=新配料()
}
}
};
var recipeModel=Mapper.Map(配方);
Console.ReadKey();
}
为了回答您对从数据库获取所有配方的担忧,如果您使用的是实体框架,则取决于是否启用了延迟加载。延迟加载可确保从数据库获取配方时,不会加载配方部分。仅当您稍后在程序流中直接访问配方部分时,才会加载它们。默认情况下启用延迟加载,因此这是默认行为。如果您将其关闭,则启用了“急切加载”,加载所有配方部分并依次加载它们的配料
这可能会有帮助:。的确如此!这是一个矛盾的问题,我必须写这个问题,以认识到一切都与这个映射:谢谢你非常详细的答案。事实上,我写的这个映射确实ok@GhostCat抱歉我的选择,谢谢你的提示。下次我会尽我最大的努力。我很感谢你的迅速而友好的复出!