1

I am learning about Unit of work, repository and service. Now I don't know how to update data in database

How to update the data based the 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; } } } 

I get this error:

Cannot convert from DataAccess.Table1 to Model.Mdl

1
  • What type _uow.Register.GetByID is returning? Is this returning Table_1 type? Also you could put the var config = new MapperConfiguration(cfg =>... inside the constructor. and the IMapper _mapper = config.CreateMapper(); should be private readonly IMapper _mapper create a instance of IMapper in the constructor you can use it in all methods of your class. Just some suggestions. Commented Jun 15, 2019 at 7:45

1 Answer 1

1

While mapping we need to map the Model with the table after updating you need map the table with the model

Just a small change

Do understand the concept of mapping

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; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.