Linq to sql, copy original entity to new one and save

前端 未结 6 1559
失恋的感觉
失恋的感觉 2020-12-16 10:51

I have a situation when I cant just update original record in database, but instead make a new record, copy all fields from old and apply changes to new one. (something like

6条回答
  •  暖寄归人
    2020-12-16 11:43

    Is it possible to make it work without manually copying every field?

    Yes - don't manually copy every field:

    You could use AutoMapper.

    Set up somewhere (called once at program start):

    AutoMapper.Mapper.CreateMap()
    // don't map the id to stop conflicts when adding into db
        .ForMember(a => a.Id, a => a.Ignore()); 
    

    Then call:

    var newObject = AutoMapper.Mapper.Map(oldObject);
    

提交回复
热议问题