public virtual MetadataModelNameMapper GetNameMapper([NotNull] IModel relationalModel) { Check.NotNull(relationalModel, nameof(relationalModel)); return(new MetadataModelNameMapper( relationalModel, entity => ExtensionsProvider.For(entity).TableName, property => ExtensionsProvider.For(property).ColumnName)); }
public HistoryRepository( [NotNull] IDatabaseCreator databaseCreator, [NotNull] ISqlStatementExecutor executor, [NotNull] IRelationalConnection connection, [NotNull] IDbContextOptions options, [NotNull] IMigrationsModelDiffer modelDiffer, [NotNull] IMigrationsSqlGenerator migrationsSqlGenerator, [NotNull] IRelationalMetadataExtensionProvider annotations, [NotNull] IUpdateSqlGenerator sql) { Check.NotNull(databaseCreator, nameof(databaseCreator)); Check.NotNull(executor, nameof(executor)); Check.NotNull(connection, nameof(connection)); Check.NotNull(options, nameof(options)); Check.NotNull(modelDiffer, nameof(modelDiffer)); Check.NotNull(migrationsSqlGenerator, nameof(migrationsSqlGenerator)); Check.NotNull(annotations, nameof(annotations)); Check.NotNull(sql, nameof(sql)); _databaseCreator = (IRelationalDatabaseCreator)databaseCreator; _executor = executor; _connection = connection; _modelDiffer = modelDiffer; _migrationsSqlGenerator = migrationsSqlGenerator; Sql = sql; var relationalOptions = RelationalOptionsExtension.Extract(options); TableName = relationalOptions?.MigrationsHistoryTableName ?? DefaultTableName; TableSchema = relationalOptions.MigrationsHistoryTableSchema; _model = new LazyRef <IModel>( () => { var modelBuilder = new ModelBuilder(new ConventionSet()); modelBuilder.Entity <HistoryRow>( x => { ConfigureTable(x); x.ToTable(TableName, TableSchema); }); return(modelBuilder.Model); }); var entityType = new LazyRef <IEntityType>(() => _model.Value.GetEntityType(typeof(HistoryRow))); _migrationIdColumnName = new LazyRef <string>( () => annotations.For(entityType.Value.FindProperty(nameof(HistoryRow.MigrationId))).ColumnName); _productVersionColumnName = new LazyRef <string>( () => annotations.For(entityType.Value.FindProperty(nameof(HistoryRow.ProductVersion))).ColumnName); }
protected virtual void EnsureDistinctTableNames([NotNull] IModel model) { var tables = new HashSet <string>(); foreach (var entityType in model.EntityTypes.Where(et => et.BaseType == null)) { var annotations = _relationalExtensions.For(entityType); var name = annotations.Schema + "." + annotations.TableName; if (!tables.Add(name)) { ShowError(Relational.Internal.Strings.DuplicateTableName(annotations.TableName, annotations.Schema, entityType.DisplayName())); } } }
public virtual Expression CreateMaterializer( [NotNull] IEntityType entityType, [NotNull] SelectExpression selectExpression, [NotNull] Func <IProperty, SelectExpression, int> projectionAdder, [NotNull] IRelationalMetadataExtensionProvider relationalExtensions, [CanBeNull] IQuerySource querySource) { Check.NotNull(entityType, nameof(entityType)); Check.NotNull(selectExpression, nameof(selectExpression)); Check.NotNull(projectionAdder, nameof(projectionAdder)); var valueBufferParameter = Expression.Parameter(typeof(ValueBuffer)); var concreteEntityTypes = entityType.GetConcreteTypesInHierarchy().ToArray(); var indexMap = new int[concreteEntityTypes[0].GetProperties().Count()]; var propertyIndex = 0; foreach (var property in concreteEntityTypes[0].GetProperties()) { indexMap[propertyIndex++] = projectionAdder(property, selectExpression); } var materializer = _entityMaterializerSource .CreateMaterializeExpression( concreteEntityTypes[0], valueBufferParameter, indexMap); if (concreteEntityTypes.Length == 1 && concreteEntityTypes[0].RootType() == concreteEntityTypes[0]) { return(Expression.Lambda <Func <ValueBuffer, object> >(materializer, valueBufferParameter)); } var discriminatorProperty = relationalExtensions.For(concreteEntityTypes[0]).DiscriminatorProperty; var discriminatorColumn = selectExpression.Projection .OfType <AliasExpression>() .Single(c => c.TryGetColumnExpression()?.Property == discriminatorProperty); var firstDiscriminatorValue = Expression.Constant( relationalExtensions.For(concreteEntityTypes[0]).DiscriminatorValue); var discriminatorPredicate = Expression.Equal(discriminatorColumn, firstDiscriminatorValue); if (concreteEntityTypes.Length == 1) { selectExpression.Predicate = new DiscriminatorPredicateExpression(discriminatorPredicate, querySource); return(Expression.Lambda <Func <ValueBuffer, object> >(materializer, valueBufferParameter)); } var discriminatorValueVariable = Expression.Variable(discriminatorProperty.ClrType); var returnLabelTarget = Expression.Label(typeof(object)); var blockExpressions = new Expression[] { Expression.Assign( discriminatorValueVariable, _entityMaterializerSource .CreateReadValueExpression( valueBufferParameter, discriminatorProperty.ClrType, discriminatorProperty.Index)), Expression.IfThenElse( Expression.Equal(discriminatorValueVariable, firstDiscriminatorValue), Expression.Return(returnLabelTarget, materializer), Expression.Throw( Expression.Call( _createUnableToDiscriminateException, Expression.Constant(concreteEntityTypes[0])))), Expression.Label( returnLabelTarget, Expression.Default(returnLabelTarget.Type)) }; foreach (var concreteEntityType in concreteEntityTypes.Skip(1)) { indexMap = new int[concreteEntityType.GetProperties().Count()]; propertyIndex = 0; foreach (var property in concreteEntityType.GetProperties()) { indexMap[propertyIndex++] = projectionAdder(property, selectExpression); } var discriminatorValue = Expression.Constant( relationalExtensions.For(concreteEntityType).DiscriminatorValue); materializer = _entityMaterializerSource .CreateMaterializeExpression(concreteEntityType, valueBufferParameter, indexMap); blockExpressions[1] = Expression.IfThenElse( Expression.Equal(discriminatorValueVariable, discriminatorValue), Expression.Return(returnLabelTarget, materializer), blockExpressions[1]); discriminatorPredicate = Expression.OrElse( Expression.Equal(discriminatorColumn, discriminatorValue), discriminatorPredicate); } selectExpression.Predicate = new DiscriminatorPredicateExpression(discriminatorPredicate, querySource); return(Expression.Lambda <Func <ValueBuffer, object> >( Expression.Block(new[] { discriminatorValueVariable }, blockExpressions), valueBufferParameter)); }
protected virtual IEntityType FindEntityType( [CanBeNull] IModel model, [CanBeNull] string schema, [NotNull] string tableName) => model?.EntityTypes.FirstOrDefault( t => _annotations.For(t).TableName == tableName && _annotations.For(t).Schema == schema);