EF - Eager include grandchildren not working, does not contain definition for 'Select' - entity-framework

I am working in VS 2015 solution with an MVC 5 project and a code library project using EF 6.1. I have redone the entire project from a previous version in VS 2013 hoping it would resolve this problem, but no luck. I am just trying to eager load a child with grandchildren. I tried this:
Test t = db.Tests
.Include("TestsRemoteOBD")
.Include("TestsRemoteOBD.TestsRemoteOBDDtcs")
.FirstOrDefault();
and this:
Test t = db.Tests
.Include(i=>i.TestsRemoteOBD)
.Include(i=>i.TestsRemoteOBD.Select(s=>s.TestsRemoteOBDDtcs))
.FirstOrDefault();
Include using the string works for the child, but not the grandchildren. And with the 2nd query, I'm getting this error:
'TestsRemoteOBD' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'TestsRemoteOBD' could be found (are you missing a using directive or an assembly reference?)
I've seen the same question resolved by adding "using System.Data.Entity;", but I had done that a long time ago. No help. What else could I be missing?
Thanks!

Thanks so much, to all those who commented. I would have been banging my head for another day, at least. The way I had written the second statement would have worked fine if the parent [Tests] had a 1-to-many relationship with [TestRemoteOBD], but since the latter was not a collection, there was no 'Select' defined. I have not tested, but I believe it should be something like:
Test t = db.Tests
.Include(i=>i.TestsRemoteOBD)
.Include(i=>i.TestsRemoteOBD.TestsRemoteOBDDtcs)
.FirstOrDefault();

Bump. Found this and thought I'd throw in my own research.
Microsoft's
Documentation
states: To include a reference and then a reference one level down: query.Include(e => e.Level1Reference.Level2Reference)
-- but this doesn't seem to work under even the simplest use case. Executing the query with a .ToList() does not actually include anything. Smells like an obvious bug to me!
I can see 2 workarounds.
First, as Ben here suggests, you can manually iterate over the entities to force EF to load them.
Second, if scoping permits, you can forget the 'using' block and keep a more permanent instance of the dbcontext as a class member so that it's still available, not disposed (System.ObjectDisposedException), when the members are accessed.

Related

EF Core Second level ThenInclude missworks

Assume having these models first:
Method that has one OriginalCode
OriginalCode that has many Mutants
Mutant that has many ParseSubTrees
Now when querying on Method I want the other being loaded. So I have the following:
Method targetMethod = dBContext.Methods
.Include(me => me.OriginalCode)
.ThenInclude(oc => oc.Mutants)
.FirstOrDefault(me => me.Id == id);
and the next step is to include additionally the ParseSubTree. But the thing is that I can't access it. See the following Image:
the problem is "mu is a list instead of being an object reference"!
Where is my mistake!
TG.
This is a known Intellisense issue with the ThenInclude overload for collection type navigation properties, tracked by the Completion missing members of lambda parameter in fault tolerance case #8237 Roslyn GitHub issue.
Until it gets fixed, simply type the name of the property and it will compile successfully and work as expected.
.ThenInclude(mu => mu.ParseSubTrees)
Update: Now it's even specifically mentioned in the Including multiple levels
section of the EF Core documentation:
Note
Current versions of Visual Studio offer incorrect code completion options and can cause correct expressions to be flagged with syntax errors when using the ThenInclude method after a collection navigation property. This is a symptom of an IntelliSense bug tracked at https://github.com/dotnet/roslyn/issues/8237. It is safe to ignore these spurious syntax errors as long as the code is correct and can be compiled successfully.
In my case there was a conflict between the namespaces System.Data.Entity and Microsoft.EntityFrameworkCore. Just delete the first using line.

Entity Framework 7 Errors from Key, Collection, Reference definitions missing in ASP.NET Beta 8

