2016-09-26 2 views
0

既存のデータベース項目を変更した後、ListViewに更新された項目を取得できません。しかし、アプリケーションをリロードすると、更新されたアイテムがListViewに表示されます。私はこれが私のインターフェイスEntityフレームワークでコンテキストを更新する方法

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

あるListView

ためObservableCollectionにバインドされている

私はIService IEmployeeServiceDataクラスを実装しています。

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(); 
    } 
} 

これがアイテムを終了私の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); 
     } 
    } 

更新している:entitに行わ

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

+0

'EmployeeMasterData'にnotifyプロパティ変更イベントがありますか? – OmegaMan

+0

はい@OmegaManを実装しました –

+0

'Employee'は' INotifyPropertyChanged'を実装していますか? – OmegaMan

答えて

0

変更2つのインスタンスがであり、この例ではではないため、Entity Frameworkのyは画面に反映されません。はい、同じ値を持っていますが、メモリ内の2つの異なる別のreferenceの場所です。 ** ObservableCollectionはリストのコピーであり、実際のリストはあなたの例では操作されません。

したがって、これらは関連していません。


は、これらのオプションを持って変更を表示するには:

  1. 変更にして行われる変更を反映するために監視可能なコレクションで開催された実際のオブジェクトのプロパティ(IES)をEFエンティティ。また、EFエンティティはINotifyPropertyChangeに準拠する必要があります。また、データプロパティの変更が画面に表示されません。
  2. また、画面エンティティを削除し、変更されたエンティティをリストに追加します。
  3. また、観測可能リスト全体を削除し、EFの最新バージョンで再追加します。 (あなたはこれを行うと言います、それもオプションです)。
関連する問題