C# 带EF的自动映射ICollection

C# 带EF的自动映射ICollection,c#,entity-framework,.net-core,automapper,C#,Entity Framework,.net Core,Automapper,我试图将我的一个模型的嵌套ICollection映射到现有Dto,但我很难用AutoMapper正确地映射它 型号: public class Ingredient : BaseEntity<long> { [MaxLength(100)] public string Name { get; set; } [ForeignKey("Id")] public int CustomerId { get; set; } public bool IsP

我试图将我的一个模型的嵌套ICollection映射到现有Dto,但我很难用AutoMapper正确地映射它

型号:

public class Ingredient : BaseEntity<long>
{
    [MaxLength(100)]
    public string Name { get; set; }
    [ForeignKey("Id")]
    public int CustomerId { get; set; }
    public bool IsPackaging { get; set; }
    public virtual ICollection<ProductIngredient> ProductIngredient { get; set; }
    public virtual ICollection<IngredientComposition> IngredientComposition { get; set; }
}
public class IngredientDto
{
    public long Id { get; set; }
    public DateTime CretedOn { get; set; }
    public DateTime UpdatedOn { get; set; }
    public string Name { get; set; }
    public int CustomerId { get; set; }
    public int UsedCount { get; set; }
    public bool IsPackaging { get; set; }
    public IList<Composition> Ingredients { get; set; }
}

public class Composition
{
    public string Type { get; set; }
    public string Key { get; set; }
    public string Value { get; set; }
}
公共类成分:BaseEntity
{
[MaxLength(100)]
公共字符串名称{get;set;}
[外国钥匙(“Id”)]
public int CustomerId{get;set;}
公共bool IsPackaging{get;set;}
公共虚拟ICollection{get;set;}
公共虚拟ICollection InCreditComposition{get;set;}
}
收集模式:

public class IngredientComposition : BaseEntity<int>
{
    [MaxLength(20)]
    public string Type { get; set; }
    [MaxLength(200)]
    public string Key { get; set; }
    [MaxLength(200)]
    public string Value { get; set; }
}
公共类IngCreditComposition:BaseEntity
{
[MaxLength(20)]
公共字符串类型{get;set;}
[MaxLength(200)]
公共字符串密钥{get;set;}
[MaxLength(200)]
公共字符串值{get;set;}
}
Dto:

public class Ingredient : BaseEntity<long>
{
    [MaxLength(100)]
    public string Name { get; set; }
    [ForeignKey("Id")]
    public int CustomerId { get; set; }
    public bool IsPackaging { get; set; }
    public virtual ICollection<ProductIngredient> ProductIngredient { get; set; }
    public virtual ICollection<IngredientComposition> IngredientComposition { get; set; }
}
public class IngredientDto
{
    public long Id { get; set; }
    public DateTime CretedOn { get; set; }
    public DateTime UpdatedOn { get; set; }
    public string Name { get; set; }
    public int CustomerId { get; set; }
    public int UsedCount { get; set; }
    public bool IsPackaging { get; set; }
    public IList<Composition> Ingredients { get; set; }
}

public class Composition
{
    public string Type { get; set; }
    public string Key { get; set; }
    public string Value { get; set; }
}
public类ingrediendto
{
公共长Id{get;set;}
public DateTime CretedOn{get;set;}
public DateTime UpdatedOn{get;set;}
公共字符串名称{get;set;}
public int CustomerId{get;set;}
public int UsedCount{get;set;}
公共bool IsPackaging{get;set;}
公共IList元素{get;set;}
}
公课作文
{
公共字符串类型{get;set;}
公共字符串密钥{get;set;}
公共字符串值{get;set;}
}
我的地图如下所示,因为我正在努力正确设置“ForMemeber”方法:

CreateMap();
CreateMap();
任何帮助都是适当的! 谢谢

编辑:

这就是我获取数据的方式:

return await _context.Ingredients
                     .Where(i => i.CustomerId ==_userResolverService.GetCustomerId())
                     .Include(i => i.IngredientComposition)
                     .Select(i => _mapper.Map<Ingredient, IngredientDto>(i))
                     .OrderBy(i => i.Name)
                     .ToListAsync();
return wait\u context.xml
.Where(i=>i.CustomerId==\u userResolverService.GetCustomerId())
.包括(i=>i.IngCreditComposition)
.Select(i=>_mapper.Map(i))
.OrderBy(i=>i.Name)
.ToListAsync();

如果您为子DTO制作了一张地图,Automapper足够聪明,可以在不使用
FormMember的情况下将其绘制出来:

CreateMap<IngredientComposition , Composition>()
    .ReverseMap();  //Reverse map tells AM to go both ways

CreateMap<Ingredient, IngredientDto>()
    .ReverseMap();

