C#Automapper 6.1.1 - 收集/列出目的地属性丢失值实体框架6 [英] C# Automapper 6.1.1 - Collection/List destination properties loses values Entity Framework 6

查看:549
本文介绍了C#Automapper 6.1.1 - 收集/列出目的地属性丢失值实体框架6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Automapper 6和I映射集合时,我遇到问题,找不到解决方案。在下面的 updatedArticle 对象中,我有旧的创建更新值,因为它们不存在于视图模型上。但是,描述中创建的更新的值将丢失。通过视图模型进入的值都会正确更新。我究竟做错了什么? Automapper也会丢失Entity Framework 6映射文章,因为这些值也会丢失。



控制器方法:

  public async任务< IHttpActionResult> UpdateArticle(ArticleViewModel articleVm)
{
文章articleOriginal = await iArticleRepository.GetAsync(articleVm.Id);
文章updatedArticle = Mapper.Map< ArticleViewModel,Article>(articleVm,articleOriginal);
return Ok();
}

映射:

  Mapper.Initialize(cfg => 
{
cfg.CreateMap< ArticleViewModel,Article>()
.ForMember(x =>已创建,opt => opt.Ignore())
.ForMember(x => x.Updated,opt => opt.Ignore());

cfg.CreateMap< DescriptionViewModel,Description>()
.ForMember(x => x.Created,opt => opt.Ignore())
.ForMember(x => x.Updated,opt => opt.Ignore())
.ForMember(x => x.ArticleId,opt => opt.Ignore())
.ForMember(x => x.Article,opt => opt.Ignore());
}
Mapper.AssertConfigurationIsValid();

查看模式:

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

public string Name {get; set;}

public List< DescriptionViewModel>描述{get; set;}
}

public c lass DescriptionViewModel
{
public int Id {get;组; }

public string标题{get;组;
}

模型:

  public class文章:IEntity< int> 
{
public Article()
{
描述=新列表<描述&();
}

[Key]
public int Id {get;组; }

public DateTime Created {get;组; }

public DateTime更新{get;组; }

[MaxLength(256)]
public string Name {get;组; }

public virtual ICollection< Description>描述{get;组; }
}

public class说明:IEntity< int>
{
[Key]
public int Id {get;组; }

public DateTime Created {get;组; }

public DateTime更新{get;组;

[MaxLength(256)]
public string标题{get;组; }

public int ArticleId {get;组;

public virtual Article Article {get;组; }
}


解决方案

天无奈。从 https://github.com/AutoMapper/AutoMapper.Collection 安装AutoMapper.Collection

  PM> Install-Package AutoMapper.Collection 
PM> Install-Package AutoMapper.Collection.EntityFramework

我不得不更改的是 cfg .AddCollectionMappers(); EqualityComparison 。示例:

  Mapper.Initialize(cfg => 
{
cfg.AddCollectionMappers(); $ ($)
$ b cfg.SetGeneratePropertyMaps ,dst)=> src.Id == dst.Id);

cfg.CreateMap< DescriptionViewModel,Description>(MemberList.Source)
.EqualityComparison((src,dst)= > src.Id == dst.Id);
}
Mapper.AssertConfigurationIsValid();


I have a problem when mapping collections with Automapper 6 and I can't find a solution. In the updatedArticle object below I have the old Created and Updated values left after mapping since they do not exist on the view model. However the values for Created and Updated in Descriptions are lost. The values that do come in via the view model are all updated correctly. What am I doing wrong? Automapper also loses Entity Framework 6 mapping for Article since those values are lost as well.

Controller method:

public async Task<IHttpActionResult> UpdateArticle(ArticleViewModel articleVm)
{
    Article articleOriginal = await iArticleRepository.GetAsync(articleVm.Id);
    Article updatedArticle = Mapper.Map<ArticleViewModel, Article>(articleVm, articleOriginal);
    return Ok();
}

Mapping:

Mapper.Initialize(cfg =>
{
    cfg.CreateMap<ArticleViewModel, Article>()
        .ForMember(x => x.Created, opt => opt.Ignore())
        .ForMember(x => x.Updated, opt => opt.Ignore());

    cfg.CreateMap<DescriptionViewModel, Description>()
        .ForMember(x => x.Created, opt => opt.Ignore())
        .ForMember(x => x.Updated, opt => opt.Ignore())
        .ForMember(x => x.ArticleId, opt => opt.Ignore())
        .ForMember(x => x.Article, opt => opt.Ignore());
}
Mapper.AssertConfigurationIsValid();

Viewmodels:

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

    public string Name { get; set; }

    public List<DescriptionViewModel> Descriptions { get; set; }
}

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

    public string Heading { get; set; }
}

Models:

public class Article : IEntity<int>
{
    public Article()
    {
        Descriptions = new List<Description>();
    }

    [Key]
    public int Id { get; set; }

    public DateTime Created { get; set; }

    public DateTime Updated { get; set; }

    [MaxLength(256)]
    public string Name { get; set; }

    public virtual ICollection<Description> Descriptions { get; set; }
}

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

    public DateTime Created { get; set; }

    public DateTime Updated { get; set; }

    [MaxLength(256)]
    public string Heading { get; set; }

    public int ArticleId { get; set; }

    public virtual Article Article { get; set; }
}

解决方案

Finally solved it after two days frustration. Installed AutoMapper.Collection from https://github.com/AutoMapper/AutoMapper.Collection

PM> Install-Package AutoMapper.Collection
PM> Install-Package AutoMapper.Collection.EntityFramework

All I had to change was cfg.AddCollectionMappers(); and EqualityComparison. Example:

Mapper.Initialize(cfg =>
{
    cfg.AddCollectionMappers();

    cfg.SetGeneratePropertyMaps<GenerateEntityFrameworkPrimaryKeyPropertyMaps<DbContext>>();

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

    cfg.CreateMap<DescriptionViewModel, Description>(MemberList.Source)
        .EqualityComparison((src, dst) => src.Id == dst.Id);
}
Mapper.AssertConfigurationIsValid();

这篇关于C#Automapper 6.1.1 - 收集/列出目的地属性丢失值实体框架6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