private static string GetPrimaryKeyName(Type type) { string tempPrimaryKeyName = ""; PropertyInfo[] props = type.GetProperties(); foreach (PropertyInfo prop in props) { foreach (var attr in prop.GetCustomAttributes(typeof(EdmScalarPropertyAttribute), false)) { EdmScalarPropertyAttribute edmProp = (EdmScalarPropertyAttribute)attr; if (edmProp.EntityKeyProperty) { tempPrimaryKeyName = prop.Name; break; } if (!String.IsNullOrEmpty(tempPrimaryKeyName)) { break; } } if (!String.IsNullOrEmpty(tempPrimaryKeyName)) { break; } } return(tempPrimaryKeyName); //return typeof(TEntity).GetProperties().Where(property => ((EdmScalarPropertyAttribute)property.GetCustomAttributes(typeof(EdmScalarPropertyAttribute), false).FirstOrDefault()).EntityKeyProperty).FirstOrDefault().Name; }
/// <summary> /// Gets information about the property of an EntityObject that defines its primary key. /// Note that this method does not support composite primary keys. (will throw exception) /// </summary> /// <param name="type"></param> /// <returns></returns> public PropertyInfo GetPrimaryKeyProperty(Type type) { if (!type.IsSubclassOf(typeof(EntityObject))) { throw new ArgumentOutOfRangeException("It appears that an object was passed to GetPrimaryKeyProperty that does not inherit from EntityObject."); } PropertyInfo[] properties = type.GetProperties(); bool primaryKeyFound = false; PropertyInfo primaryKeyProperty = null; foreach (PropertyInfo property in properties) { // The EdmScalarPropertyAttribute declared in the auto-generated code tells what properties are part of the primary key. // Again, this code does not support composite keys. EdmScalarPropertyAttribute attribute = (EdmScalarPropertyAttribute)Attribute.GetCustomAttribute(property, typeof(EdmScalarPropertyAttribute)); if (attribute != null && attribute.EntityKeyProperty) { if (primaryKeyFound) { throw new ArgumentOutOfRangeException("It appears that an entity with a composite primary key was passed to GetPrimaryKeyProperty, which is not supported."); } primaryKeyProperty = property; primaryKeyFound = true; } } return(primaryKeyProperty); }
private static PropertyInfo GetEntityPrimaryKey <T>() where T : EntityObject { PropertyInfo[] properties = typeof(T).GetProperties(); foreach (PropertyInfo info in properties) { foreach (Attribute attribute in info.GetCustomAttributes(typeof(EdmScalarPropertyAttribute), false)) { EdmScalarPropertyAttribute attribute2 = attribute as EdmScalarPropertyAttribute; if ((attribute2 != null) && attribute2.EntityKeyProperty) { return(info); } } } return(null); }
public static CustomAttributeBuilder CreateCustom(EdmScalarPropertyAttribute attribute) { return(new CustomAttributeBuilder( attribute.GetType().GetDeclaredConstructor(), new object[0], new PropertyInfo[] { typeof(EdmScalarPropertyAttribute).GetDeclaredProperty("EntityKeyProperty"), typeof(EdmScalarPropertyAttribute).GetDeclaredProperty("IsNullable") }, new object[] { attribute.EntityKeyProperty, attribute.IsNullable })); }
private static List <PropertyInfo> GetKeyColumns(System.Type type) { List <PropertyInfo> retval = new List <PropertyInfo>(); PropertyInfo[] props = type.GetProperties(); foreach (PropertyInfo prop in props) { IEnumerable <Attribute> attributes = prop.GetCustomAttributes(true).OfType <System.Attribute>().AsEnumerable(); EdmScalarPropertyAttribute edmScalarPropertyAttribute = attributes.OfType <EdmScalarPropertyAttribute>().FirstOrDefault(); if (edmScalarPropertyAttribute != null) { if (edmScalarPropertyAttribute.EntityKeyProperty == true) { retval.Add(prop); } } } return(retval); }
public virtual void AutoObjectSetValue <T>(T objectT) where T : EntityObject { PropertyInfo[] properties = typeof(T).GetProperties(); foreach (PropertyInfo info in properties) { foreach (Attribute attribute in info.GetCustomAttributes(typeof(EdmScalarPropertyAttribute), false)) { EdmScalarPropertyAttribute attribute2 = attribute as EdmScalarPropertyAttribute; if ((attribute2 != null) && !attribute2.EntityKeyProperty) { string str = null; this.FindFieldValue(info.Name, this.Controls, ref str); if (str != null) { if (info.PropertyType == typeof(Guid)) { info.SetValue(objectT, new Guid(str), null); } else if (info.PropertyType == typeof(string)) { info.SetValue(objectT, str, null); } else if (info.PropertyType == typeof(DateTime)) { info.SetValue(objectT, DateTime.Parse(str), null); } else if (info.PropertyType == typeof(int)) { info.SetValue(objectT, int.Parse(str), null); } else if (info.PropertyType == typeof(double)) { info.SetValue(objectT, double.Parse(str), null); } else if (info.PropertyType == typeof(bool)) { info.SetValue(objectT, (str.ToString().ToLower() == "true") ? ((object)1) : ((object)(str.ToString().ToLower() == "1")), null); } } } } } }
/// <summary> /// 获取对象主健值 /// </summary> /// <typeparam name="T">泛型</typeparam> /// <returns></returns> private static PropertyInfo GetEntityPrimaryKey <T>() where T : System.Data.Objects.DataClasses.EntityObject { Type type = typeof(T); PropertyInfo[] objPropertyInfoList = type.GetProperties(); foreach (PropertyInfo objPropertyInfo in objPropertyInfoList) { foreach (Attribute attr in objPropertyInfo.GetCustomAttributes(typeof(System.Data.Objects.DataClasses.EdmScalarPropertyAttribute), false)) { EdmScalarPropertyAttribute objEdmScalarPropertyAttribute = attr as EdmScalarPropertyAttribute; if (objEdmScalarPropertyAttribute != null) { if (objEdmScalarPropertyAttribute.EntityKeyProperty) { return(objPropertyInfo); } } } } return(null); }
private void ResolveEnumTypeProperty(StructuralType declaringType, PropertyInfo clrProperty) { EdmType edmType; if (!this.TryGetLoadedType(clrProperty.PropertyType, out edmType) || !Helper.IsEnumType(edmType)) { this.SessionData.EdmItemErrors.Add(new EdmItemError(Strings.Validator_OSpace_ScalarPropertyNotPrimitive((object)clrProperty.Name, (object)clrProperty.DeclaringType.FullName, (object)clrProperty.PropertyType.FullName))); } else { EdmScalarPropertyAttribute propertyAttribute = clrProperty.GetCustomAttributes <EdmScalarPropertyAttribute>(false).Single <EdmScalarPropertyAttribute>(); EdmProperty edmProperty = new EdmProperty(clrProperty.Name, TypeUsage.Create(edmType, new FacetValues() { Nullable = (FacetValueContainer <bool?>) new bool?(propertyAttribute.IsNullable) }), clrProperty, declaringType.ClrType); declaringType.AddMember((EdmMember)edmProperty); if (declaringType.BuiltInTypeKind != BuiltInTypeKind.EntityType || !propertyAttribute.EntityKeyProperty) { return; } ((EntityTypeBase)declaringType).AddKeyMember((EdmMember)edmProperty); } }
/// <summary> /// 更新实体上下文中的对象(未保存到数据库中,请调用SaveContextChanges方法) /// </summary> /// <param name="obj"></param> public void UpdateFromContext(object obj) { EntityObject NewEntity = (EntityObject)obj; object OldEntity = null; string keyName = string.Empty; string keyValue = string.Empty; if (NewEntity.EntityKey != null) { if (!this.IsGetObjectByKey(NewEntity.EntityKey, ref OldEntity)) { this.GetObjectInOtherway(NewEntity.EntityKey, ref OldEntity); } } else { foreach (PropertyInfo info in NewEntity.GetType().GetProperties()) { EdmScalarPropertyAttribute attribute = info.GetCustomAttributes(typeof(EdmScalarPropertyAttribute), false).FirstOrDefault <object>() as EdmScalarPropertyAttribute; if ((attribute != null) && attribute.EntityKeyProperty) { keyName = info.Name; keyValue = info.GetValue(NewEntity, null).ToString(); break; } } string name = obj.GetType().Name; EntityKey key = new EntityKey(DALFacoty.DBContextName + "." + name, keyName, keyValue); this.lbc.GetDataContext().TryGetObjectByKey(key, out OldEntity); } //将修改的普通属性值(不包括外键)设置到当前上下文实体上 this.lbc.GetDataContext().ApplyPropertyChanges(NewEntity.GetType().Name, NewEntity); //将修改的外键值设置到当前上下文实体上 foreach (IRelatedEnd newEntityRefrence in ((IEntityWithRelationships)NewEntity).RelationshipManager.GetAllRelatedEnds()) { //if (!newEntityRefrence.IsLoaded) newEntityRefrence.Load();load后值会被修改为数据库里的原值 //EntityReference r = newEntityRefrence.EntityKey; //foreach (IEntityWithKey entityR in newEntityRefrence) //{ EntityReference ef = null; try { //EntityReference,EntityReferenceConllection继承IRelatedEnd //如果是外键,为EntityReference,如果是主表则为EntityReferenceConllection ef = newEntityRefrence as EntityReference; } catch (Exception ex) { } if (ef != null) { object objRefNew = null; if (ef.EntityKey != null) { this.lbc.GetDataContext().TryGetObjectByKey(ef.EntityKey, out objRefNew); if (NewEntity.GetType().Name == newEntityRefrence.TargetRoleName) { (OldEntity as EntityObject).GetType().GetProperty(newEntityRefrence.TargetRoleName + "2").SetValue(OldEntity, objRefNew, null); } else { if ((OldEntity as EntityObject).GetType().GetProperty(newEntityRefrence.TargetRoleName).PropertyType.Name == "EntityCollection`1") { continue; } (OldEntity as EntityObject).GetType().GetProperty(newEntityRefrence.TargetRoleName).SetValue(OldEntity, objRefNew, null); } } } } }