How to force Entity Framework to always get updated data from the database?

前端 未结 5 1865
[愿得一人]
[愿得一人] 2020-12-07 21:44

I am using EntityFramework.Extended library to perform batch updates. The only problem is EF does not keep track of the batch updates performed by the library. So when I que

相关标签:
5条回答
  • 2020-12-07 22:13

    I stumbled upon this question while searching for a solution to a problem I was having where the navigation properties were not populating after updating the entity. Whenever I attempted to reload the entity from the database, it would grab the entry from the local store instead which would not populate the navigation properties via lazy loading. Instead of destroying the context and recreating one, I found this allowed me to get fresh data with the proxies working:

    _db.Entry(entity).State = EntityState.Detached;
    

    The logic behind it was - my update attached the entity so it would track changes to it. This adds it to the local store. Thereafter, any attempts to retrieve the entity with functional proxies would result in it grabbing the local one instead of going out to the db and returning a fresh, proxy-enabled entity. I tried the reload option above, which does refresh the object from the database, but that doesn't give you the proxied object with lazy-loading. I tried doing a Find(id), Where(t => t.Id = id), First(t => t.Id = id). Finally, I checked the available states that were provided and saw there was a "Detached" state. Eureka! Hope this helps someone.

    0 讨论(0)
  • 2020-12-07 22:31

    Making the code run on the same context will not yield you updated entities. It will only append new entities created in the database between runs. EF force reload can be done like this:

    ObjectQuery _query = Entity.MyEntity;
    _query.MergeOption = MergeOption.OverwriteChanges;
    var myEntity = _query.Where(x => x.Id > 0).ToList();
    
    0 讨论(0)
  • 2020-12-07 22:32

    For me ... I access my DbContext like this:

    _viewModel.Repo.Context
    

    To force EF to hit the database I do this:

    _viewModel.Repo.Context = new NewDispatchContext();
    

    Overwriting the current DbContext with a new instance. Then the next time I use my data services they get the data from the database.

    0 讨论(0)
  • 2020-12-07 22:33

    Please try this to refresh a single entity:

    Context.Entry<T>(entity).Reload()
    

    Edit: To get fresh data for a collection of entities is worth trying to dispose the DbContext instance after each request.

    0 讨论(0)
  • 2020-12-07 22:35

    I declared the entity variable, without assignment, as part of the class. This allowed me to dispose of an instance without losing the variable for reference by other methods. I just came across this so it doesn't have alot of runtime under it's belt, but so far it seems to be working fine.

    public partial class frmMyForm
    {
        private My_Entities entity;
    
        public frmMyForm()
        {
            InitializeComponent();
        }
    
        private void SomeControl_Click(object sender, EventArgs e)
        {
            db.SaveChanges();
            db.Dispose();
            entity = new My_Entities();
            //more code using entity ...
    }
    
    0 讨论(0)
提交回复
热议问题