-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Closed
Labels
✔️ Resolution: AnsweredResolved because the question asked by the original author has been answered.Resolved because the question asked by the original author has been answered.Status: Resolvedarea-blazorIncludes: Blazor, Razor ComponentsIncludes: Blazor, Razor Componentsquestion
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
I created a validation attribute that uses something like db = (MyDbContext)validationContext .GetService(typeof(MyDbContext)); to get the dbcontext injected, the problem is that it randomly throws an error saying that the dbcontext has been disposed, I injected the service in the constructor and also check if dbcontext is null, and if so, it try to create a new instance of the dbcontext, and sometimes works sometimes it does not... please check my code below and the error received on blazor.
public class ConstraintsV2_Require_Resolution : ValidationAttribute
{
private MyDbContext db { get; set; }
private void CreateContext(ValidationContext validationContext)
{
db = (MyDbContext)validationContext.GetService(typeof(MyDbContext));
}
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
{
FACT_Constraint_v2 fACT_Constraint_V2 = (FACT_Constraint_v2)validationContext.ObjectInstance;
if (value == null)
{
if (db == null) CreateContext(validationContext); //db context
//------------------------- NEXT LINE IS WHERE ERROR IS PRODUCED
DIM_STATUS currentStatus = db.Dim_Statuses.FirstOrDefault(f => f.status_guid == fACT_Constraint_V2.status_guid);
//-------------------------
if (currentStatus != null)
{
if (currentStatus.status.Contains("COMPLETE", StringComparison.CurrentCultureIgnoreCase))
{
return new ValidationResult("If Status is \"COMPLETE\" a Resolution must be provided.", new List<string>() { validationContext.MemberName });
}
}
}
return ValidationResult.Success;
}
}
Expected Behavior
Expected behavior should be to keep the instance of dbcontext I made, not to return a disposed object.
Steps To Reproduce
No response
Exceptions (if any)
.NET Version
7
Anything else?
No response
Metadata
Metadata
Assignees
Labels
✔️ Resolution: AnsweredResolved because the question asked by the original author has been answered.Resolved because the question asked by the original author has been answered.Status: Resolvedarea-blazorIncludes: Blazor, Razor ComponentsIncludes: Blazor, Razor Componentsquestion

