private void ApplyValue(DataRow dataRow, EntityObject entityObject) { var properties = entityObject.GetType().GetProperties() .Where(c => (!typeof(IList).IsAssignableFrom(c.PropertyType) && !typeof(EntityObject).IsAssignableFrom(c.PropertyType)) && !typeof(EntityKey).IsAssignableFrom(c.PropertyType)); foreach (var property in properties) { if (dataRow.Table.Columns[property.Name] != null) { dataRow[property.Name] = entityObject.GetValue(property.Name); } } }
public static string GetEntitySetName(this ObjectContext context, EntityObject entity) { Type entityType = ObjectContext.GetObjectType(entity.GetType()); if (entityType == null) { throw new InvalidOperationException("not an entity"); } EntityContainer container = context.MetadataWorkspace.GetEntityContainer(context.DefaultContainerName, DataSpace.CSpace); return container.BaseEntitySets .Where(anEntitySet => anEntitySet.ElementType.Name.Equals(entityType.Name)) .Select(anEntitySet => anEntitySet.Name) .Single(); }
public static string GetEntitySetFullName(this ObjectContext context, EntityObject entity) { // If the EntityKey exists, simply get the Entity Set name from the key if (entity.EntityKey != null) { return entity.EntityKey.EntitySetName; } else { string entityTypeName = entity.GetType().Name; var container = context.MetadataWorkspace.GetEntityContainer(context.DefaultContainerName, DataSpace.CSpace); string entitySetName = (from meta in container.BaseEntitySets where meta.ElementType.Name == entityTypeName select meta.Name).First(); return container.Name + "." + entitySetName; } }
private void CreateRow(EntityObject entityObject, DataRow parentRow, Dictionary<EntityKey, EntityState> states, DataSet dataset) { var tableName = entityObject.GetType().Name; var dataTable = dataset.Tables[tableName]; var state = GetState(entityObject, states, parentRow); DataRow dataRow = null; if (state == EntityState.Added) { dataRow = dataTable.NewRow(); ApplyValue(dataRow, entityObject); dataTable.Rows.Add(dataRow); ApplyRelationValue(dataRow, parentRow, dataset); if (parentRow != null && parentRow.RowState == DataRowState.Unchanged) { parentRow.SetModified(); } } else { dataRow = dataTable.Rows.Find(entityObject.EntityKey.EntityKeyValues.Select(c => c.Value).ToArray()); if (dataRow != null) { if (state == EntityState.Deleted) { dataRow.Delete(); } else { ApplyValue(dataRow, entityObject); } } } var navigationProperties = entityObject.GetType().GetProperties().Where(c => typeof(IList).IsAssignableFrom(c.PropertyType)); foreach (var navigationProperty in navigationProperties) { var navigationValue = (IList)navigationProperty.GetValue(entityObject, null); foreach (EntityObject childObject in navigationValue) { CreateRow(childObject, dataRow, states, dataset); } } }
public static string GetTableName(EntityObject entity) { string tableName = entity.GetType().Name; //T_FB_CHARGEAPPLYMASTER entityCharge = entity as T_FB_CHARGEAPPLYMASTER; //if (entityCharge != null) //{ // entityCharge.T_FB_EXTENSIONALORDERReference.Load(); // entityCharge.T_FB_EXTENSIONALORDER.T_FB_EXTENSIONALTYPEReference.Load(); // if (entityCharge.T_FB_EXTENSIONALORDER.T_FB_EXTENSIONALTYPE.EXTENSIONALTYPECODE == "CCBX") // { // tableName = typeof(T_FB_TRAVELEXPAPPLYMASTER).Name; // } //} return tableName; }
public static void Insert(EntityObject item) { EntityModelContainer context = GetContext(); if (item.EntityState != EntityState.Added) context.AddObject(item.GetType().Name + "s", item); }
/// <summary> /// 获取传入实体的表名/实体类名 /// </summary> /// <param name="entity"></param> /// <returns></returns> private static string GetTableName(EntityObject entity) { string tableName = entity.GetType().Name; return tableName; }
public static void SetEntityKey(this ObjectContext context, EntityObject entity) { entity.EntityKey = context.CreateEntityKey(EntityHelper.GetEntitySetName(entity.GetType(), context), entity); }
public static void AttachAsModified(this ObjectContext context, EntityObject current, EntityObject original) { if (context == null) { throw new ArgumentNullException("context"); } if (current == null) { throw new ArgumentNullException("current"); } if (original == null) { throw new ArgumentNullException("original"); } if (current.EntityState != EntityState.Detached) { context.Detach(current); } Type entityType = current.GetType(); EntityType type = EntityHelper.GetEntityType(context, entityType); PropertyInfo[] source = type.Properties.Select<EdmProperty, PropertyInfo>(delegate(EdmProperty p) { return entityType.GetProperty(p.Name); }).Where<PropertyInfo>(delegate(PropertyInfo p) { return (p != null); }).ToArray<PropertyInfo>(); PropertyDescriptor[] descriptorArray = type.NavigationProperties.Select<NavigationProperty, PropertyDescriptor>(delegate(NavigationProperty p) { return TypeDescriptor.GetProperties(entityType)[p.Name]; }).Where<PropertyDescriptor>(delegate(PropertyDescriptor p) { return !typeof(IEnumerable).IsAssignableFrom(p.PropertyType); }).ToArray<PropertyDescriptor>(); PropertyDescriptor[] descriptorArray2 = descriptorArray.Select<PropertyDescriptor, PropertyDescriptor>(delegate(PropertyDescriptor p) { return EntityHelper.GetReferenceProperty(p); }).ToArray<PropertyDescriptor>(); object[] objArray = source.Select<PropertyInfo, object>(delegate(PropertyInfo p) { return p.GetValue(current, null); }).ToArray<object>(); EntityKey[] keyArray = descriptorArray2.Select<PropertyDescriptor, EntityReference>(delegate(PropertyDescriptor p) { return (EntityReference)p.GetValue(current); }).Select<EntityReference, EntityKey>(delegate(EntityReference p) { if (p == null) { return null; } return p.EntityKey; }).ToArray<EntityKey>(); for (int i = 0; i < source.Length; i++) { source[i].SetValue(current, source[i].GetValue(original, null), null); } for (int j = 0; j < descriptorArray2.Length; j++) { EntityReference reference = (EntityReference)descriptorArray2[j].GetValue(current); EntityReference reference2 = (EntityReference)descriptorArray2[j].GetValue(original); EntityKey entityKey = reference2.EntityKey; EntityObject obj2 = (EntityObject)descriptorArray[j].GetValue(current); if ((obj2 != null) && (obj2.EntityKey != entityKey)) { descriptorArray[j].SetValue(current, null); } reference.EntityKey = entityKey; } context.Attach(current); for (int k = 0; k < source.Length; k++) { PropertyInfo info2 = source[k]; object objA = objArray[k]; object objB = info2.GetValue(original, null); if (!object.Equals(objA, objB)) { info2.SetValue(current, objA, null); } } for (int m = 0; m < descriptorArray2.Length; m++) { EntityReference reference3 = (EntityReference)descriptorArray2[m].GetValue(current); EntityKey key2 = keyArray[m]; if (!object.Equals(reference3.EntityKey, key2)) { reference3.EntityKey = key2; } } }
private void DoEntityCollectionFunction(EntityObject masterObject, EntityObject detailObject, EntityCollectionFunction functionName) { PropertyInfo detailObjectPropertyInfo = masterObject.GetType().GetProperty(this.Command.EntitySetName); detailObjectPropertyInfo.PropertyType.GetMethod(functionName.ToString()).Invoke(detailObjectPropertyInfo.GetValue(masterObject, null), new object[] { detailObject }); }
private void UpdateObject(EntityObject entityObject, List<TransField> updateColumnsList, EntityState state, bool keyFieldsChanged, bool secondOperation) { var sqlBuidler = new StringBuilder(); foreach (var field in updateColumnsList) { if (field.ReadOnly) { throw new ArgumentException(MessageHelper.EFTransactionMessage.TransDesFieldIsReadOnly); } if (sqlBuidler.Length > 0) { sqlBuidler.Append(","); } if (keyFieldsChanged) { if (secondOperation) { field.OldValue = 0; } else { field.NewValue = 0; } } string columnSql = string.Empty; switch (field.UpdateMode) { case UpdateMode.Increase: columnSql = string.Format("{0} = {0} + {1} - {2}", field.DesField, field.NewValue ?? 0, field.OldValue ?? 0); break; case UpdateMode.Decrease: columnSql = string.Format("{0} = {0} - {1} + {2}", field.DesField, field.NewValue ?? 0, field.OldValue ?? 0); break; case UpdateMode.Replace: if (field.NewValue == null || field.NewValue == DBNull.Value) { columnSql = string.Format("{0} = null", field.DesField); } else if (field.NewValue is string || field.NewValue is Guid) { columnSql = string.Format("{0} = '{1}'", field.DesField, field.NewValue); } else { columnSql = string.Format("{0} = {1}", field.DesField, field.NewValue); } break; } sqlBuidler.Append(columnSql); } var whereBuilder = new StringBuilder(); foreach (var keyValues in entityObject.EntityKey.EntityKeyValues) { if (whereBuilder.Length > 0) { whereBuilder.Append(" and "); } var key = keyValues.Key; var value = keyValues.Value; if (value is string || value is Guid) { whereBuilder.Append(string.Format("{0} = '{1}'", key, value)); } else { whereBuilder.Append(string.Format("{0} = {1}", key, value)); } } string updateSql = string.Format("Update {0} set {1} where {2}", entityObject.GetType().Name, sqlBuidler, whereBuilder); this.Context.ExecuteNonQuery(updateSql.ToString()); }