Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

change made by _context.SaveChangesAsync written to DB, but on next page, changes are not visible until refresh

$
0
0

I work on a .NET 6 project based on Blazor and EF core, which saves a lot of work for me. However, I faced a problem. The app has many pages. In the first page, the central servce (containing all the EF and database related stuff) is injected (in the .razor file). Once I perform a database insert / update via Add / Update function, and run SaveChangesAsync, the data is written to the DB, I can see the change in DBeaver. So far, it is fine.

However, if I navigate to the next page, displaying (listing) the records manipulated in the first page, I see only some default state of that paricular row (it is added, but the properties are not filled correctly, e.g. if I have an amount property, it is displayed as 0, instead of the real value, which I see in the DB correctly). Then if I refresh the page, all the correct prop values appear.

The second page injects the central service as well. The central sevice injects the context via constructor DI.

I tried to debug why it happenes, but couldn't catch the problem.In the OnInitializedAsync funct of the second page that record appears with the default values indeed, and didn't find the reason.

I would be thankful for any idea or hint, because EF helps really a lot, and this is the only reason I'm blocked with the project now.

Thank you!

Zsolt

I have tried to change the lifespan of the central service in Program.cs, but didn't see any change.

[Edit]

Program.cs

// add & configure DB CONTEXTbuilder.Services.AddDbContext<StContext>(options =>options.UseOracle(builder.Configuration.GetConnectionString("StConnString") ?? throw new InvalidOperationException("Connection string 'StConnString' not found.")));//builder.Services.AddRazorPages();builder.Services.AddServerSideBlazor();builder.Services.AddScoped<ImpregnationService>();

Page1, .razor

@inject ImpregnationService ImpregService

Page1, code behind:

// apply changesforeach (var record in recordsToSelectedCharges){    try    {        ImpregService._context.ImpregnalasNew.Update(record);        ImpregService._context.SaveChangesAsync();        ShowNotification(new NotificationMessage { Severity = NotificationSeverity.Info, Summary = "OK", Detail = $"" });    }    catch (Exception ex)    {        ShowNotification(new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "NOK", Detail = $"{ex.Message}" });    }}

Page2, razor view:

@inject ImpregnationService ImpregService

Page2, code behind

protected override async Task OnInitializedAsync(){    AuthOk = true;    LotsWithoutPredryWaitingForImpregStart = await ImpregService.GetAmountsDataFilteredAsync(0, 0, 0);    ChamberList = await ImpregService.GetChamberListAsync();    NextStepNavUrl = navigationManager.BaseUri + $"startpreparedimpregnation/";}

Service, DI:

public class ImpregnationService{    public StContext _context { get; set; }    public IConfiguration _configuration { get; set; }    public AppAuthService _authService { get; set; }    public ImpregnationService(StContext context, IConfiguration configuration, AppAuthService authService)    {        _configuration = configuration;        _authService = authService;        _context = context;    }

Service, the relevant function:

public async Task<List<ImpregnalasNew>?> GetAmountsDataFilteredAsync(int mustPredry, int predryProcStarted, int impregProcStarted){    var list = _context.ImpregnalasNew.Where(p => p.BPredry == false && p.BImpregStarted == false).OrderByDescending(o => o.DateCreated).ToArray();    return list;}

Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>