bookuha
❔ Why .Where() doesn't work with .Include()?
I need to fetch chats that have current user as a participant, and want to fetch only active users.
I execute the following query:
var query = _db.Chats
.Include(c => c.Participants.Where(cu=>cu.IsActive))
.Include(c=>c.LastMessage)
.ThenInclude(m => m.Sender)
.ThenInclude(cu => cu.User)
.Where(c =>
c.Participants.Any(cu => cu.UserId == _currentUser.MessengerUserId))
.OrderByDescending(c => c.LastMessage != null ? c.LastMessage.Timestamp : c.Created)
.AsNoTracking();
but participants filtering part doesn't work - all the users are included, while it works as intended with 'select only chats with current user as participant' condition.
https://i.stack.imgur.com/WDA6h.png - Code with Resharper Annotations
https://pastebin.com/Bvsxju91 - Generated PgSql script
https://stackoverflow.com/questions/76157093/why-where-doesnt-work-with-include - SO question8 replies
❔ CRUD(REST) + DDD
Hi, I’m developing a simple CRUD messenger and trying to also have it DDD’ed.
The problem is that the interaction interface is REST, and I want to update a specific message content/recipient/color.
By terms of DDD I could simply get some domain services, aggregates with this logic injected or value objects and simply call the ChangeMessageColor(Message) method.
But REST, the way I understand it, makes me expose a single PUT message/id endpoint, that will receive any changes to the resource
5 replies
❔ Get id from query or DTO
Imagine I have the next two entities
class Task{
public int Id {get;set;}
public string Desc {get;set;}
public int AssignmentId {get;set;}
public Assignment Assignment {get;set;}
class Assignment{
public int Id {get; set;}
public ICollection<Task> Tasks {get;set;}
}
What would be the best way to POST a task to an existing assignment?
How would endpoint and incoming DTO look like?
1) /assignments/3/tasks
Receive
TaskDTO1{
string Desc
}
and assignment id to your controller method,
call service method that will retrieve assignment by the id and append a task to its task collection, then save
2) /tasks/
Receive
TaskDTO2{
string Desc,
int AssignmentId
}
in your controller method, call service method that will create a task entity, retrieve a corresponding assignment by id (AssignmentId from dto2) and fill the Task.Assignment with this assignment, then save
Which way looks better to you? Why?
5 replies
❔ Navigation properties internals. EFCore
How are they implemented internally?
What if I select related entity Id to 3, and the related entity navigation property to the entity with id 5? What will happen? Why?
If I set the related entity id to 5, but don’t make any changes to the related navigation property, will it get filled with an entity with id 5 at runtime? will i be able access its fields before SaveChanges? Or it will remain null
2 replies
❔ Automapper map Id to Entity
I receive the following DTO (request)
BookDTO{
int Id,
int AuthorId
}
I want the mapper to map AuthorId to the Author entity and put in into the Book.Author.
How do I do this? Should I inject the dbcontext into my profiles?
Or can I simply map Dto AuthorId to Entity AuthorId and it will be fine and navigation will be filled once context is saved?
What is the best way?
20 replies
❔ Multiple cascade paths and optional FKs
My entity can be cascaded by two referenced entities. My FKs are required.
Should I always switch them to optional?
When would I want to leave them required?
Multiple cascade paths looks as very bad concept to me
2 replies
❔ REST and Subresourses
I have two next entities
class Note{
public int Id {get;set}
public string Content
public int NotebookId {get;set}
public Notebook {get;set}
}
class Notebook{
public int Id {get;set}
public List<Note> Notes {get;set}
}
If I want to change some Note Content, how should the URL look like?
19 replies
Keep up with Db(First)
How do I keep up with my Db changes with DbFirst way?
Do I rescaffold it every time I make a change?
How to transition from DbFirst to CodeFirst smoothly?
I need to add a field to my model(table), but I feel like any change may ruin the entire data layer
How does DbFirst works? Applying constraints to my DB tables also makes EF to follow them without any updates. Why?
8 replies
DBFirst one to one or zero
I have One Patient to One or No MedicalCard associated with him
EF Generated the following code:
Why did it create Collections? What's the use of them? I have id and single entity navigation property, but why the collections?
15 replies
Minimum need data to create an Entity
My entity:
My DTO:
The questions are:
1) Do I need that much data just to create a Book?
Maybe should I only have "Name" and have the entity populated
with PUT/PATCHES later?
2) What could be done better?
AuthorIds, FilesIds are obtained by a prior GET request on these resources on the frontend and mapped to the Author and DownloadableFile entites later in a CreateBookHandler
Thank you
1 replies
MediatR commands and DTOs [Answered]
Currently, I am passing DTOs to my controllers,
I am using MediatR and it supports commands.
I use these commands just to pass these DTOs into them, like some kind of envelope (what they essentially are, I guess)
Can I use commands instead of DTOs directly?
What are the pros and cons?
10 replies