kay00
kay00

Reputation: 447

Breeze System.InvalidCastException with EntityFramework

Can't get data. I can see the data in visual studio. But I can'get the web front end to display the data when I go to site mycomputername:21290/breeze/breeze/getdata. I get this exception: Unable to cast object of type 'MySqlEntities' to type 'System.Data.Entity.DbContext'.

Do I have to have odata controller?

public class BreezeController: ApiController
{
private readonly IRepository _repo;

..

[HttpGet]
        public IEnumerable<MySqlType> GetData()
        {
            return _repo.GetData();
        }

}

public class Repository : IRepository
{
private readonly EFContextProvider<MySqlEntities> 
            _contextProvider = new EFContextProvider<MySqlEntities>();

 public IQueryable<MySqlType> GetData()
        {
            return _contextProvider.Context.TableA;
        }

}

Auto-generated code in Designer.cs MySqlEntities : ObjectContext

Upvotes: 0

Views: 126

Answers (1)

Steve O
Steve O

Reputation: 228

The hint is in the error message: EFContextProvider expects a compatible type - something that can be cast to System.Data.Entity.DbContext; not your MySqlEntities type.

So this line:

private readonly EFContextProvider<MySqlEntities> 
        _contextProvider = new EFContextProvider<MySqlEntities>();

in you Repository class should be provided with a compatible db context.

See the Breeze tutorials: http://www.breezejs.com/documentation/efcontextprovider

For future, it would be clearer if you elaborated what MySqlEntities is in code.

Upvotes: 1

Related Questions