C
C#3mo ago
WAASUL

✅ Ef Core unexpected query behaviour

var memberships = await db.OrganizationMembers.AsTracking()
.Include(om => om.Organization)
.Where(om => om.UserId == user.Id && om.InvitationStatus == InvitationStatus.Approved &&
om.Role > MemberRole.None)
.ToListAsync(cancellationToken);
var memberships = await db.OrganizationMembers.AsTracking()
.Include(om => om.Organization)
.Where(om => om.UserId == user.Id && om.InvitationStatus == InvitationStatus.Approved &&
om.Role > MemberRole.None)
.ToListAsync(cancellationToken);
/// <summary>
/// Represents an enumeration of member roles.
/// </summary>
public enum MemberRole
{
/// <summary>
/// Represents the None role.
/// </summary>
None,

/// <summary>
/// Represents the Read role.
/// </summary>
Read,

/// <summary>
/// Represents the Write role.
/// </summary>
Write,

/// <summary>
/// Represents the Administrator role.
/// </summary>
Admin
}
/// <summary>
/// Represents an enumeration of member roles.
/// </summary>
public enum MemberRole
{
/// <summary>
/// Represents the None role.
/// </summary>
None,

/// <summary>
/// Represents the Read role.
/// </summary>
Read,

/// <summary>
/// Represents the Write role.
/// </summary>
Write,

/// <summary>
/// Represents the Administrator role.
/// </summary>
Admin
}
For some reason it's not including MemberRole.admin. It's including MemberRole.Write?
27 Replies
WAASUL
WAASULOP3mo ago
After changing the comparison statement to om.Role != MemberRole.None all of a sudden it's working. I would still like to understand why though?
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
Anton
Anton3mo ago
That is how it works unless you copied MemberRole or the check wrong
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
Anton
Anton3mo ago
I kind of disagree. It makes sense if you can order the roles like this by the magnitude of privilege. It may make sense to wrap it in a struct to denote which operations are supposed to be used, because it may not be obvious that the members in the enum are ordered accordingly
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX3mo ago
TeBeCo
else nothing's wrong out of the ordinary you need to debug your data and see what's going on
Quoted by
React with ❌ to remove this embed.
WAASUL
WAASULOP3mo ago
I thought it would just compare the value assigned to the enum which is an int. So it sort of made sense. I guess om.Role != MemberRole.None makes more sense.
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
WAASUL
WAASULOP3mo ago
I do have a conversion though which might have led to the issue.
builder.Property(e => e.Role)
.HasConversion(
v => v.ToString(),
v => Enum.Parse<MemberRole>(v))
.IsRequired();
builder.Property(e => e.Role)
.HasConversion(
v => v.ToString(),
v => Enum.Parse<MemberRole>(v))
.IsRequired();
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
WAASUL
WAASULOP3mo ago
I see what you mean. Very good point.
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
WAASUL
WAASULOP3mo ago
It's a bit easier to read on the database side. Since I'm doing a lot of debuging at this stage.
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
WAASUL
WAASULOP3mo ago
Not really no. 🤣
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
Anton
Anton3mo ago
you can make a table for roles and join with it when you need a human readable output
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
Anton
Anton3mo ago
that's what people usually do
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
WAASUL
WAASULOP3mo ago
To be honest, I'm not very familiar with sql directly. I don't have a lot of knowledge in that field. That's why I'm using ef core.
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
WAASUL
WAASULOP3mo ago
Oh I see. Thank you guys very much for the input. I learned some new stuffs.
Anton
Anton3mo ago
don't avoid learning SQL
WAASUL
WAASULOP3mo ago
I have always wanted to learn. I just forget the syntax for some reason. 🤣
MODiX
MODiX3mo ago
If you have no further questions, please use /close to mark the forum thread as answered
Want results from more Discord servers?
Add your server