private bool AddDiscriminatorCondition(SelectExpression selectExpression, IEntityType entityType) { SqlExpression predicate; var concreteEntityTypes = entityType.GetConcreteDerivedTypesInclusive().ToList(); if (concreteEntityTypes.Count == 1) { var concreteEntityType = concreteEntityTypes[0]; if (concreteEntityType.BaseType == null) { return(false); } var discriminatorColumn = GetMappedEntityProjectionExpression(selectExpression) .BindProperty(concreteEntityType.GetDiscriminatorProperty()); predicate = Equal(discriminatorColumn, Constant(concreteEntityType.GetDiscriminatorValue())); } else { var discriminatorColumn = GetMappedEntityProjectionExpression(selectExpression) .BindProperty(concreteEntityTypes[0].GetDiscriminatorProperty()); predicate = In( discriminatorColumn, Constant(concreteEntityTypes.Select(et => et.GetDiscriminatorValue()).ToList()), negated: false); } selectExpression.ApplyPredicate(predicate); return(true); }
private void AddOptionalDependentConditions( SelectExpression selectExpression, IEntityType entityType, ICollection <IEntityType> sharingTypes) { SqlExpression predicate = null; var requiredNonPkProperties = entityType.GetProperties().Where(p => !p.IsNullable && !p.IsPrimaryKey()).ToList(); if (requiredNonPkProperties.Count > 0) { var entityProjectionExpression = GetMappedEntityProjectionExpression(selectExpression); predicate = IsNotNull(requiredNonPkProperties[0], entityProjectionExpression); if (requiredNonPkProperties.Count > 1) { predicate = requiredNonPkProperties .Skip(1) .Aggregate( predicate, (current, property) => AndAlso( IsNotNull(property, entityProjectionExpression), current)); } selectExpression.ApplyPredicate(predicate); } else { var allNonPkProperties = entityType.GetProperties().Where(p => !p.IsPrimaryKey()).ToList(); if (allNonPkProperties.Count > 0) { var entityProjectionExpression = GetMappedEntityProjectionExpression(selectExpression); predicate = IsNotNull(allNonPkProperties[0], entityProjectionExpression); if (allNonPkProperties.Count > 1) { predicate = allNonPkProperties .Skip(1) .Aggregate( predicate, (current, property) => OrElse( IsNotNull(property, entityProjectionExpression), current)); } selectExpression.ApplyPredicate(predicate); foreach (var referencingFk in entityType.GetReferencingForeignKeys()) { var otherSelectExpression = new SelectExpression(entityType); var sameTable = sharingTypes.Contains(referencingFk.DeclaringEntityType); AddInnerJoin( otherSelectExpression, referencingFk, sameTable ? sharingTypes : null, skipInnerJoins: sameTable); selectExpression.ApplyUnion(otherSelectExpression, distinct: true); } } } }