简体   繁体   中英

How to refresh Context in Entity framework

I cannot get updated item in ListView after modifying existing database item. Though, once I reload the application one can see updated item in ListView .

I have binded to an ObservableCollection for the ListView

This is my interface

   public interface IService
{
    IEnumerable<Employee> GetDetails();
    IEnumerable<Employee> GetDetailsById(int MatchID);
}

I have implemented IService IEmployeeServiceData class.

    public class IEmployeeServiceData:IService
{
    private EmployeeContext Context
    {
        get;
        set;
    }

    public IEmployeeServiceData()
    {
        Context = new EmployeeContext();
    }

    public IEnumerable<Model.Employee> GetDetails()
    {
        return Context.Employees;
    }

    public IEnumerable<Model.Employee> GetDetailsById(int MatchID)
    {
        var q = from f in Context.Employees
                where f.EMPLOYEE_ID == MatchID
                select f;
        return q.AsEnumerable();
    }
}

This is my VM

    public void RefereshData()
    {

        var e = EmployeeService.GetDetails();

        if (SelectedIndexValue == 1)
        {
            var Data = from e1 in e
                       where e1.LOCATION == "Pune"
                       select e1;
            EmployeeMasterData = new ObservableCollection<Model.Employee>(Data);
        }

        else if(SelectedIndexValue==2)
        {
            var Data = from e1 in e
                       where e1.LOCATION == "Bangalore"
                       select e1;
            EmployeeMasterData = new ObservableCollection<Model.Employee>(Data);
        }

        else
        {
            EmployeeMasterData = new ObservableCollection<Model.Employee>(e);
        }
    }

Updating Exiting Item:

 public void UpdateEmployee()
    {
        try
        {

            Context = new EmployeeContext();
            Employee Emp = Context.Employees.First(i => i.EMPLOYEE_ID == FindId1);
            Emp.FIRST_NAME = this.FIRSTNAME;
            Emp.FAMILY_NAME = this.FAMILY_NAME;
            Emp.EXTERNAL_ID = this.EXTERNALID;
            Emp.DB_SPONSOR = this.DBSPONSOR;
            Emp.LEGAL_ENTITY = this.LEGAL_ENTITY;
            Emp.COST_CENTER = this.COST_CENTER1;
            Emp.STATUS = this.StatusCategory;
            Emp.ENTRY_DATE = this.ENTRYDATE;
            Emp.LOCATION = this.LocationCategory1;
            Context.SaveChanges();
            Clear();
            AlertMessage1 = "Employee Record is Updated Sucessfulyy !!!";
            IsVisible1 = true;
            timer.Start();

        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.InnerException);
        }
    }

Existing Item

Updated Item

Changes done to an entity in Entity Framework will not be reflected on screen because the two instances are not related in your example. Yes the have the same values, but they are two different distinct reference locations in memory. **For the ObservableCollection is a copy of the list and not the actual list being manipulated in your example.

Hence they are not related.


To show a change you have these options:

  1. Change the actual object 's property(ies) held by the observable collection to mirror the change done in to the other EF entity. Also the EF entity must adhere to INotifyPropertyChange or also the data property change won't be seen on the screen.
  2. Or delete the screen entity and add the changed entity into the list.
  3. Or delete the whole observable list and re-add it with the latest version from EF. (You mention that you do this and that is an option as well).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM