Im currently building a web api 2 API using Entity Framework 6. All was going well as a lot of the api calls are just straight through to a single table and return the object as a JSON.
Now my headaches have come with trying to build a single object from various models. I am using Code first.
A Simplified version of the object i want to return is this
{ Name : Brand.Name, CategoryScores : [{CategoryId : 'x', Score : 'x'}]}
These are my current models
[Table("Brands")]
public class BrandModel
{
[Key]
public string BrandId { get; set; }
public string BrandName { get; set; }
}
[Table("CategoryScores")]
public class CategoryScoresModel
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public string BrandId { get; set; }
public string TraqCategoryScore { get; set; }}
public class TraqIndexDTO
{
public string BrandName { get; set; }
public IEnumerable<CategoryScoresDTO> CategoryScores { get; set; }
}
public class CategoryScoresDTO
{
public string CategoryId { get; set; }
public string TraqCategoryScore { get; set; }
}
Controller the logic in here will be moved out once its working
private AuthContext db = new AuthContext();
public IHttpActionResult Get()
{
//var results = this.repository.SelectAll();
return Ok(GetTraqIndex());
}
public IEnumerable<TraqIndexDTO> GetTraqIndex()
{
var traqIndex = from b in db.Brand
select new TraqIndexDTO()
{
BrandName = b.BrandName,
CategoryScores = getCatgoryScoresByBrandId(b.BrandId)
};
return traqIndex;
}
public IEnumerable<CategoryScoresModelDTO>getCatgoryScoresByBrandId(string brandId)
{
var scores = from s in db.CategoryScoresModel
where s.BrandId == brandId
select new CategoryScoresDTO() {
CategoryId = s.CategoryId,
TraqCategoryScore = s.TraqCategoryScore
};
return scores;
}
Currently I am getting this error " getCategoryScoresByBrandId(System.String)' method, and this method cannot be translated into a store expression.""
Any help and guidance or a good example to follow would be much appreciated
Thanks Rob