0

Hopefully the title was clear enough. My repository method looks like this :

public async Task<List<Model>> GetAllByCode(string code)
{
    using (var ctx = new DatabaseContext())
    {
        return await ctx.Models.Where(m => m.Code.Equals(code)).ToListAsync();
    }
}

Out of the result of that method, I create an observable collection which I bind to the Combobox in the view.

Models = new ObservableCollection<Model>(await ModelRepository.GetAllByCode("code"));

But the ObservableCollection is always empty. I can even see the results in Debugger (if I'm not mistaken, it's an IEnumerable or IQueryable collection). I'm 100% sure that database is okay, because when I use

return await ctx.Models.ToListAsync();

it returns all the rows from the database.

Can anyone tell me what I'm doing wrong?

Edit :
The problem is in entity framework. I still haven't found a solution, but my context doesn't retrieve new data from the table, instead he always takes cached (I suppose) version even though I'm using a disposable context with every request.

Still not answer to it though.

TheITDejan
  • 808
  • 9
  • 27
  • Do you really have the value `code` in the `Code` field? – Ofir Winegarten Sep 27 '17 at 12:35
  • 1
    Sure there is any Model with `m.Code == "code"`? Or did you perhaps intend to write `ModelRepository.GetAllByCode(code)` instead of `ModelRepository.GetAllByCode("code")`? – Clemens Sep 27 '17 at 12:36
  • Yes, I do have it. I double checked it, even for spelling errors. – TheITDejan Sep 27 '17 at 12:37
  • Can you share some sample data? – Ofir Winegarten Sep 27 '17 at 12:40
  • Note that the comparison is done in the DB so it will be based on the collation you have set there which may or may not ignore case. – juharr Sep 27 '17 at 12:48
  • Database has been set to ignore cases. – TheITDejan Sep 27 '17 at 12:49
  • 2
    Have you tried to trace the actual sql that ef generates? – oakman Sep 27 '17 at 12:57
  • Also check for simple things that are not always obvious like trailing spaces – Mark Peters Sep 27 '17 at 13:04
  • I'm not sure what's wrong it it, I think I found the error. EF Doesn't retrieve the last 3 entries I added (which are the entries with the code I'm looking for). I did manually enter it since this is just a testing phase. – TheITDejan Sep 27 '17 at 13:04
  • I'm just querying a model object from his database table, and the model has "Code" property in it. It's just a string. Regardless, I've figured it's an EntityFramework issue where the Context I'm using (even though it's disposable) is not returning any new data. I have no clue how to fix this. :/ – TheITDejan Sep 27 '17 at 13:42

1 Answers1

0

Equals can fail in LINQ to Entity if Code isn't a string, you can use String.Compare() or (m.Code as string).Equals() or this:

public async Task<List<Model>> GetAllByCode(string code)
{
    using (var ctx = new DatabaseContext())
    {
        return await ctx.Models.Where(m => m.Code.ToLower() == code.ToLower()).ToListAsync();
    }
}

Or check your entries in table with this, step by step in debugger:

public async Task<List<Model>> GetAllByCode(string code)
{
    using (var ctx = new DatabaseContext())
    {
        var all = await ctx.Models.ToListAsync();
        var result = all.Where(m => m.Code.Equals(code, StringComparison.InvariantCultureIgnoreCase)));
        return result;
    }
}

Please note that an ObservableCollection is not thread-safe.

cSteusloff
  • 2,487
  • 7
  • 30
  • 51
  • I tried this, it's the same result. `result` variable holds an IEnumerable, but when I call `.ToList()` on it, and return it from the Repo, observable collection is still empty. – TheITDejan Sep 27 '17 at 12:48
  • A observalbeCollection isn't thread safe. That's the problem. https://stackoverflow.com/questions/23108045/how-to-make-observablecollection-thread-safe But boths snippeds works fine with async & await! – cSteusloff Sep 27 '17 at 12:51
  • That might just be an answer, gonna recheck but I think I got it. Can you edit your answer with "Non-thread-safety" please. – TheITDejan Sep 27 '17 at 12:53