This db query takes way too long - c#

I have a query for a details-view of a meeting, which has a lot of included related data:
Meeting meeting = await db.Meetings
.Include(a => a.Agenda)
.ThenInclude(s => s.Speakers)
.ThenInclude(u => u.User)
.Include(s => s.Summonings)
.ThenInclude(u => u.User)
.Where(p => p.Id == parsedMeetingId)
.FirstOrDefaultAsync();
I have set up a Stopwatch, and these are the results for the last six runs (SS:MS):
25:23
25:04
24:36
00:03 // What happened here, I really don't know!
25:25
25:06
There is not much data in the db; Only one Meeting with 4 Summonings, an Agenda of 11 AgendaItems, and only 1 Speaker in 1 AgendaItem.
My dev PC is a rather modest AMD laptop with only 8 GB RAM, running Windows 10. At the time of testing, I was only running Visual Studio and Chrome.
(How) can the query or model design be improved?
Where can I look for other bootle necks?
The models:
public class Meeting
{
public Guid Id { get; set; }
public Guid OwnerId { get; set; }
public Guid ModeratorId { get; set; }
public Guid ReportWriterId { get; set; }
public string Title { get; set; }
public List<AgendaItem> Agenda { get; set; }
public List<MeetingSummoning> Summonings { get; set; }
public ApplicationUser Owner { get; set; }
public ApplicationUser Moderator { get; set; }
public ApplicationUser ReportWriter { get; set; }
// Some more properties ...
}
public class AgendaItem
{
public Guid Id { get; set; }
public string Title { get; set; }
public Guid MeetingId { get; set; }
public Meeting Meeting { get; set; }
public List<Speaker> Speakers { get; set; }
// Some more properties ...
}
public class MeetingSummoning
{
public Guid Id { get; set; }
public Guid MeetingId { get; set; }
public Guid UserId { get; set; }
public Meeting Meeting { get; set; }
public ApplicationUser User { get; set; }
}
public class Speaker
{
public Guid Id { get; set; }
public DateTime Requested { get; set; }
public DateTime? Started { get; set; }
public DateTime? Finished { get; set; }
public bool IsCurrent { get; set; }
public Guid? ReplyToSpeakerId { get; set; }
public Speaker ReplyToSpeaker { get; set; }
public Guid AgendaItemId { get; set; }
public AgendaItem AgendaItem { get; set; }
public Guid UserId { get; set; }
public ApplicationUser User { get; set; }
}
public class ApplicationUser : IdentityUser<Guid>
{
public string FirstName { get; set; }
public string LastName { get; set; }
public List<MeetingSummoning> MeetingSummonings { get; set; }
public List<Meeting> OwnedMeetings { get; set; }
public List<Meeting> ModeratedMeetings { get; set; }
public List<Meeting> ReportedMeetings { get; set; }
// Lots more properties ...
}

Related

Cascade delete with EF Core and C# (excludes business and thus delete address together)

My class Address:
public class Address
{
public int Id { get; set; }
public string Street { get; set; }
public string ZipCode { get; set; }
public string StreetNumber { get; set; }
public string District { get; set; }
public string Complement { get; set; }
public string City { get; set; }
public string State { get; set; }
public virtual ICollection<Business> Business { get; set; }
}
My class Business:
public class Business
{
public int Id { get; set; }
public string CNPJ { get; set; }
public string SocialReson { get; set; }
public string FantasyName { get; set; }
public string BusinessName { get; set; }
public Priority Priority { get; set; }
public Category Category { get; set; }
public int AddressId { get; set; }
public string MainImage { get; set; }
public virtual Address Address { get; set; }
public virtual ICollection<BusinessPhotos> BusinessPhotos { get; set; }
public virtual ICollection<OpeningHours> OpeningHours { get; set; }
public virtual ICollection<Product> Products { get; set; }
public virtual ICollection<SocialMedia> SocialMedias { get; set; }
}
My Delete method:
public async Task Delete(int id)
{
var business = await _context
.Business
.Include(x => x.Address)
.FirstOrDefaultAsync(x => x.Id == id);
if (business == null)
return;
_context.Business.Remove(business);
await _context.SaveChangesAsync();
}
Even if I leave the FK AddressId as .OnDelete(DeleteBehavior.Cascade), I can't delete the address together.
I'm using .NET 6 and SQL Server.

Reference to application users always returning null even though included

