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

Scoped service instances in Blazor

$
0
0

I am new to Blazor. I'm creating a simple object to get familiar with it. Using Entity Framework with SQL Server as back end.

I created a customer class that has orders in it. Created a ICustomerService, IOrderService to perform CRUD operations on these entities which have access to the DbContext to access/update/add items to the database.

I've registered these services as scoped:

services.AddScoped<IOrderService, OrderService>();

Customer object is like this:

public class Customer{    ICollection<Order> Orders = { get; set; } = new List<Order>();}

Customer details component displays list of orders and has a button when clicked on it takes them to Order details page. Or clicking on ordered takes them to details page.

Order object is created as

Order = OrderService.GetOrder(orderId); 

OrderService is injected here.

Order object is bound to an EditForm.

The user updates some fields of the order in the component/page and instead of saving navigates to customer details page where in order list -> corresponding order shows the updated values which shouldn’t as the user is navigated away from the order details page without saving.

I understand this behavior is due to scoped instance as it is retaining the changes in memory.

How to fix this so orders in customer details page displays the values that are in the database not in memory?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



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