Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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#AutoMapper-可用于列表,但不能用于单个可为空的实例_C#_Entity Framework_Automapper_Automapper 6 - Fatal编程技术网

带有实体框架的C#AutoMapper-可用于列表,但不能用于单个可为空的实例

带有实体框架的C#AutoMapper-可用于列表,但不能用于单个可为空的实例,c#,entity-framework,automapper,automapper-6,C#,Entity Framework,Automapper,Automapper 6,我犯了一个非常严重的错误,我无法理解。我将AutoMapper 6与AutoMapper.Collection和AutoMapper.Collection.EntityFramework一起使用 从下面的屏幕截图可以看出,除了updatedContact为空的图像外,每个组件都会更新。但是,如果我只为updateImage执行显式映射,它就会工作。它还可以毫无问题地更新图像集合。有人经历过吗?其他单一属性也可以工作,但由于某种原因,Image会造成麻烦 //Works var updatedA

我犯了一个非常严重的错误,我无法理解。我将AutoMapper 6与
AutoMapper.Collection
AutoMapper.Collection.EntityFramework
一起使用

从下面的屏幕截图可以看出,除了
updatedContact
为空的图像外,每个组件都会更新。但是,如果我只为
updateImage
执行显式映射,它就会工作。它还可以毫无问题地更新图像集合。有人经历过吗?其他单一属性也可以工作,但由于某种原因,
Image
会造成麻烦

//Works
var updatedArticle = Mapper.Map<ArticleViewModel, Article>(articleVm, articleOriginal);
//Every component is updated a part from Image.
var updatedContact = Mapper.Map<ContactViewModel, Contact>(contactVm, contactOriginal);
//Works
var updatedImage = Mapper.Map<ImageViewModel, Image>(contactVm.Image);
//Works
var newContact = Mapper.Map<ContactViewModel, Contact>(contactVm);
//有效
var updatedArticle=Mapper.Map

映射:

cfg.CreateMap<ArticleViewModel, Article>(MemberList.Source)
    .EqualityComparison((src, dst) => src.Id == dst.Id);

cfg.CreateMap<ImageViewModel, Image>(MemberList.Source)
    .EqualityComparison((src, dst) => src.Id == dst.Id)
    .ForSourceMember(x => x.IsDeleted, opt => opt.Ignore())
    .ForMember(dest => dest.ImageBytes, opt => opt.MapFrom(src => Encoding.ASCII.GetBytes(src.Image)));

cfg.CreateMap<ContactViewModel, Contact>(MemberList.Source)
.EqualityComparison((src, dst) => src.Id == dst.Id)
.ForSourceMember(x => x.IsDeleted, opt => opt.Ignore())
.ForSourceMember(x => x.FullName, opt => opt.Ignore());
cfg.CreateMap(MemberList.Source)
.EqualityComparison((src,dst)=>src.Id==dst.Id);
CreateMap(MemberList.Source)
.EqualityComparison((src,dst)=>src.Id==dst.Id)
.ForSourceMember(x=>x.IsDeleted,opt=>opt.Ignore())
.ForMember(dest=>dest.ImageBytes,opt=>opt.MapFrom(src=>Encoding.ASCII.GetBytes(src.Image));
CreateMap(MemberList.Source)
.EqualityComparison((src,dst)=>src.Id==dst.Id)
.ForSourceMember(x=>x.IsDeleted,opt=>opt.Ignore())
.ForSourceMember(x=>x.FullName,opt=>opt.Ignore());
档案:

public class ArticleViewModel
{
    public int Id { get; set; }
    ... 
    public List<ImageViewModel> Images { get; set; }
}

public class Article : IEntity<int>
{
    public int Id { get; set; }
    ...
    public virtual ICollection<Image> Images { get; set; }  
}

public class ContactViewModel
{
    public int Id { get; set; }
    ... 
    public ImageViewModel Image { get; set; }
}

public class Contact: IEntity<int>
{
    [Key]
    public int Id { get; set; }

    ...

    public int? ImageId { get; set; }

    public Image Image { get; set; }

}

public class ImageViewModel
{
    public int Id { get; set; }

    public string Image { get; set; }

    public string ImageType { get; set; }

    public bool IsDeleted { get; set; }
}

public class Image : IEntity<int>
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public DateTime Created { get; set; }

    public DateTime Updated { get; set; }

    public byte[] ImageBytes { get; set; }

    public string ImageType { get; set; }

    public int? ArticleId { get; set; }

    public virtual Article Article { get; set; }
}
公共类ArticleViewModel
{
公共int Id{get;set;}
... 
公共列表图像{get;set;}
}
公共类文章:科学性
{
公共int Id{get;set;}
...
公共虚拟ICollection映像{get;set;}
}
公共类ContactViewModel
{
公共int Id{get;set;}
... 
公共图像视图模型图像{get;set;}
}
公共类联系人:IEntity
{
[关键]
公共int Id{get;set;}
...
public int?ImageId{get;set;}
公共映像映像{get;set;}
}
公共类ImageViewModel
{
公共int Id{get;set;}
公共字符串图像{get;set;}
公共字符串ImageType{get;set;}
公共布尔被删除{get;set;}
}
公众阶级形象:理性
{
[Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
公共int Id{get;set;}
已创建公共日期时间{get;set;}
公共日期时间已更新{get;set;}
公共字节[]图像字节{get;set;}
公共字符串ImageType{get;set;}
公共int?ArticleId{get;set;}
公共虚拟项目{get;set;}
}

我认为您需要在配置中告诉您的联系人映射器显式地使用映像vm的映射。在我从内存中执行此操作时,可能会有一个或两个输入错误,但应该类似于:

.ForMember(x => x.Image, opt => opt.MapFrom(contact => Mapper.Map<ImageViewModel, Image>(contact.ImageVm);))
.ForMember(x=>x.Image,opt=>opt.MapFrom(contact=>Mapper.Map(contact.ImageVm);)

最终解决了这个问题,我忘了在
联系人
中将
图像
标记为
虚拟
。做完这件事以后,一切都开箱即用

public virtual Image Image { get; set; }

“其他单一属性也可以工作,但出于某种原因,图像会造成麻烦。”——但您在代码片段中说映射
Image
可以工作,而
Contact
不能工作。你能提供一个实际显示问题的例子吗?@poke的公式不正确,它应该是
每个组件都从图像中更新了一部分,正如你在屏幕截图中看到的那样。我没有看到任何证据表明任何嵌套的复杂属性都得到了实际映射<
updateImage
中的code>Article
也为空。
Image
类可由
Article
联系人使用。由于这是一个
联系人
文章
应该为空
Article
有一组图像,而
Contact
只有一组图像。@如果我在
Contact
中将
Image
标记为
virtual
,谢谢你的帮助,一切都开始工作了。建议您不需要显式指定。我不必在其他任何地方进行此映射。我想这与我正在更新当前对象有关,在当前对象上,
图像
为空。我做了一个新的检查并创建了一个新对象,然后创建了
图像<代码>var newContact=Mapper.Map(contactVm)@pokeFair足够了。如果图像为空,那么这就解释了它-它必须从什么映射?如果我理解正确,请确保在进行映射之前加载图像?@AdamDiament
image
contactVm
中有值,但在我的上述示例中,contactOriginal
的值为
null
。我不想让
updatedContact
拥有
contactVm
中的值。其他每个属性都从
Image
@AdamDiament更新了一部分。感谢您的帮助,如果我在
联系人中将
Image
标记为
virtual
,一切都开始工作了。