Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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#_Asp.net Core_Automapper_Entity Framework Core - Fatal编程技术网

C# 使用automapper更新具有关系的实体

C# 使用automapper更新具有关系的实体,c#,asp.net-core,automapper,entity-framework-core,C#,Asp.net Core,Automapper,Entity Framework Core,我正在使用efcore和Automapper更新我的实体。我有一个名为“Aluno”的实体,它与“Endereco”有关系。代码如下: public class Aluno : ApplicationUser{ ... public Endereco Endereco { get; set; } ... } public class AlunoViewModel : UserViewModel { public int Id { get; set; }

我正在使用efcore和Automapper更新我的实体。我有一个名为“Aluno”的实体,它与“Endereco”有关系。代码如下:

public class Aluno : ApplicationUser{

    ...
    public Endereco Endereco { get; set; }
    ...
}

public class AlunoViewModel : UserViewModel
{
    public int Id { get; set; }

    public EnderecoViewModel Endereco { get; set; }
}

public class Endereco{

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get;set; }

   ...
}

public class EnderecoViewModel {

    public int Id { get;set; }
    ...
}
自动映射配置:

config.CreateMap<Aluno, AlunoViewModel>().ReverseMap();
config.CreateMap<Endereco, EnderecoViewModel>().ReverseMap();
当我尝试更新“Aluno”实体时,我在context.SaveChanges()上遇到异常:

无法在表'Enderecos'中插入标识列的显式值 当IDENTITY_INSERT设置为OFF时

但我并不是想插入一个新的“Endereco”,只是更新实体“Aluno”,或者更新“Endereco”,它正确地加载在var Aluno中


我做错什么了吗?

请尝试以下语法:

Mapper
    .CreateMap<Aluno, AlunoViewModel>()
    .ForMember(
        dest => dest.Endereco,
        opt => opt.MapFrom(src => src.EnderecoViewModel)
    );
Mapper
.CreateMap()文件
福门博先生(
目的地=>dest.Endereco,
opt=>opt.MapFrom(src=>src.EnderecoViewModel)
);

我认为您有映射问题。保存操作之前,请检查映射是否正确。您的Aluno映射需要“FormMember”函数为其属性对象定义映射。您可以尝试添加FormMember(i=>i.Endereco,i=>i.MapFrom(s=>s.Endereco))。您是对的!你能把它贴出来作为答案吗?这样我就可以标记它了?谢谢大家!@穆尼尔:请不要滥用AutoMapper绑定回实体,这会给您带来很大的麻烦,尤其是在映射回集合时。AutoMapper现在和过去都不打算用于双向映射。请阅读AutoMapper作者在这里发布的有关AutoMapper的预期用途和良好运行的用例的帖子。尝试将其用于双向绑定会导致a)您的域模型破坏封装以支持该功能,b)您的持久性模型/ORM映射器在绑定方面存在问题。请参阅本博客文章,了解为什么将AutoMapper与ORM(ViewModel到持久性模型绑定)一起使用如果你用另一种方式(持久化模型->dto或viewmodel),它可能会工作得很好,但也可能会触发延迟加载(同步发生),从而给你带来麻烦,因此阻止你的ASP.NET请求线程返回线程池)@Tseng,我同意你的看法。我解决了上面的问题,但我现在有很多其他问题,与Automapper有关。我决定在执行插入或更新时直接将属性从ViewModel映射到实体。谢谢你的推荐,非常有帮助。对不起,我需要投否决票。虽然它可以解决眼前的问题,但您只是在鼓励OP和其他人以错误的方式使用AutoMapper。AutoMapper不打算用于域模型或持久性模型的双向绑定!我不鼓励任何人使用任何具体的方式做任何事情,只是展示了如果他们使用AutoMapper,应该如何使用它,不需要否决@Tseng@Tseng,虽然你的观点几乎正确,但我认为否决这个答案是不公平的。它解决了这个问题,而否决权不会帮助其他人。真正有帮助的是用你认为最合适的解决方案发布一个答案。
Mapper
    .CreateMap<Aluno, AlunoViewModel>()
    .ForMember(
        dest => dest.Endereco,
        opt => opt.MapFrom(src => src.EnderecoViewModel)
    );