I have this code in my "Department Form"
private readonly SIMContext _SIMContext;
private Department model;
public Frm_Department(SIMContext SIMContext)
{
InitializeComponent();
_SIMContext = SIMContext;
}
And this is the method that get data from database I added to load event
I have BindingSource component "dataBindingSource" that assign to gridControl DataSource
private async Task LoadDataAsync()
{
// Call the LoadAsync method to asynchronously get the data for the given DbSet from the database.
await _SIMContext.Departments.LoadAsync().ContinueWith(loadTask =>
{
// Bind data to control when loading complete
dataBindingSource.DataSource = _SIMContext.Departments.Local.ToBindingList();
}, TaskScheduler.FromCurrentSynchronizationContext());
}
all my CRUD operations work fine (using DbContext).
Now to the problem:
If I add records directly(Manually) to the database (not using DbContext) and if I call LoadDataAsync method I get all records without any problem.
But when I edit or remove any record(Manually) (not using DbContext) and then call LoadDataAsync method I get old values not updated one.
How can I resolve this problem?.
I try:
dataBindingSource.ResetBindings(false);
var state = _SIMContext.Entry(model).State;
state = EntityState.Modified;
but without any success.
I am using EF Core 3.1.15 with .Net 4.7.2
Update:
This is my DbContext class
public partial class SIMContext : DbContext
{
public SIMContext()
{
}
public SIMContext(DbContextOptions<SIMContext> options)
: base(options)
{
}
public virtual DbSet<Department> Departments { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["SIMContext"].ConnectionString);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new DepartmentConfiguration());
}
}
and this is how I inject DbContext using AutoFac
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
public static IContainer Container;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Container = Configure();
Application.Run(new XtraMain(Container.Resolve<SIMContext>()));
}
static IContainer Configure()
{
var builder = new ContainerBuilder();
builder.RegisterType<SIMContext>().AsSelf();
builder.RegisterType<XtraMain>();
return builder.Build();
}
}
And This is the main form:
public partial class XtraMain : DevExpress.XtraBars.Ribbon.RibbonForm
{
private readonly SIMContext _SIMContext;
public XtraMain(SIMContext SIMContext)
{
InitializeComponent();
_SIMContext = SIMContext;
}
private void bbtnDepartment_ItemClick(object sender, ItemClickEventArgs e)
{
Frm_Department frme = new(_SIMContext)
{
Name = "Frm_Department"
};
ViewChildForm(frme);
}
}
I think the problem is with the injection DbContext method.
In this way DbContext is alive for the entire lifetime of an application and if any change(update,Remove) happens outside of DbContext then I end up with stale data and do more coding to reload new data.
I think the problem will be solved if injected DbContext will disposed after each request