private void button1_Click(object sender, EventArgs e) { listBox1.Items.Clear(); //EntityState.Added foreach (var entry in context.ObjectStateManager .GetObjectStateEntries(EntityState.Added)) { string NewEmplNo, NewEmplName; NewEmplNo = ((Transfer)(entry.Entity)).TransferID; NewEmplName = ((Transfer)(entry.Entity)).Description; listBox1.Items.Add(String.Format( "員工代碼{0}為Added,目前的員工名稱:{1} ", NewEmplNo, NewEmplName)); } //EntityState.Modified foreach (var entry in context.ObjectStateManager .GetObjectStateEntries(EntityState.Modified)) { string NewEmplName, OldEmplName, OrginalEmplNo; //取得員工名稱目前值 CurrentValueRecord curr = entry.CurrentValues; NewEmplName = (string)(curr.GetValue(curr.GetOrdinal("Description"))); //取得員工名稱原來值 OriginalValueRecord org = entry.GetUpdatableOriginalValues(); OldEmplName = (string)(org.GetValue(org.GetOrdinal("Description"))); OrginalEmplNo = ((Transfer)(entry.Entity)).TransferID; listBox1.Items.Add(String.Format("員工代碼{0}為Modified, " + "目前的員工名稱:{1},原來的員工名稱:{2} ", OrginalEmplNo, NewEmplName, OldEmplName)); } //EntityState.Deleted foreach (var entry in context.ObjectStateManager .GetObjectStateEntries(EntityState.Deleted)) { string OldEmplNo, OldEmplName; OldEmplNo = ((Transfer)(entry.Entity)).TransferID; OldEmplName = ((Transfer)(entry.Entity)).Description; listBox1.Items.Add(String.Format("員工代碼{0}為Deleted, " + "原來的員工名稱:{1} ", OldEmplNo, OldEmplName)); } //EntityState.Unchanged foreach (var entry in context.ObjectStateManager .GetObjectStateEntries(EntityState.Unchanged)) { string currEmplNo, currEmplName; currEmplNo = ((Transfer)(entry.Entity)).TransferID; currEmplName = ((Transfer)(entry.Entity)).Description; listBox1.Items.Add(String.Format("員工代碼{0}為Unchanged, " + "目前的員工名稱:{1} ", currEmplNo, currEmplName)); } }
private int GetPropertyIndex(ObjectStateEntry entry, string propertyName) { OriginalValueRecord record = entry.GetUpdatableOriginalValues(); for (int i = 0; i != record.FieldCount; i++) { FieldMetadata metaData = record.DataRecordInfo.FieldMetadata[i]; if (metaData.FieldType.Name == propertyName) { return(metaData.Ordinal); } } return(-1); }
void IDataServiceUpdateProvider.SetConcurrencyValues(object resourceCookie, bool?checkForEquality, IEnumerable <KeyValuePair <string, object> > concurrencyValues) { if (checkForEquality == null) { throw new DataServiceException(500, "Cannot perform operation without ETag."); } if (!checkForEquality.Value) { throw new DataServiceException(500, "IfNoneMatch header not supported in update and delete."); } ObjectStateEntry objectStateEntry = CurrentDataSource.ObjectContext.ObjectStateManager.GetObjectStateEntry(resourceCookie); OriginalValueRecord originalValues = objectStateEntry.GetUpdatableOriginalValues(); foreach (KeyValuePair <string, object> etag in concurrencyValues) { int propertyOrdinal = originalValues.GetOrdinal(etag.Key); originalValues.SetValue(propertyOrdinal, etag.Value); } }
private static void PopulateComplexType(OriginalValueRecord originalValues, string propertyName, object originalValue, ComplexType complexType) { var ordinal = originalValues.GetOrdinal(propertyName); originalValues = (OriginalValueRecord)originalValues.GetValue(ordinal); foreach (var cp in complexType.Properties) { var value = Server.Helper.GetMemberValue(originalValue, cp.Name); if (cp.TypeUsage.EdmType is ComplexType loopComplexType) { PopulateComplexType(originalValues, cp.Name, value, loopComplexType); } else { var loopOrdinal = originalValues.GetOrdinal(cp.Name); originalValues.SetValue(loopOrdinal, value); } } }
private void context_SavingChanges(Object sender, EventArgs e) { string NewProductID, OldProductID; int NewQuantity, OldQuantity; //取得所有新增的存貨異動單明細 foreach (var AddedEntry in context.ObjectStateManager .GetObjectStateEntries(EntityState.Added)) { if (!AddedEntry.IsRelationship) { if (AddedEntry.Entity is TransferDetails) { NewProductID = ((TransferDetails)(AddedEntry.Entity)).ProductID; //判斷異動別 if (transferTypeTextBox.Text == "1") { //異動別=入庫,要更新庫存量 XIN.IncStock(context, NewProductID, ((TransferDetails)(AddedEntry.Entity)).Quantity); } else { //異動別=出庫,要更新庫存量 XIN.DecStock(context, NewProductID, ((TransferDetails)(AddedEntry.Entity)).Quantity); } } } } //取得所有修改的存貨異動單明細 foreach (var ModifiedEntry in context.ObjectStateManager .GetObjectStateEntries(EntityState.Modified)) { if (!ModifiedEntry.IsRelationship) { if (ModifiedEntry.Entity is TransferDetails) { //取得目前的商品編號與數量 CurrentValueRecord curr = ModifiedEntry.CurrentValues; NewProductID = (string)(curr.GetValue( curr.GetOrdinal("ProductID"))); NewQuantity = (int) (curr.GetValue(curr.GetOrdinal("Quantity"))); //取得原來的商品編號與數量 OriginalValueRecord org = ModifiedEntry.GetUpdatableOriginalValues(); OldProductID = (string) (org.GetValue(curr.GetOrdinal("ProductID"))); OldQuantity = (int)(org.GetValue(curr.GetOrdinal("Quantity"))); //判斷異動別 if (transferTypeTextBox.Text == "1") { //異動別=入庫 //增加目前的商品編號的庫存量 XIN.IncStock(context, NewProductID, NewQuantity); //減少原來的商品編號的庫存量 XIN.DecStock(context, OldProductID, OldQuantity); } else { //異動別=出庫 //增加原來的商品編號的庫存量 XIN.IncStock(context, OldProductID, OldQuantity); //減少目前的商品編號的庫存量 XIN.DecStock(context, NewProductID, NewQuantity); } } } } //取得所有刪除的存貨異動單明細 foreach (var DeletedEntry in context.ObjectStateManager .GetObjectStateEntries(EntityState.Deleted)) { if (!DeletedEntry.IsRelationship) { if (DeletedEntry.Entity is TransferDetails) { OldProductID = ((TransferDetails) (DeletedEntry.Entity)).ProductID; //判斷異動別 if (DeleteTransferType == "1") { //異動別=入庫,更新庫存量 XIN.DecStock(context, OldProductID, ((TransferDetails)(DeletedEntry.Entity)).Quantity); } else { //異動別=出庫,更新庫存量 XIN.IncStock(context, OldProductID, ((TransferDetails)(DeletedEntry.Entity)).Quantity); } } } } }
private void context_SavingChanges(Object sender, EventArgs e) { string NewProductID, OldProductID; int NewQuantity, OldQuantity; //取得所有新增的出貨單明細 foreach (var AddedEntry in context.ObjectStateManager .GetObjectStateEntries(EntityState.Added)) { if (!AddedEntry.IsRelationship) { if (AddedEntry.Entity is DeliveryDetails) { NewProductID = ((DeliveryDetails)(AddedEntry.Entity)).ProductID; //判斷出貨別 if (deliveryTypeTextBox.Text == "1") { //出貨別=出貨,要更新庫存量與更新商品的最近出貨日 //更新庫存量 XIN.DecStock(context, NewProductID, ((DeliveryDetails)(AddedEntry.Entity)).Quantity); //更新商品的最近出貨日 var qry = (from P in context.Product where P.ProductID == NewProductID select P).FirstOrDefault(); qry.LastDeliveryDate = Convert.ToDateTime( deliveryDateTextBox.Text); } else { //出貨別=出貨退回,只要更新庫存量 XIN.IncStock(context, NewProductID, ((DeliveryDetails)(AddedEntry.Entity)).Quantity); } } } } //取得所有修改的出貨單明細 foreach (var ModifiedEntry in context.ObjectStateManager .GetObjectStateEntries(EntityState.Modified)) { if (!ModifiedEntry.IsRelationship) { if (ModifiedEntry.Entity is DeliveryDetails) { //取得目前的商品編號與數量 CurrentValueRecord curr = ModifiedEntry.CurrentValues; NewProductID = (string)(curr.GetValue( curr.GetOrdinal("ProductID"))); NewQuantity = (int) (curr.GetValue(curr.GetOrdinal("Quantity"))); //取得原來的商品編號與數量 OriginalValueRecord org = ModifiedEntry.GetUpdatableOriginalValues(); OldProductID = (string) (org.GetValue(curr.GetOrdinal("ProductID"))); OldQuantity = (int)(org.GetValue(curr.GetOrdinal("Quantity"))); //判斷出貨別 if (deliveryTypeTextBox.Text == "1") { //出貨別=出貨,要更新庫存量與更新商品的最近出貨日 //增加原來的商品編號的庫存量 XIN.IncStock(context, OldProductID, OldQuantity); //減少目前的商品編號的庫存量 XIN.DecStock(context, NewProductID, NewQuantity); //更新商品的最近出貨日 var qry = (from P in context.Product where P.ProductID == NewProductID select P).FirstOrDefault(); qry.LastDeliveryDate = Convert.ToDateTime( deliveryDateTextBox.Text); } else { //出貨別=出貨退回,只要更新庫存量 //增加目前的商品編號的庫存量 XIN.IncStock(context, NewProductID, NewQuantity); //減少原來的商品編號的庫存量 XIN.DecStock(context, OldProductID, OldQuantity); } } } } //取得所有刪除的出貨單明細 foreach (var DeletedEntry in context.ObjectStateManager .GetObjectStateEntries(EntityState.Deleted)) { if (!DeletedEntry.IsRelationship) { if (DeletedEntry.Entity is DeliveryDetails) { OldProductID = ((DeliveryDetails) (DeletedEntry.Entity)).ProductID; //判斷出貨別 if (DeleteDeliveryType == "1") { //出貨別=出貨,要更新庫存量 //更新庫存量 XIN.IncStock(context, OldProductID, ((DeliveryDetails)(DeletedEntry.Entity)).Quantity); } else { //出貨別=出貨退回,只要更新庫存量 XIN.DecStock(context, OldProductID, ((DeliveryDetails)(DeletedEntry.Entity)).Quantity); } } } } }