My problem is when I perform CRUD operations on my context, the changes are not made in my components.
DbContext is registered as follows:
services.AddDbContextFactory<DBContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("my_ConnectionStrings")),
lifetime: ServiceLifetime.Transient);
In the above code, I configure ServiceLifetime as Transient. So a new instance of the service must be created every time it is requested.
Here is my code:
[Inject] IDbContextFactory<DBContext> DbFactory;
private string updateMessage(int messageID)
{
DBContext _context = DbFactory.CreateDbContext();
var result = _context.Messages.FirstOrDefault(s => s.MessageID == messageID);
if (result != null)
{
result.LastUpdate= DateTime.Now.ToString("HH:mm:ss");
_context.SaveChanges();
}
var assert = _context.Messages.FirstOrDefault(s => s.MessageID == messageID);
return "Last update is: " + assert.LastUpdate;
}
In the above code, I inject the factory into my component and create new instances. But the context changes are not considered in my component. I want to observe the context changes in my components.
In the above code, I modify the context, but nothing has changed and the method returns the previous value of LastUpdate.