zzzprojects / EntityFramework-Extensions

Entity Framework Bulk Operations | Improve Entity Framework performance with Bulk SaveChanges, Insert, update, delete and merge for SQL Server, SQL Azure, SQL Compact, MySQL and SQLite.
https://entityframework-extensions.net
346 stars 57 forks source link

DeleteFromQuery() throws Object reference not set to an instance of an object. #259

Closed JendaPerl closed 5 years ago

JendaPerl commented 5 years ago

Seems a recent update broke the DeleteFromQuery(). Even things like

db.EntitySet.Where(x => x.Id == 123456).DeleteFromQuery() throw the System.NullReferenceException

Stack: [NullReferenceException: Object reference not set to an instance of an object.] DbContextExtensions.(IQueryable1 , Action1 , CancellationToken ) +78 DbContextExtensions.DeleteFromQuery(IQueryable1 query, Action1 bulkOperationFactory) +61 AdminWeb.Controllers.HomeController.Index() in C:\Linksoft\Kosmas\AdminWeb\Controllers\HomeController.cs:40 ...

I thought that maybe it was the Miniprofiler, but no. It throws the exception even when the Miniprofiler is disabled and the db.StoreConnection is the plain System.Data.SqlClient.SqlConnection.

from packages.config in one project: <package id="Z.EntityFramework.Classic" version="7.1.11" targetFramework="net462" /> <package id="Z.EntityFramework.Extensions.Classic" version="3.21.9" targetFramework="net462" /> <package id="Z.Expressions.Eval" version="2.9.45" targetFramework="net462" />

and another project: <package id="Z.EntityFramework.Classic" version="7.1.9" targetFramework="net452" /> <package id="Z.EntityFramework.Extensions.Classic" version="3.18.5" targetFramework="net452" /> <package id="Z.Expressions.Eval" version="2.9.39" targetFramework="net472" />

The method throws in both projects.

I think I've tried this in the first project before and it worked, but I'm not sure what versions were in use at that time. I think it was the 7.1.9+3.18.5+2.9.33, but that doesn't work now in the second project.

I normally call this.Connection.Open(); within the object context constructor, but I tried without and it did not make a difference. Neither did removing this.ContextOptions.LazyLoadingEnabled = true; from the constructor.

This is a cut-down test project, the DeleteFromQuery() throws even here. EFC_app.zip

JonathanMagnan commented 5 years ago

Hello @JendaPerl ,

Are you sure it ever worked with our library? We used some very old version and we never successfully make it works.

The issue is that we don't find any DbContext from your ObjectContext.

One way for us to solve it is by creating the DbContext

return new DbContext(objectContext, false);

Is there a reason why you still create your model using the ObjectContext instead of the DbContext in EF6?

JendaPerl commented 5 years ago

I may be wrong. I think I tried it and it worked, but with all the changes and attempts to make things work I may be wrong. :-)

I guess it's not worth it to make changes to your code. On places where it matters I use stored procedures already anyway. I'll just keep in mind that it's a "DbContext" feature only.

I can't just "switch" to DbContext. Back in EF 4.0 times I "forked" the DB.designer.tt and created a Visual Studio plugin that adds lots of properties to all kinds of objects in the .edmx/Model Designer and I've kept modifying and updating the .tt and related libraries so now I generate maybe five times more code from the edmx and together with all the libraries ... Already updating the .tt to work with EF 6 without switching over to DbContext was an awful lot of work.