// CreateMap<IngredientDto, Ingredient>();  ** Not needed with ReverseMap()
CreateMap()
.ReverseMap()//反向地图告诉AM双向行驶
CreateMap()
.ReverseMap();
//CreateMap();**ReverseMap()不需要

如果您为子DTO制作了一张地图,Automapper足够聪明,可以在不使用
FormMember的情况下将其绘制出来:

CreateMap<IngredientComposition , Composition>()
    .ReverseMap();  //Reverse map tells AM to go both ways

CreateMap<Ingredient, IngredientDto>()
    .ReverseMap();

// CreateMap<IngredientDto, Ingredient>();  ** Not needed with ReverseMap()
CreateMap()
.ReverseMap()//反向地图告诉AM双向行驶
CreateMap()
.ReverseMap();
//CreateMap();**ReverseMap()不需要

首先,必须添加
CreateMap()执行此操作后,必须更改Linq查询。你可以用

如果未将其设置为获取此异常:

映射程序未初始化。使用适当的配置调用Initialize。如果您试图通过容器或其他方式使用映射器实例,请确保没有任何对静态mapper.Map方法的调用,如果您使用的是ProjectTo或UseAsDataSource扩展方法,请确保传入相应的IConfigurationProvider实例

更多


更新:您的属性必须具有相同的名称。如果您将Dto属性
成分更改为
InCreditComposition
不需要使用
FormMember
首先,您必须添加
CreateMap()执行此操作后,必须更改Linq查询。你可以用

如果未将其设置为获取此异常:

映射程序未初始化。使用适当的配置调用Initialize。如果您试图通过容器或其他方式使用映射器实例,请确保没有任何对静态mapper.Map方法的调用,如果您使用的是ProjectTo或UseAsDataSource扩展方法,请确保传入相应的IConfigurationProvider实例

更多


更新:您的属性必须具有相同的名称。如果您将Dto属性
成分
更改为
InCreditComposition
不需要使用
FormMember
实际上这对我很有效:

查询:

return await _context.Ingredients.Where(i => i.CustomerId == _userResolverService.GetCustomerId())
        .Include(sx=>sx.IngredientComposition)
        .ProjectTo<IngredientDto>()
        .ToListAsync();
return await\u context.components.Where(i=>i.CustomerId==\u userResolverService.GetCustomerId())
.Include(sx=>sx.IngreditComposition)
.ProjectTo()
.ToListAsync();
地图: 首先,正如您所建议的,内部集合映射,然后是main objects mapping+ForMember,一旦内部对象被映射,它就会工作

CreateMap<IngredientComposition, Composition>().ReverseMap();
CreateMap<Ingredient, IngredientDto>().ForMember(d => d.Ingredients, opt=>opt.MapFrom(c=>c.IngredientComposition)).ReverseMap();
CreateMap().ReverseMap();
CreateMap().formMember(d=>d.Components,opt=>opt.MapFrom(c=>c.IngreditComposition)).ReverseMap();

谢谢你的帮助

事实上,这对我很有效:

查询:

return await _context.Ingredients.Where(i => i.CustomerId == _userResolverService.GetCustomerId())
        .Include(sx=>sx.IngredientComposition)
        .ProjectTo<IngredientDto>()
        .ToListAsync();
return await\u context.components.Where(i=>i.CustomerId==\u userResolverService.GetCustomerId())
.Include(sx=>sx.IngreditComposition)
.ProjectTo()
.ToListAsync();
地图: 首先,正如您所建议的,内部集合映射,然后是main objects mapping+ForMember,一旦内部对象被映射,它就会工作

CreateMap<IngredientComposition, Composition>().ReverseMap();
CreateMap<Ingredient, IngredientDto>().ForMember(d => d.Ingredients, opt=>opt.MapFrom(c=>c.IngredientComposition)).ReverseMap();
CreateMap().ReverseMap();
CreateMap().formMember(d=>d.Components,opt=>opt.MapFrom(c=>c.IngreditComposition)).ReverseMap();

谢谢你的帮助

添加
CreateMap()并重试。很遗憾,这不起作用。我仍然在IngRediendTo上获取null而不是数组,但Component已附加集合-查看编辑我获取数据的方式Add
CreateMap()并重试。很遗憾,这不起作用。在IngRediendTo上,我仍然得到null而不是数组,但Component已附加集合-查看编辑我如何获得数据。很遗憾,这不起作用。在IngRediendTo上,我仍然得到null而不是数组,但Component已附加集合-查看编辑我如何获得数据。很遗憾,这不起作用。在IngRediendTo上,我仍然得到null而不是数组,但Component已经附加了集合-看编辑我如何获取数据不需要Include,默认情况下AM获取所有内容。是的,也许@sziszu确实使用了渴望加载。奇怪的是,它仍然