public class VillageDto
{
public int id { get; set; }
public string Name { get; set; }
public string HindiName { get; set; }
public int CentreId { get; set; }
}
public class CentreDto
{
public int id { get; set; }
public string Name { get; set; }
public string HindiName { get; set; }
}
public class BankDto
{
public int id { get; set; }
public string BankName { get; set; }
public String BankShortName { get; set; }
}
public class GrowerDto
{
public int Id { get; set; }
public int VillageId { get; set; }
public VillageDto village { get; set; }
public string Name { get; set; }
public string FatherName { get; set; }
public string HindiName { get; set; }
public string HindiFatherName { get; set; }
public string AccountNo { get; set; }
public int CentreId { get; set; }
public CentreDto centre { get; set; }
public int BankId { get; set; }
public BankDto bank { get; set; }
}
public MappingProfile()
{
Mapper.CreateMap<Village, VillageDto>();
Mapper.CreateMap<VillageDto, Village>();
Mapper.CreateMap<Grower, GrowerDto>();
Mapper.CreateMap<GrowerDto, Grower>();
Mapper.CreateMap<Bank, BankDto>();
Mapper.CreateMap<BankDto, Bank>();
}
public class GrowerController : ApiController
{
private ApplicationDbContext _context;
public GrowerController()
{
_context = new ApplicationDbContext();
}
public IHttpActionResult GetAllGrowers(int? pagenumber, int? pagesize)
{
var allgrowers = _context.growers.Include(v => v.village).ToList().Select(Mapper.Map<Grower, GrowerDto>).OrderBy(g => g.Id);
var currentPageNumber = pagenumber ?? 1;
var currentPageSize = pagesize ?? 5;
return Ok(allgrowers.Skip((currentPageNumber - 1) * currentPageSize).Take(currentPageSize));
}
}
1.primary key for grower is (id,villageid)
2.villageid is also foreign key and also primary key for village model
3.when api called then api returns the same data multiple times.