【发布时间】:2019-04-06 06:05:33
【问题描述】:
在我的 Entity Framework Core 2.2 API 项目中,我构建了上下文。因为它代表我的数据库中的数据,所以我得到类似的条目 -
entity.Property(e => e.Active)
.IsRequired()
.HasDefaultValueSql("((1))");
其中Active 是必需的,默认值为 1。
我也在使用 AutoMapper 映射实体。
我的 MappingEntity 类 -
public class MappingEntity : Profile
{
public MappingEntity()
{
CreateMap<MemberUpdate, Member>();
}
}
这会将 ViewModel MemberUpdate 映射到我的数据库对象 Member
我的控制器中似乎丢失了默认值 -
public ActionResult Account(int id, [FromBody]MemberUpdate updatedMember)
{
Member member = _memberRepository.GetById(id);
//HERE MY MEMBER OBJECT IS CORRECT AND `ACTIVE` HAS IT'S DEFAULT VALUE
member = _mapper.Map<Member>(updatedMember);
//HERE MY MEMBER OBJECT IS INCORRECT AND THE ACTIVE FIELD DOES NOT SHOW THE DEFAULT VALUE
//proceed to save back to the database
}
是否有 AutoMapper 设置来保留默认值?
【问题讨论】:
标签: c# automapper .net-core-2.2