My Club has many members so I have added the following virtual property Members my question is when I want to list the club members shouldn't adding just the virtual load this into lazy loading am using .net 5.0.1 at present.
var members = _context.Clubs.Include(c=>c.Members).Where(w => w.ClubId == tennantId ).ToList();
When I look at var members even when the members have been included it is zero even though the teannatId matches.
public class MSFSAddonDBContext : IdentityDbContext<ApplicationUser>
{
public MSFSAddonDBContext(DbContextOptions<MSFSAddonDBContext> options)
: base(options)
{
}
public DbSet<Club> Clubs { get; set; }
public virtual List<ApplicationUser>? Members { get; set; }
}
This is my controller method.
public void IActionResult ClubMembers()
{
var tennantId = GetTennantId().Result;
var members = _context.Clubs.Include(c=>c.Members).Where(w => w.ClubId == tennantId ).ToList();
return View(members);
}
There are two members references at top level and inside the clubs.
This is the club class
public class Club
{
public int Id { get; set; }
public Guid? TeannatId { get; set; }
public Guid? ClubId { get; set; }
public Guid? UserId { get; set; }
public string? Name { get; set; }
public string? Description { get; set; }
public string? Url { get; set; }
public int? ClubLikes { get; set; }
public int? ClubDislikes { get; set; }
public int? MembersCount { get; set; }
public string? Logo { get; set; }
public List<Flight>? Flights { get; set; }
public string? ThumbNail { get; set; }
public DateTimeOffset? GpdrRemoveRequestDate { get; set; }
public bool? isGpdrRemoveRequest { get; set; }
public DateTimeOffset? BannedTime { get; set; }
public TimeSpan? BanPeriod { get; set; }
public bool? isBanned { get; set; }
public bool? isActive { get; set; }
public bool? isDeleted { get; set; }
public DateTime? CreatedDate { get; set; }
public string? CreatedBy { get; set; }
public virtual List<ApplicationUser>? Members { get; set; }
}
Edit 2
This is the application user class.
public class ApplicationUser : IdentityUser
{
public enum UserTypeEnum
{
Guest=0,
User=1,
Author=2,
AddonOwner=3,
GroupOwner=5,
ClubMember=6,
ClubAdmin=7,
SuperAdmin=199,
BandUser=999
}
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string? GamerTag { get; set; }
public bool? isOnline { get; set; }
public int? UserType { get; set; }
public Guid? TennantId { get; set; }
public Guid? ClubId { get; set; }
public List<Badges>? Badges { get; set; }
}
NB As always with ef the primary keys in all my tables are the Id column thanks

EF Core self referencing many to many

