コード例 #1
0
        public static EntityCollection <TElement> CreateCollection <T, TElement>(this T entity, Expression <Func <T, EntityCollection <TElement> > > expr, params TElement[] items)
            where T : EntityObject
            where TElement : EntityObject
        {
            if (expr.Body.NodeType != ExpressionType.MemberAccess)
            {
                throw new ArgumentException("Expression is not correct.", "expr");
            }
            var          member = ((MemberExpression)expr.Body).Member;
            PropertyInfo pi     = member as PropertyInfo;

            if (pi == null)
            {
                throw new ArgumentException("Expression is not correct.", "expr");
            }
            EdmRelationshipNavigationPropertyAttribute attribute = (EdmRelationshipNavigationPropertyAttribute)Attribute.GetCustomAttribute(pi, typeof(EdmRelationshipNavigationPropertyAttribute));
            EntityCollection <TElement> result = new EntityCollection <TElement>();
            RelationshipManager         rm     = RelationshipManager.Create(entity);

            rm.InitializeRelatedCollection(attribute.RelationshipName, attribute.TargetRoleName, result);
            foreach (var item in items)
            {
                result.Add(item);
            }
            return(result);
        }