Search code examples
c#mysql.netentity-framework

Console gets data but API call doesn't when using same method


I'm in the middle of creating a multi-tier application which includes the following layers

API

Business

DAL

-

Entities

All Layers have access to the Entities and also access to the layer that is beneath them in the list.

I also have a Console project.

My problem is that I make a call to the Business layer to get all data from a table, it works fine and retrieves two rows. However, I used the same method inside my API layer and it returns no rows - it also throws no errors and has left me stranded.

These are the classes/methods:

API:

public class CompaniesController : ApiController
{
    CompaniesService _companiesService = new CompaniesService();

    public IEnumerable<Company> GetAllCompanies()
    {
        return _companiesService.GetAllCompanies();
    }
}

Business:

public class CompaniesService
{
    public List<Company> GetAllCompanies()
    {
        var companyRepository = new CompanyRepository();

        return companyRepository.GetAll();
    }
}

DAL:

public class CompanyRepository
{
    public List<Company> GetAll()
    {
        //create DBContext object
        using (var context = new dbEntities())
        {
            var data = context.Company.ToList();

            return data;
        }
    }
}

Entity:

[Table("Companies")]
public class Company
{
    [Key]
    [Column("ID")]
    public int ID { get; set; }

    public string Name { get; set; }

    public string Symbol { get; set; }

    public string Exchange { get; set; }
}

I am using Entity Framework 6 on Visual Studio 2013 and using a MySQL Database. Each layer is inside of a different project - just in case this is a reference based problem.


Solution

  • The problem was a simple fix, I simply didn't have the correct connection string inside of the API Project.