Example #1
0
        public EfSaveEngine(DbContext context, CancellationToken cancellationToken, params Expression <Func <T, object> >[] pivotKeys)
        {
            this._cancellationToken = cancellationToken;
            _context = context;
            var entityType = context.Model.FindEntityType(typeof(T));

            if (entityType == null)
            {
                throw new InvalidOperationException("DbContext does not contain EntitySet for Type: " + typeof(T).Name);
            }
            _keyPropertyInfos = entityType.GetProperties().Where(i => !i.IsShadowProperty() && i.IsPrimaryKey()).Select(i => i.PropertyInfo).ToList();
            List <List <PropertyInfo> > propertyInfosForPivot;

            if ((pivotKeys?.Length ?? 0) == 0)
            {
                propertyInfosForPivot = new List <List <PropertyInfo> > {
                    entityType.FindPrimaryKey().Properties.Select(i => i.PropertyInfo).ToList()
                }
            }
            ;
            else
            {
                propertyInfosForPivot = pivotKeys.Select(pivotKey => KeyDefinitionExtractor.GetKeys(pivotKey)).ToList();
            }

            _findConditionExpression = CreateFindConditionExpression(propertyInfosForPivot);
        }
Example #2
0
        private List <IProperty> GetPropertiesForPivot(List <IProperty> allProperties, IProperty tenantIdProp, Expression <Func <T, object> > pivotKey)
        {
            var pivotKeyNames = KeyDefinitionExtractor.GetKeys(pivotKey).Select(i => i.Name).ToList();

            if (tenantIdProp != null)
            {
                pivotKeyNames.Add(tenantIdProp.GetColumnName(StoreObject));
            }
            return(allProperties.Where(i => pivotKeyNames.Contains(i.Name)).ToList());
        }
Example #3
0
        public BulkSaveEngineBase(DbContext context, Expression <Func <T, object> > pivotKey = null)
        {
            List <IProperty> computedProperties;
            List <IProperty> defaultValuesProperties;
            List <IProperty> notPivotComputedProperties;

            this._context = context;

            var entityType = context.Model.FindEntityType(typeof(T));

            if (entityType == null)
            {
                throw new InvalidOperationException("DbContext does not contain EntitySet for Type: " + typeof(T).Name);
            }
            _schema = entityType.Relational().Schema;
            _table  = entityType.Relational().TableName;

            _entityTypes = GetAllRelatedEntityTypes(entityType).Distinct().ToList();
            var allProperties = _entityTypes.SelectMany(dt => dt.GetProperties()).Distinct(new LambdaEqualityComparer <IProperty, string>(i => i.Relational().ColumnName)).ToList();

            if (pivotKey == null)
            {
                _propertiesForPivot = entityType.FindPrimaryKey().Properties.ToList();
            }
            else
            {
                var pivotKeyNames = KeyDefinitionExtractor.GetKeys(pivotKey).Select(i => i.Name).ToList();
                _propertiesForPivot = allProperties.Where(i => pivotKeyNames.Contains(i.Name)).ToList();
            }

            //identityProperty = entityType.GetProperties().FirstOrDefault(i => i.SqlServer().ValueGenerationStrategy == SqlServerValueGenerationStrategy.IdentityColumn);
            // string timestampDbTypeName = nameof(TimestampAttribute).Replace("Attribute", "").ToLower(); // = "timestamp";
            computedProperties         = allProperties.Where(i => (i.ValueGenerated & ValueGenerated.OnAddOrUpdate) != ValueGenerated.Never).ToList();
            notPivotComputedProperties = computedProperties.Except(_propertiesForPivot).ToList();

            defaultValuesProperties = allProperties.Where(i => i.Relational().DefaultValueSql != null).ToList();

            _propertiesToBulkLoad = allProperties.Except(notPivotComputedProperties).ToList();

            _propertiesToInsert = allProperties
                                  .Except(computedProperties)
                                  //.Except(new[] { identityProperty })
                                  .ToList();
            _propertiesToUpdate = allProperties
                                  .Except(_propertiesForPivot)
                                  .Except(computedProperties)
                                  //.Except(new[] { identityProperty })
                                  .ToList();
            _propertiesToGetAfterSetInTarget = computedProperties
                                               //.Union(new[] { identityProperty })
                                               .Union(defaultValuesProperties)
                                               .Distinct(new LambdaEqualityComparer <IProperty, string>(i => i.Relational().ColumnName))
                                               .ToList();
        }
Example #4
0
        public EfSaveEngine(DbContext context, Expression <Func <T, object> > pivotKey = null)
        {
            _context = context;
            var entityType = context.Model.FindEntityType(typeof(T));

            if (entityType == null)
            {
                throw new InvalidOperationException("DbContext does not contain EntitySet for Type: " + typeof(T).Name);
            }
            _keyPropertyInfos = entityType.GetProperties().Where(i => !i.IsShadowProperty && i.IsPrimaryKey()).Select(i => i.PropertyInfo).ToList();
            List <PropertyInfo> propertyInfosForPivot;

            if (pivotKey == null)
            {
                propertyInfosForPivot = entityType.FindPrimaryKey().Properties.Select(i => i.PropertyInfo).ToList();
            }
            else
            {
                propertyInfosForPivot = KeyDefinitionExtractor.GetKeys(pivotKey);
            }

            _findConditionExpression = CreateFindConditionExpression(propertyInfosForPivot);
        }