HighwayFramework / Highway.Data

The fastest and smoothest way to great architecture
http://hwyfwk.com
MIT License
72 stars 30 forks source link

'IUnitOfWork' does not contain a definition for 'AsQueryable' #119

Closed maike9 closed 5 years ago

maike9 commented 5 years ago

I recently upgraded all my projects to .Net Framework 4.6.2. Highway.Data & Highway.Data.EntityFramework is at verison 6.0.6

What has changed that would cause the following build error .

'IUnitOfWork' does not contain a definition for 'AsQueryable' and the best extension method overload 'Queryable.AsQueryable(IEnumerable)' requires a receiver of type 'IEnumerable'

The code is

return _repository.Context.AsQueryable<ServiceData>().FirstOrDefault(c => c.CustomerID == customerId);

My predecessor implemented the Highway Framework and I am not well versed in what it does. Any help would be appreciated.

trayburn commented 5 years ago

I cannot fathom a reason why this method (which does exist on the class DataContext in the Highway.Data.EntityFramework library would not be showing up for you. I would recommend a Clean and Rebuild to see if it solves your issue.

maike9 commented 5 years ago

I did a clean and rebuild. No bueno. it worked using 4.1.0 My app is using Castle Windsor IoC to instantiate the repo. It registering Highway.Data.IRepository that instantiates Highway.Data.Repository and Highway.Data.IDataContext that instantiates Highway.Data.DataContext.

Inversion 7 should I be using IEntityDataContext in order to gan access to AsQueryable? By the way I did this but i still get the error.

Note I confirmed the latest versions of Highway.Data.dll and Highway.Data.entityFramework,dll are correct.

Any help would be appreciated,

trayburn commented 5 years ago

Ok, I figured out what was going on. With Version 6 we changed the Repository in order to make the type of querying you are doing (or rather your predecessor was doing) harder because we did not feel it modeled the behaviors the project was trying to encourage. If you change your code as follows, it should allow the behavior again:

return (DataContext)(_repository.Context).AsQueryable<ServiceData>().FirstOrDefault(c => c.CustomerID == customerId);

You could also write yourself a helper which would make this easier as an Extension Method as follows:

public static class RepositoryExtensions
{
  public DataContext GetContext(this IRepository repo)
  {
    return (IDataContext)(repo.Context);
  }
}

This should allow the call above as: return _repository.GetContext().AsQueryable<ServiceData>().FirstOrDefault(c => c.CustomerID == customerId);

I hope this helps, the code above was written at an airport with no compiler, so typos are probably present. 😄

maike9 commented 5 years ago

So close ended up with this.

return ((DataContext)(_repository.Context)).AsQueryable<ServiceData>().FirstOrDefault(c => c.CustomerID == customerId);

Builds an works now. Thanks for all your help!!