Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 缺少相关实体的map CreateMap异常(自动映射和代码优先)_C#_Entity Framework 6_Automapper - Fatal编程技术网

C# 缺少相关实体的map CreateMap异常(自动映射和代码优先)

C# 缺少相关实体的map CreateMap异常(自动映射和代码优先),c#,entity-framework-6,automapper,C#,Entity Framework 6,Automapper,我正在与汽车制造商的惯例作斗争。 您的dto中不首先包含代码中的相关实体是什么意思? 在汽车制造商的历史上,我已经看过了很多不同时期的例子,但到目前为止还没有一个是有效的。最有意义的是: <package id="AutoMapper" version="9.0.0" targetFramework="net48" /> [Table("Product")] public partial class Product { public Product() {

我正在与汽车制造商的惯例作斗争。 您的dto中不首先包含代码中的相关实体是什么意思? 在汽车制造商的历史上,我已经看过了很多不同时期的例子,但到目前为止还没有一个是有效的。最有意义的是:

<package id="AutoMapper" version="9.0.0" targetFramework="net48" />

[Table("Product")]
public partial class Product
{
    public Product()
    {
        ProductOptions = new HashSet<ProductOption>();
    }

    public int Id { get; set; }

    public byte ProductTypeId { get; set; }

    [StringLength(255)]
    public string ProductName { get; set; }

    public virtual ICollection<ProductOption> ProductOptions { get; set; }
}

[Table("ProductOption")]
public partial class ProductOption
{
    public int Id { get; set; }

    public int ProductId { get; set; } //Foreign key to Product.Id

    [Required]
    [StringLength(255)]
    public string OptionName { get; set; }
}


public class ProductBase
{
    public int Id { get; set; }
    public byte ProductTypeId { get; set; }
    public string ProductName { get; set; }
    public ProductOptionBase ProductOptionsBase { get; set; }
}

public class ProductOptionBase
{
    public int Id { get; set; }
    public int ProductId { get; set; } 
    public string OptionName { get; set; }
}


        var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<Product, ProductBase>().IncludeMembers(po => po.ProductOptions);
            cfg.CreateMap<ProductOption, ProductOptionBase>();
        });
        var mapper = config.CreateMapper();


        using (var _db = new DataContext())
        {
            return _db.Products.Include(po => po.ProductOptions).ProjectTo<ProductBase>(config).ToList();
        }

[表(“产品”)]
公共部分类积
{
公共产品()
{
ProductOptions=newhashset();
}
公共int Id{get;set;}
公共字节ProductTypeId{get;set;}
[StringLength(255)]
公共字符串ProductName{get;set;}
公共虚拟ICollection ProductOptions{get;set;}
}
[表(“产品选项”)]
公共部分类ProductOption
{
公共int Id{get;set;}
public int ProductId{get;set;}//Product.Id的外键
[必需]
[StringLength(255)]
公共字符串选项名称{get;set;}
}
公共类产品库
{
公共int Id{get;set;}
公共字节ProductTypeId{get;set;}
公共字符串ProductName{get;set;}
public ProductOptionBase ProductOptionBase{get;set;}
}
公共类ProductOptionBase
{
公共int Id{get;set;}
public int ProductId{get;set;}
公共字符串选项名称{get;set;}
}
var config=new-MapperConfiguration(cfg=>
{
CreateMap().IncludeMembers(po=>po.ProductOptions);
CreateMap();
});
var mapper=config.CreateMapper();
使用(var\u db=new DataContext())
{
返回_db.Products.Include(po=>po.ProductOptions).ProjectTo(config.ToList();
}
System.Collections.Generic.ICollection
1[Model.ProductOption]到DataContracts.ProductBase的映射缺失。使用CreateMap创建


在var配置行上,配置应为:

        var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<Product, ProductBase>()
             .ForMember(pob => pob.ProductOptionsBase, opts => opts.MapFrom(po => po.ProductOptions));
            cfg.CreateMap<ProductOption, ProductOptionBase>();
        });
var config=new-MapperConfiguration(cfg=>
{
cfg.CreateMap()
.ForMember(pob=>pob.productoptionbase,opts=>opts.MapFrom(po=>po.ProductOptions));
CreateMap();
});
也不需要使用.Include(po=>po.ProductOptions)