Datatables Server Side: How to fetch data from database using Entity Framework instead of dummy data

Datatables Server Side: How to fetch data from database using Entity Framework instead of dummy data

Sifat AhmedSifat Ahmed Posts: 3Questions: 1Answers: 0

I am following this article https://levelnis.co.uk/blog/datatables-with-web-api-part-3-paging-sorting-searching where dummy data is used using a class.

I am stuck in here (Here I want to fetch data from database using Entity Framework)

public class CustomerSearchController : SearchController
{
    public IHttpActionResult Post(SearchRequest request)
    {
        var allCustomers = JsonConvert.DeserializeObject<CustomerData>(CustomerData.DataSource);
        var response = WrapSearch(allCustomers.Data, request);
        return Ok(response);
    }

    private static CustomerSearchResponse WrapSearch(ICollection<CustomerSearchDetail> details, SearchRequest request)
    {
        var results = ApiHelper.FilterCustomers(details, request.Search.Value).ToList();
        var response = new CustomerSearchResponse
        {
            Data = PageResults(results, request),
            Draw = request.Draw,
            RecordsFiltered = results.Count,
            RecordsTotal = details.Count
        };
        return response;
    }
}

CustomerData Class:

public class CustomerData
{
    public const string DataSource = @"

    {
   ""Data"": [
    {
      ""CompanyName"": ""Microsoft"",
      ""Address"": ""1 Microsoft Way, London"",
      ""Postcode"": ""N1 1NN"",
      ""Telephone"": ""020 7100 1000""  
    },
    {
      ""CompanyName"": ""Nokia"",
      ""Address"": ""2 Nokia Way, London"",
      ""Postcode"": ""N2 2NN"",
      ""Telephone"": ""020 7200 2000""
    },
    {
     ""CompanyName"": ""Apple"",
      ""Address"": ""3 Apple Way, London"",
      ""Postcode"": ""N3 3NN"",
      ""Telephone"": ""020 7300 3000""
      }
     ] 
   }";

    public IList<CustomerSearchDetail> Data { get; set; }
}

I don't want to use dummy constant data. Instead, I want the data to come from ApplicationDbContext using Entity Framework. Any help?

Answers

  • allanallan Posts: 61,714Questions: 1Answers: 10,103 Site admin

    Sorry - that article wasn't written by myself, so you'd need to contact the author if you need help with it. I've not used EF much, so I can't help in that regard.

    Allan

  • Sifat AhmedSifat Ahmed Posts: 3Questions: 1Answers: 0

    Thank you so much for your reply.

    I have contacted the author. I guess he is busy. Let see.

This discussion has been closed.