我正在了解工作单元,存储库和服务。现在我不知道如何更新数据库中的数据
如何根据ID更新数据?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using DataAccess;
using DataAccess.UoW;
using Model;
namespace ClassLibrary1
{
public class Service
{
private readonly Unit _uow;
public Service()
{
_uow = new Unit();
}
public IEnumerable<Mdl> GetAllLogins()
{
var logins = _uow.Register.GetAll().ToList();
if (logins.Any())
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Table_1, Mdl>();
});
IMapper mapper = config.CreateMapper();
var dest = mapper.Map<List<Table_1>, List<Mdl>>(logins);
return dest;
}
return null;
}
public Mdl UpdateId(int Id)
{
var EmpId = _uow.Register.GetByID(Id);
if(EmpId != null)
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Mdl, Table_1>();
});
IMapper mapper = config.CreateMapper();
var dest = mapper.Map<Mdl, Table_1>(EmpId); //Issue Arises here
return dest;
}
return null;
}
}
}
我收到此错误:
无法从DataAccess.Table1转换为Model.Mdl
答案 0 :(得分:0)
在映射后,我们需要在更新后将模型与表进行映射,而您需要将模型与表进行映射
只是一个小变化
了解映射的概念
public Mdl UpdateId(int Id)
{
var EmpId = _uow.Register.GetByID(Id);
if(EmpId != null)
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Mdl, Table_1>();
});
IMapper mapper = config.CreateMapper();
var dest = mapper.Map<Table_1,Mdl>(EmpId);
return dest;
}
return null;
}