实体框架:如何检测对另一个表单上的实体所做的更改?

时间:2014-12-11 13:18:57

标签: entity-framework bindinglist

以下是我们项目的INVOICE课程的一部分:

public abstract class Invoice
{
   public int InvoiceId { get; set; }
   public int Status { get; set; }
   public virtual ObservableCollection<OrderRequestStatus> OrderRequestStatuses { get; set; }
   public EnumType.Follow Follow {get; set; 
   public string FicheNo { get; set; }
   public DateTime Date_ { get; set; }
   public DateTime? DueDate_ { get; set; }
   public DateTime? ShipmentDateEstimated { get; set; }  
   public DateTime? ShipmentDate { get; set; } 
   public string DoCode { get; set; } 
   public int AccountId{ get; set; } 
   public virtual AccountLayer.Account Account { get; set; }
   public int? SourceIndex{ get; set; } 
   public int CurrencyTypeId { get; set; }
   public virtual CommonLayer.CurrencyType CurrencyType { get; set; } 
   public double CurrencyRate { get; set; }
   public CommonLayer.InvoicePricing Price { get; set; }
   public string GenExp1 { get; set; }
   public string GenExp2 { get; set; }
   public string GenExp3 { get; set; }
   public string GenExp4 { get; set; }
   public int? LogoInvoiceLogicalRef { get; set; }
   public CommonLayer.TableLog Added { get; set; }
   public CommonLayer.TableLog Edited { get; set; }
   public CommonLayer.TableLog Cancelled { get; set; }
   public virtual ObservableCollection<StoredProcedures.sp_OtherSalesandOffers> sp_OtherSalesandOffers { get; set; }

 public Invoice()
   {
       OrderRequestStatuses = new ObservableCollection<OrderRequestStatus>();
       sp_OtherSalesandOffers = new ObservableCollection<StoredProcedures.sp_OtherSalesandOffers>();
       Price = new CommonLayer.InvoicePricing();
       Added = new CommonLayer.TableLog();
       Edited = new CommonLayer.TableLog();
       Cancelled = new CommonLayer.TableLog();

   } 

相关上下文代码是:

  public TaksimGroupContext()
        : base("Name=TaksimGroupContext")

    {
        this.Configuration.LazyLoadingEnabled = true;
        this.Configuration.ValidateOnSaveEnabled = true;
        this.Configuration.AutoDetectChangesEnabled = true;
       this.Configuration.ProxyCreationEnabled = true;
    }

我们有两种形式: 1-发票 2-发票细节

表格1 INVOICE表格相关代码

 private void DataBind_grdInvoices()
    {
         (from a in base._Context.Invoices
          orderby a.Date_
          where a.Date_ > DateTime.Today.AddDays(-3650);
          select a
              ).Load();

           grdInvoicess.DataSource = base._Context.Invoices.Local.ToBindingList();
    }

表格2发票详情:

数据加载与上面相同的逻辑

问题是:

我们从中加载INVOICE。然后我们选择一张发票并打开INVOICE DETAULS表格,其中包含发票标题(来自发票类)和发票明细(来自Stline类)

当我们对发票(不是详细信息)进行任何更改时,例如我们更改帐户,然后我们保存更改,INVOICE表单不会检测到这些更改。

编辑:更确切地说,这些变化也未被检测到(我们也不会尝试)也不会反映到网格中。

我们在哪里犯错,或者我们应该遵循哪种其他方法?

提前致谢。

1 个答案:

答案 0 :(得分:0)

base._Context.ObjectStateManager保留上下文中发生的更改,与其编辑的表单无关。

您可以使用base._Context.ObjectStateManager.GetObjectStateEntry获取Invoice对象的ObjectStateEntry,该对象在OriginalValues和CurrentValues属性中保存其原始值和当前值。

您可以使用GetModifiedProperties方法获取已更改的属性的名称

source