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

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

            var entityReferenceType = foreignKeyMapping.ReferenceKeyProperty.DeclaringType;

            // TJT: Clean the EntitySet access up!
            var entitySetSource  = _entityMapping.Model.DataContext.EntitySets[entityReferenceType];
            var referenceKeyName = foreignKeyMapping.ReferenceKeyProperty.Name;
            var foreignKeyValue  = _entityMapping.GetForeignKey(entity, foreignKeyMapping.PropertyName);

            if (!entityReference.HasSource)
            {
                entityReference.SetSource(entitySetSource, referenceKeyName, foreignKeyValue);
            }
            else
            {
                throw new InvalidOperationException("Entity reference source already set.");
            }
        }