예제 #1
0
        public static void SetChildren <T>(this SQLiteConnection conn, IEnumerable <T> elements, params string[] relationshipPropertyNames) where T : new()
        {
            if (elements is ITableQuery <T> )
            {
                throw new InvalidOperationException("Use WithChildren<T> or enumerate the query before calling SetChildren<T>.");
            }

            var results = elements as ICollection <T> ?? elements.ToList();

            var relationshipProperties = typeof(T).GetRelationshipProperties().ToList();

            if (relationshipPropertyNames != null && relationshipPropertyNames.Any())
            {
                foreach (var propertyName in relationshipPropertyNames)
                {
                    if (relationshipProperties.All(p => p.Name != propertyName))
                    {
                        throw new ArgumentException(String.Format("Invalid relationship property name '{0}'.", propertyName));
                    }
                }

                var toRemove = relationshipProperties.Where(p => !relationshipPropertyNames.Contains(p.Name)).ToList();

                foreach (var p in toRemove)
                {
                    relationshipProperties.Remove(p);
                }
            }

            foreach (var relationshipProperty in relationshipProperties)
            {
                var relationshipAttribute = relationshipProperty.GetAttribute <RelationshipAttribute>();

                if (relationshipAttribute is ManyToOneAttribute)
                {
                    conn.GetManyToOneChildren(results, relationshipProperty);
                }
                else if (relationshipAttribute is OneToManyAttribute)
                {
                    conn.GetOneToManyChildren(results, relationshipProperty);
                }
#if DEBUG
                else
                {
                    Debug.WriteLine("WARNING: RelationshipAttribute {0} is not yet supported by WithChildren or SetChildren.", relationshipAttribute.GetType().Name);
                }
#endif
            }
        }