I have User table and I'd like to add connection called UserFriend between 2 users. I've searched a lot and basicly tried many different solutions and none of them worked. Everytime I get same error:
Introducing FOREIGN KEY constraint 'FK_UserFriends_Users_Friend2Id' on table 'UserFriends' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Here are my models:
public class User
{
[Key]
public Guid Id { get; set; }
public string Username { get; set; }
public string EmailAddress { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public virtual ICollection<UserFriend> Friends { get; set; }
public virtual ICollection<UserFriend> FriendOf { get; set; }
}
public class UserFriend
{
public User Friend1 { get; set; }
public Guid Friend1Id { get; set; }
public User Friend2 { get; set; }
public Guid Friend2Id { get; set; }
public bool Confirmed { get; set; }
public DateTime Added { get; set; }
}
And here's code in DataContext:
modelBuilder.Entity<UserFriend>().HasKey(sc => new { sc.Friend1Id, sc.Friend2Id });
modelBuilder.Entity<UserFriend>()
.HasOne(c => c.Friend1)
.WithMany(c => c.FriendOf)
.HasForeignKey(f => f.Friend1Id);
modelBuilder.Entity<UserFriend>()
.HasOne(c => c.Friend2)
.WithMany(c => c.Friends)
.HasForeignKey(f => f.Friend2Id)
.OnDelete(DeleteBehavior.Restrict);
Change your code to below and remove the other lines you have posted.
public class User
{
[Key]
public Guid Id { get; set; }
public string Username { get; set; }
public string EmailAddress { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public virtual ICollection<UserFriend> Friends { get; set; }
public virtual ICollection<UserFriend> FriendOf { get; set; }
}
public class UserFriend
{
public User Friend1 { get; set; }
[ForeignKey("Friend1")]
public Guid? Friend1Id { get; set; }
public User Friend2 { get; set; }
[ForeignKey("Friend2")]
public Guid? Friend2Id { get; set; }
public bool Confirmed { get; set; }
public DateTime Added { get; set; }
}
modelBuilder.Entity<User>();
modelBuilder.Entity<UserFriend>();

How to create a third table for Many to Many relationship in Entity Framework Core?

I have two classes:
One is User
public class User
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public List<Subscription> Subscriptions { get; set; }
}
Other is Subscription:
public class Subscription
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
As you can see that User has a list of Subscriptions.
Now when using the entity framework code first approach I am getting a table for User which doesn't contain Subscriptions but a new column for User Id is being added to Subscription table. I was expecting to have a third table which contains two columns one with User ID and the other with subscription ID.
How can I achieve this?
From documentation:
Many-to-many relationships without an entity class to represent the join table are not yet supported. However, you can represent a many-to-many relationship by including an entity class for the join table and mapping two separate one-to-many relationships.
So this answer is correct.
I just corrected code a little bit:
class MyContext : DbContext
{
public DbSet<Use> Users { get; set; }
public DbSet<Subscription> Subscriptions { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<UserSubscription>()
.HasKey(t => new { t.UserId, t.SubscriptionId });
modelBuilder.Entity<UserSubscription>()
.HasOne(pt => pt.User)
.WithMany(p => p.UserSubscription)
.HasForeignKey(pt => pt.UserId);
modelBuilder.Entity<UserSubscription>()
.HasOne(pt => pt.Subscription)
.WithMany(t => t.UserSubscription)
.HasForeignKey(pt => pt.SubscriptionId);
}
}
public class User
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public List<UserSubscription> UserSubscriptions{ get; set; }
}
public class Subscription
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public List<UserSubscription> UserSubscriptions{ get; set; }
}
public class UserSubscription
{
public int UserId { get; set; }
public User User { get; set; }
public int SubscriptionId { get; set; }
public Subscription Subscription { get; set; }
}
PS. You don't need use virtual in navigation property, because lazy loading still not available in EF Core.
Create a third middle table named: UserSubscriptions for example.
public class User
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public virtual ICollection<UserSubscription> Subscriptions { get; set; }
}
public class Subscription
{
public int ID { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
public class UserSubscription
{
public int ID { get; set; }
public int SubscriptionID { get; set; }
public decimal Price { get; set; }
public int UserID { get; set; }
public virtual User { get; set; }
public DateTime BeginDate { get; set; }
public DateTime EndDate { get; set; }
}
Second Solution:
Add reference for Subscription to User and name it CurrentSubscription for example.
public class User
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public int CurrentSubscriptionID { get; set; }
public virtual Subscription Subscription { get; set; }
}
public class Subscription
{
public int ID { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}

Entity Framework (EF) code first migration creates only some of the tables

I am running my initial EF migration trying to create the database.
But only 3 of my 5 domains are being created as tables in the database. No errors occur during migration.
I try to run SeedData methods on run, but of course it stops when it tries to save data to the table that does not exist.
I am using EF7. I use DNX to run migrations.
Does anyone know why only 3 of the tables are being created?
Here are the 2 tables that did NOT get created:
// Enumclass, this is not a table
public enum MatchTypeEnum
{
Group, RoundOf32, RoundOf16, QuarterFinals, SemiFinals, BronzeFinal, Final
}
public partial class Match
{
[Key]
public Guid MatchId { get; set; }
public DateTime KickOffTime { get; set; }
public int? HomeScore { get; set; }
public int? AwayScore { get; set; }
public Guid HomeTeamId { get; set; }
public Guid AwayTeamId { get; set; }
public MatchTypeEnum MatchType { get; set; }
public Guid? GroupId { get; set; }
public virtual Team HomeTeam { get; set; }
public virtual Team AwayTeam { get; set; }
[ForeignKey("GroupId")]
public virtual Group Group { get; set; }
}
public enum MatchOutcomeEnum
{
H,A,D
}
public partial class Bet
{
[Key]
public Guid BetId { get; set; }
public Guid UserId { get; set; }
public Guid MatchId { get; set; }
public int? HomeScore { get; set; }
public int? AwayScore { get; set; }
public MatchOutcomeEnum? MatchOutcome{ get; set; }
public int? PointsCorrectScore { get; set; }
public int? PointsBonus { get; set; }
public int? MultiplyScoreWith { get; set; }
public int? TotalPointsBet { get; set; }
[ForeignKey("MatchId")]
public virtual Match MatchBettedOn { get; set; }
}
And here are the 3 classes that do get created:
public class Team
{
[Key]
public Guid TeamId { get; set; }
public string TeamName { get; set; }
public string TeamCountry { get; set; }
public Guid GroupId { get; set; }
public string LogoUrl { get; set; }
[ForeignKey("GroupId")]
public virtual Group TeamGroup { get; set; }
}
public enum GroupEnum
{
GroupA, GroupB, GrooupC, GroupD,GroupE,GroupF, GroupG, GroupH, GroupI
}
/// <summary>
/// The group.
/// </summary>
public partial class Group
{
[Key]
public Guid GroupId { get; set; }
public GroupEnum GroupName { get; set; }
public int? NumberTeamsProceed { get; set; }
public virtual ICollection<Team> TeamsInGroup { get; set; }
public virtual ICollection<Match> MatchesInGroup { get; set; }
}
public class League
{
[Key]
public Guid LeagueId { get; set; }
public string LeagueName { get; set; }
public bool Public { get; set; }
public int LeagueNumber { get; set; }
public string LeagueLink { get; set; }
}
The DbContext:
public class CupBettingContext : DbContext
{
public CupBettingContext()
{
Database.EnsureCreated();
}
public DbSet<Group> Groups { get; set; }
public DbSet<Match> Matches { get; set; }
public DbSet<Bet> Bets { get; set; }
public DbSet<League> Leagues { get; set; }
public DbSet<Team> Teams { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var connectionString = Startup.Configuration["Data:CupBettingContextConnection"];
optionsBuilder.UseSqlServer(connectionString);
base.OnConfiguring(optionsBuilder);
}
}
Thank you for your time :)
UPDATE:
It worked after I I removed the two lines from the class Match:
public virtual Team HomeTeam { get; set; }
public virtual Team AwayTeam { get; set; }
Strange that it ran the migration :/
If anyone is still reading this question, how can I add those virtual object without getting an error?

Categories

Resources