Ejemplo n.º 1
0
        /// <summary>
        /// Gets the metadata for an entityset using the .NET type information of an entity
        /// </summary>
        /// <param name="entityType">.NET CLR type information of an entity</param>
        /// <returns>metatdata for the passed entity or nothing if not found</returns>
        /// <remarks>
        /// </remarks>
        private static EntitySetBase GetEntitySet(Type entityType)
        {
            EntitySetBase result = null;

            if (entityType != null && metaData != null)
            {
                // Get attribute of entity type using reflection and LINQ on attributes
                EdmEntityTypeAttribute edmAttrib = default(EdmEntityTypeAttribute);
                edmAttrib = entityType.GetCustomAttributes(typeof(EdmEntityTypeAttribute), true).FirstOrDefault() as EdmEntityTypeAttribute;
                ////Debug.Assert(edmAttrib != null);

                // Get entity containters for metadata
                var globalItems = metaData.GetItems(DataSpace.CSpace);

                // LINQ query on metadata conceptual space
                var query2 = from c in globalItems
                             where c.BuiltInTypeKind == BuiltInTypeKind.EntityContainer
                             from s in ((EntityContainer)c).BaseEntitySets
                             where s.ElementType.Name == edmAttrib.Name &&
                             s.ElementType.NamespaceName == edmAttrib.NamespaceName
                             select(EntitySetBase) s;

                result = query2.FirstOrDefault();
            }
            return(result);
        }
Ejemplo n.º 2
0
        public static void ApplyPropertyChanges <T>(this ObjectContext context, T entity)
            where T : EntityObject
        {
            EdmEntityTypeAttribute entityTypeAttr = (EdmEntityTypeAttribute)Attribute.GetCustomAttribute(typeof(T), typeof(EdmEntityTypeAttribute), false);

            if (entityTypeAttr == null)
            {
                throw new NotSupportedException("T is not an entity.");
            }
            context.ApplyPropertyChanges(entityTypeAttr.Name, entity);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Returns the entity typename for the given .NET type.
        /// </summary>
        public string GetEntityTypeNameOf(Type type)
        {
            EdmEntityTypeAttribute attr = (EdmEntityTypeAttribute)type.GetCustomAttributes(typeof(EdmEntityTypeAttribute), true).FirstOrDefault();

            if (attr != null)
            {
                return(attr.NamespaceName + "." + attr.Name);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 4
0
        public static EntityKey GetEntityKey <T>(this ObjectContext context, T entity)
            where T : EntityObject
        {
            EdmEntityTypeAttribute entityTypeAttr = (EdmEntityTypeAttribute)Attribute.GetCustomAttribute(entity.GetType(), typeof(EdmEntityTypeAttribute), false);

            if (entityTypeAttr == null)
            {
                throw new NotSupportedException("T is not an entity.");
            }
            string entityFullname = context.DefaultContainerName + "." + entityTypeAttr.Name;

            return(new System.Data.EntityKey(entityFullname,
                                             from p in entity.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public)
                                             where p.GetGetMethod(false) != null
                                             let attribute = (EdmScalarPropertyAttribute)Attribute.GetCustomAttribute(p, typeof(EdmScalarPropertyAttribute))
                                                             where attribute != null && attribute.EntityKeyProperty
                                                             select new KeyValuePair <string, object>(p.Name, p.GetValue(entity, null))));
        }
Ejemplo n.º 5
0
        public static EntityObject AttachExistedObject(this ObjectContext context, object objEntity)
        {
            EntityObject           entity         = objEntity as EntityObject;
            EdmEntityTypeAttribute entityTypeAttr = (EdmEntityTypeAttribute)Attribute.GetCustomAttribute(objEntity.GetType(), typeof(EdmEntityTypeAttribute), false);

            if (entityTypeAttr == null)
            {
                throw new NotSupportedException("T is not an entity.");
            }
            if (entity.EntityKey == null)
            {
                string entityFullname = context.DefaultContainerName + "." + entityTypeAttr.Name;
                entity.EntityKey = new System.Data.EntityKey(entityFullname,
                                                             from p in objEntity.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public)
                                                             where p.GetGetMethod(false) != null
                                                             let attribute = (EdmScalarPropertyAttribute)Attribute.GetCustomAttribute(p, typeof(EdmScalarPropertyAttribute))
                                                                             where attribute != null && attribute.EntityKeyProperty
                                                                             select new KeyValuePair <string, object>(p.Name, p.GetValue(entity, null)));
            }
            context.Attach(entity);
            context.ApplyPropertyChanges(entityTypeAttr.Name, entity);
            return(entity);
        }
Ejemplo n.º 6
0
        public static string GetEntityTableName <T>() where T : EntityObject
        {
            EdmEntityTypeAttribute attribute = (EdmEntityTypeAttribute)Attribute.GetCustomAttribute(typeof(T), typeof(EdmEntityTypeAttribute), false);

            return((attribute == null) ? "" : attribute.Name);
        }
Ejemplo n.º 7
0
 private static object[] GetArgs(EdmEntityTypeAttribute attribute)
 {
     return(new object[] { });
 }