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()
{
await _SIMContext.Departments.LoadAsync().ContinueWith(loadTask =>
{
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 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
{
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);
}
}
What I have tried:
dataBindingSource.ResetBindings(false);
var state = _SIMContext.Entry(model).State;
state = EntityState.Modified;