Problem
I am having trouble with upgrading my beta7 application to beta8. I had problems with over 50 errors, but now have gotten it down just to EF7. It was recommended by stack overflow friends and users that I ask this question specifically.
Entity Framework Error
Error CS1061 'EntityTypeBuilder<Unit>' does not contain a definition for 'Collection' and no extension method 'Collection' accepting a first argument of type 'EntityTypeBuilder<Unit>' could be found (are you missing a using directive or an assembly reference?) SampleProject.DNX 4.5.1
I get the same error for Reference as well.
I have the following code in my OnModelCreating(ModelBuilder builder) method in my DbContext.cs file.
builder.Entity<Unit>(entity =>
{
entity.HasKey(unit => unit.UnitId);
entity.Collection(unit => unit.UnitBins).InverseReference(bin => bin.BinUnit)
.ForeignKey(bin => bin.UnitId).Required(true);
entity.Reference(u => u.RefUnitType).InverseReference().ForeignKey<Unit> (u => u.RefUnitTypeId).Required();
});
The problem is that EntityBuilder does not seem to have definitions for Collection and Reference. This is about the second or third time this kind of logic has been refactored in EF it feels like and so I am very confused as to how to rewrite my logic above.
Attempts:
I tried the following things:
I tried using intellisense in VS15, but nothing similar was found
I have searched the EF7 issues GitHub page, but didn't see anything
I also tried dnu restore just to be sure the upgrade didn't do something strange with my project.json lock.
Using entity.HasKey() instead of entity.Key(), but have not found other methods comparable to the others?
Now that beta8 is feature complete, I may rewrite significant portions of my project. However, for now I need to resolve these errors so that I can compile the project again. Any help, assistance, and advice rendered would be greatly appreciated.
Collection => HasMany
Reference => HasOne
InverseReference => WithOne

breeze mongo manager.saveChanges() error

I'm learning from breeze-zza-mongodb sample.
I get some problems when i try to use the saveChanges() function from breeze.
This is the error i get:
"TypeError: Cannot read property 'update' of null at... node_modules\breeze-mongodb\mongoSaveHandler.js : 229:20 at Array.forEach"
Any of you tried and got this error? I searched Google for a bit longer but i can't find this issue. And if i try to manager.getChanges() and put the changes in array, i get my entity with modified state.
The guys from breeze didn't covered this part and i'm completly blind in this. Thank you for your time guys.
I solved my problem. I included the modules in VS so i can debug and i noticed that breeze misnamed my collection name for some reason adding an s at the end.
Anyway.. for now i just removed that, and it works. I will dig deeper to see where and why is breeze adding an s at the end of my collection name because i want to treat the cause, not the effect. Thanks.

Autofac configuration problems trying to integrate Quartz

I am working with the Owned type as found here: Strong reference of Autofac 2
I'm also using Quartz scheduler, MSMQ, and EF.
My config looks as follows. I've clearly got something wrong as the context that gets injected to the repositories is a different instance than the one given to the service.
builder.RegisterType<EmailAllocationJob>();
builder.RegisterGeneric(typeof(JobWrapper<>));
builder.RegisterType<DataContext>().InstancePerOwned<EmailAllocationJob>();
builder.RegisterType<DataContext>().As<IUnitOfWork>();
builder.RegisterType<EmailAccountRepository>().As<IEmailAccountRepository>();
builder.RegisterType<EmailMessageRepository>().As<IEmailMessageRepository>();
builder.RegisterType<EmailMessageQueue>().As<IEmailMessageQueue>();
builder.RegisterType<EmailAllocationService>().As<IEmailAllocationService>();
I can't for the life of me figure out how to get the configuration fixed. I'd reckon it's the line:
builder.RegisterType<DataContext>().As<IUnitOfWork>();
What I want to say is something like:
builder.RegisterType<DataContext>().As<IUnitOfWork>().InstancePerOwned<EmailAllocationJob>();
Thanks in advance if you can help.
Ok I got it. Needed the line:
builder.RegisterType<DataContext>().InstancePerOwned<EmailAllocationJob>()
.As<IUnitOfWork>().AsSelf();
So it seems important that the DataContext features as the generic argument to RegisterType ONCE, and that the method calls to As<>() and AsSelf() are to be daisy chained in a single statement. Seems obvious now, with a fresh head following yesterday evening.

How to get an EntityCollection<class> instead of just a class?

I'm trying to convert a one-to-many relationship in entity framework to a many-to-many.
I've done the admin work and generated a database, which seems to be fine but now I'm running through the resulting errors and trying to get the code to compile. Most of it is fine, but one particular change is giving me a headache - I can't figure out the correct syntax. I'm sure I'm being very stupid here.
The original line was:
addedService.CompiledDatabase = context.Databases.OfType<CompiledDatabase>().First();
But addedService.CompiledDatabase is no longer looking for a single instance of CompiledDatabase, it's looking for a collection: an EntityCollection to be precise.
I can't figure out how to get a linq/lambda query that will return a collection of the required type. Assistance gratefully appreciated.
Something like?
addedService.CompiledDatabase.Attach(context.Databases.OfType<CompiledDatabase>());
...if addedService is already in context.