eager-loading

Eager Loading in Entity Framework Core 2.2

丶灬走出姿态 提交于 2020-06-01 05:52:45
问题 Have Entity Framework Core 2.2 removed Eager Loading? The include method won't appear in Visual Studio intellisense, and when I write it manually it gives an error: "it does not contain a definition for 'Include' and no accessible extensions..." Example School school = _context.Students.Include... My demo is made with a simple ASP Core 2.2. Web Application. Not much have been changed. The project use this packages: <ItemGroup> <PackageReference Include="Microsoft.AspNetCore.App" />

Returning Domain Objects from Repositories on Joining tables

元气小坏坏 提交于 2020-04-30 11:22:21
问题 I have been reading that Repositories should return domain objects only. I am having difficulty with implementing this. I currently have API with Service Layer, Repository and I am using EF Core to access sql database. If we consider User(Id, Name, address, PhoneNumber, Email, Username) and Orders (id, OrderDetails, UserId) as 2 domain objects. One Customer can have multiple Orders. I have created navigation property public virtual User User{ get; set; } and foreign Key. Service layer needs

Return first element from relation with `Lazy Eager Loading` [Laravel 5.2]

假装没事ソ 提交于 2020-03-06 03:08:08
问题 I have relation like this: public function message() { return $this->hasMany('Engine\Message'); } inside my Conversation model. and for each conversation I need to get last message. Here is what I tried, but this will get only one message for first conversation but will not get message from other conversations... $con = Conversation::all(); $con->load(['message' => function ($q) use ( &$mess ) { $mess = $q->first(); }]); return $con; I don't wana query for each record... Anyone know how to

How to eager load relationships on multiple levels?

回眸只為那壹抹淺笑 提交于 2020-01-25 16:49:07
问题 I have the following models: ProductA - ProductB - Feature - Option I have the following relations: ProductA belongsToMany ProductB ProductB belongsToMany Feature Feature belongsToMany Option When I want to see details of ProductA, (a post with ProductA id, that will be managed by a controller) I'd like to eager load all the relationships to pass a variable containing all the details to the view. Is it possible with a single Eloquent instruction? Something like this (I know it's not working):

Passing conditional param to Eager Loading in larave ends with error

試著忘記壹切 提交于 2020-01-22 02:34:08
问题 my code is as followed: return User::whereHas('roles', function ($role, $query) { return $role; $query->whereId($role); })->get(); what I am trying is to pass role id here to query builder. it ends up with following error: Symfony\Component\Debug\Exception\FatalThrowableError Too few arguments to function App\Http\Controllers\UserController::App\Http\Controllers\{closure}(), 1 passed in /Users/x/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php on line 962 and exactly 2

Returned object has null/empty ICollection unless it is accessed/inspected first

≯℡__Kan透↙ 提交于 2020-01-17 02:37:11
问题 Seeding deserialised JSON data in the Context and then attempting to return the DbSet. api/module returns all Modules with empty ICollections (null if I don't instantiate them), where as the Assessments happily return the virtual Module. Previous experience in MVC whereby I would have been accessing the object before it being sent to the view, so I haven't encountered this issue before. The commented out line: //Enumerable.ToList(ModuleItems.Include(mi => mi.Assessments)); Resolves the issue