bookuha
bookuha
CC#
Created by bookuha on 5/2/2023 in #help
❔ 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 question
8 replies
CC#
Created by bookuha on 4/13/2023 in #help
❔ Access appsettings.json in ASP.NET Web API in Azure published App Service
2 replies
CC#
Created by bookuha on 3/21/2023 in #help
❔ 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
CC#
Created by bookuha on 3/5/2023 in #help
❔ S3 Buckets over HTTP? Is it ok?
I want to implement file exchanging in my messanger app (videos, photos). In the examples I see people write memorystreams to bytes arrays and return them, is this a normal practice?
11 replies
CC#
Created by bookuha on 1/1/2023 in #help
❔ Locating views in another Assembly
4 replies
CC#
Created by bookuha on 11/27/2022 in #help
❔ Entity logic splitting
4 replies
CC#
Created by bookuha on 11/25/2022 in #help
❔ 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
CC#
Created by bookuha on 11/25/2022 in #help
❔ 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
CC#
Created by bookuha on 11/25/2022 in #help
❔ 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
CC#
Created by bookuha on 11/23/2022 in #help
❔ 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
CC#
Created by bookuha on 11/23/2022 in #help
❔ 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
CC#
Created by bookuha on 11/8/2022 in #help
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
CC#
Created by bookuha on 11/5/2022 in #help
Phantom constraint
1 replies
CC#
Created by bookuha on 11/5/2022 in #help
DBFirst one to one or zero
I have One Patient to One or No MedicalCard associated with him EF Generated the following code:
public Patient(){
{
Appointments = new HashSet<Appointment>();
MedicalCards = new HashSet<MedicalCard>();
}

public int Id { get; set; }
public string? Lastname { get; set; }
public string? Firstname { get; set; }
public string? Middlename { get; set; }
public string? Phone { get; set; }
public string? Address { get; set; }
public bool? Gender { get; set; }
public int? Medcard { get; set; }

public virtual MedicalCard? MedcardNavigation { get; set; }
public virtual ICollection<Appointment> Appointments { get; set; }
public virtual ICollection<MedicalCard> MedicalCards { get; set; }

public partial class MedicalCard
{
public MedicalCard()
{
MedicalReports = new HashSet<MedicalReport>();
Patients = new HashSet<Patient>();
}
public int Id { get; set; }
public int Patient { get; set; }
public DateTime? Issued { get; set; }

public virtual Patient PatientNavigation { get; set; } = null!;
public virtual ICollection<MedicalReport> MedicalReports { get; set; }
public virtual ICollection<Patient> Patients { get; set; }
}
public Patient(){
{
Appointments = new HashSet<Appointment>();
MedicalCards = new HashSet<MedicalCard>();
}

public int Id { get; set; }
public string? Lastname { get; set; }
public string? Firstname { get; set; }
public string? Middlename { get; set; }
public string? Phone { get; set; }
public string? Address { get; set; }
public bool? Gender { get; set; }
public int? Medcard { get; set; }

public virtual MedicalCard? MedcardNavigation { get; set; }
public virtual ICollection<Appointment> Appointments { get; set; }
public virtual ICollection<MedicalCard> MedicalCards { get; set; }

public partial class MedicalCard
{
public MedicalCard()
{
MedicalReports = new HashSet<MedicalReport>();
Patients = new HashSet<Patient>();
}
public int Id { get; set; }
public int Patient { get; set; }
public DateTime? Issued { get; set; }

public virtual Patient PatientNavigation { get; set; } = null!;
public virtual ICollection<MedicalReport> MedicalReports { get; set; }
public virtual ICollection<Patient> Patients { get; set; }
}
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
CC#
Created by bookuha on 11/3/2022 in #help
DTOs and the data in them
24 replies
CC#
Created by bookuha on 11/3/2022 in #help
DTOs inside of DTOs
Is it normal to have class GuyDTO{ string Name {get;set} int Age {get;set;} BikeDTO Bike {get;set;} } Like, I should have BikeDTO always, not the Bike entity itself, right?
25 replies
CC#
Created by bookuha on 10/6/2022 in #help
Correct POST and PATCH
2 replies
CC#
Created by bookuha on 10/3/2022 in #help
Many-To-Many with same fields of same type
I have a tables Users and Transactions. I want to make my Transactions store two entities: sender and reciever (Both are users) How do I configure with Entity Framework Fluent API?
17 replies
CC#
Created by bookuha on 9/28/2022 in #help
Minimum need data to create an Entity
My entity:
public class Book : BaseEntity
{
public string Name { get; set; }

public Genres Genres {get; set;}
public string BriefDescription { get; set; }
public string FullDescription { get; set; }

public DateTime OriginallyPublishedAt { get; set; }
public DateTime AppPublishedAt { get; set; }

public ICollection<Author> Authors { get; set; } = new HashSet<Author>();
public ICollection<DownloadableFile> DownloadableFiles { get; set; } = new HashSet<DownloadableFile>();


}
public class Book : BaseEntity
{
public string Name { get; set; }

public Genres Genres {get; set;}
public string BriefDescription { get; set; }
public string FullDescription { get; set; }

public DateTime OriginallyPublishedAt { get; set; }
public DateTime AppPublishedAt { get; set; }

public ICollection<Author> Authors { get; set; } = new HashSet<Author>();
public ICollection<DownloadableFile> DownloadableFiles { get; set; } = new HashSet<DownloadableFile>();


}
My DTO:
public record BookRequest()
{
public long Id { get; set; } // Do I need it here? MSDN has it here
public string Name { get; set; }
public Genres Genres { get; set; }
public string BriefDescription { get; set; }
public string FullDescription { get; set; }
public DateTime OriginallyPublishedAt { get; set; } = new DateTime(1995, 11, 1);
public ICollection<long> AuthorIds { get; set; } = new HashSet<long>();
public ICollection<long> FileIds { get; set; } = new HashSet<long>();
}
public record BookRequest()
{
public long Id { get; set; } // Do I need it here? MSDN has it here
public string Name { get; set; }
public Genres Genres { get; set; }
public string BriefDescription { get; set; }
public string FullDescription { get; set; }
public DateTime OriginallyPublishedAt { get; set; } = new DateTime(1995, 11, 1);
public ICollection<long> AuthorIds { get; set; } = new HashSet<long>();
public ICollection<long> FileIds { get; set; } = new HashSet<long>();
}
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
CC#
Created by bookuha on 9/28/2022 in #help
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