Beispiel #1
0
        public override void SetValue(TEntity entity, object value)
        {
            // The base.GetValue gets the entityReference<TEntity> class
            var entityCollection = base.GetValue(entity) as IEntitySource;

            // The The foreign key mapping comes from the value passed to this method
            IForeignKeyMapping foreignKeyMapping = (IForeignKeyMapping)value;

            var entityCollectionType = foreignKeyMapping.ForeignKeyProperty.DeclaringType;

            // TJT: Clean the EntitySet access up!
            var entitySetSource = _entityMapping.Model.DataContext.EntitySets[entityCollectionType];

            var keyName  = foreignKeyMapping.ForeignKeyProperty.Name;
            var keyValue = _entityMapping.GetColumn(entity, foreignKeyMapping.ReferenceKeyProperty.Name);

            if (!entityCollection.HasSource)
            {
                entityCollection.SetSource(entitySetSource, keyName, keyValue);
            }
            else
            {
                throw new InvalidOperationException("Entity reference source already set.");
            }
        }
        public void RegisterForeignKeyMapping <TParentEntity, TChildEntity>(
            IForeignKeyMapping <TParentEntity, TChildEntity> mapping)
            where TParentEntity : IDomainObject
            where TChildEntity : IDomainObject
        {
            string parentName = typeof(TParentEntity).FullName;

            if (!_foreignKeMapping.ContainsKey(parentName))
            {
                _foreignKeMapping.Add(parentName, new Dictionary <string, object>());
            }

            IDictionary <string, object> maps = _foreignKeMapping[parentName];
            string childName = typeof(TChildEntity).FullName;

            if (!maps.ContainsKey(childName))
            {
                maps.Add(childName, mapping);
            }
        }
        public IList <TChildEntity> GetForeignKeyValues <TParentEntity, TChildEntity>(TParentEntity entity)
            where TParentEntity : IDomainObject
            where TChildEntity : IDomainObject
        {
            string parentName = typeof(TParentEntity).FullName;

            if (!_foreignKeMapping.ContainsKey(parentName))
            {
                throw new ArgumentException(string.Format("Foreign key mapping associated to parent type '{0}' does not exists", parentName));
            }

            IDictionary <string, object> maps = _foreignKeMapping[parentName];
            string childName = typeof(TChildEntity).FullName;

            if (!maps.ContainsKey(childName))
            {
                throw new ArgumentException(string.Format("Foreign key mapping associated to child type '{0}' does not exists", childName));
            }

            IForeignKeyMapping <TParentEntity, TChildEntity> fkMapping = (IForeignKeyMapping <TParentEntity, TChildEntity>)maps[childName];

            return(fkMapping.GetChildEntities(entity));
        }