public void MaxBatchSize_is_optional()
        {
            var optionsBuilder = new DbContextOptionsBuilder();

            optionsBuilder.UseSqlServer("Database=Crunchie");

            var typeMapper = new SqlServerTypeMappingSource(
                TestServiceFactory.Instance.Create <TypeMappingSourceDependencies>(),
                TestServiceFactory.Instance.Create <RelationalTypeMappingSourceDependencies>());


            var factory = new SqlServerModificationCommandBatchFactory(
                new RelationalCommandBuilderFactory(
                    new FakeDiagnosticsLogger <DbLoggerCategory.Database.Command>(),
                    typeMapper),
                new SqlServerSqlGenerationHelper(
                    new RelationalSqlGenerationHelperDependencies()),
                new SqlServerUpdateSqlGenerator(
                    new UpdateSqlGeneratorDependencies(
                        new SqlServerSqlGenerationHelper(
                            new RelationalSqlGenerationHelperDependencies()),
                        typeMapper)),
                new TypedRelationalValueBufferFactoryFactory(
                    new RelationalValueBufferFactoryDependencies(
                        typeMapper)),
                optionsBuilder.Options);

            var batch = factory.Create();

            Assert.True(batch.AddCommand(new ModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, false, null)));
            Assert.True(batch.AddCommand(new ModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, false, null)));
        }
Пример #2
0
        public void AddCommand_returns_false_when_max_batch_size_is_reached()
        {
            var typeMapper = new SqlServerTypeMappingSource(
                TestServiceFactory.Instance.Create <TypeMappingSourceDependencies>(),
                TestServiceFactory.Instance.Create <RelationalTypeMappingSourceDependencies>());

            var batch = new SqlServerModificationCommandBatch(
                new RelationalCommandBuilderFactory(
                    new FakeDiagnosticsLogger <DbLoggerCategory.Database.Command>(),
                    typeMapper),
                new SqlServerSqlGenerationHelper(
                    new RelationalSqlGenerationHelperDependencies()),
                new SqlServerUpdateSqlGenerator(
                    new UpdateSqlGeneratorDependencies(
                        new SqlServerSqlGenerationHelper(
                            new RelationalSqlGenerationHelperDependencies()),
                        typeMapper)),
                new TypedRelationalValueBufferFactoryFactory(
                    new RelationalValueBufferFactoryDependencies(
                        typeMapper, new CoreSingletonOptions())),
                1);

            Assert.True(
                batch.AddCommand(
                    new ModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, false, null)));
            Assert.False(
                batch.AddCommand(
                    new ModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, false, null)));
        }
    public void AddCommand_returns_false_when_max_batch_size_is_reached()
    {
        var typeMapper = new SqlServerTypeMappingSource(
            TestServiceFactory.Instance.Create <TypeMappingSourceDependencies>(),
            TestServiceFactory.Instance.Create <RelationalTypeMappingSourceDependencies>());

        var logger = new FakeRelationalCommandDiagnosticsLogger();

        var batch = new SqlServerModificationCommandBatch(
            new ModificationCommandBatchFactoryDependencies(
                new RelationalCommandBuilderFactory(
                    new RelationalCommandBuilderDependencies(
                        typeMapper)),
                new SqlServerSqlGenerationHelper(
                    new RelationalSqlGenerationHelperDependencies()),
                new SqlServerUpdateSqlGenerator(
                    new UpdateSqlGeneratorDependencies(
                        new SqlServerSqlGenerationHelper(
                            new RelationalSqlGenerationHelperDependencies()),
                        typeMapper)),
                new TypedRelationalValueBufferFactoryFactory(
                    new RelationalValueBufferFactoryDependencies(
                        typeMapper, new CoreSingletonOptions())),
                new CurrentDbContext(new FakeDbContext()),
                logger),
            1);

        Assert.True(
            batch.AddCommand(
                CreateModificationCommand("T1", null, false)));
        Assert.False(
            batch.AddCommand(
                CreateModificationCommand("T1", null, false)));
    }
Пример #4
0
        public static DbContext DbContext()
        {
            var typeMappingSource = new SqlServerTypeMappingSource(new TypeMappingSourceDependencies(new ValueConverterSelector(new ValueConverterSelectorDependencies())), new RelationalTypeMappingSourceDependencies()); // EF Core 2.1

            // From SQL type to C# type.
            var type = typeMappingSource.FindMapping("nvarchar(max)").ClrType;

            var conventionSet = SqlServerConventionSetBuilder.Build();
            var builder       = new ModelBuilder(conventionSet);

            // My
            var entity = builder.Entity(typeof(My));

            entity.ToTable("My");
            entity.Property("Id");
            entity.Property("Id").HasAnnotation(CoreAnnotationNames.TypeMapping, typeMappingSource.FindMapping(typeof(int)));
            entity.Property("Text").HasAnnotation(CoreAnnotationNames.TypeMapping, typeMappingSource.FindMapping(typeof(string)));

            // MyView
            entity = builder.Entity(typeof(MyView));
            entity.ToTable("MyView");
            entity.Property("Id");
            entity.Property("Id").HasAnnotation(CoreAnnotationNames.TypeMapping, typeMappingSource.FindMapping(typeof(int)));
            entity.Property("Text").HasAnnotation(CoreAnnotationNames.TypeMapping, typeMappingSource.FindMapping(typeof(string)));

            var model   = builder.Model;
            var options = new DbContextOptionsBuilder();

            options.UseSqlServer("Data Source=localhost; Initial Catalog=AdventureWorks2016; Integrated Security=True;").UseModel(model);

            var dbContext = new DbContext(options.Options);

            return(dbContext);
        }
        public virtual void GenerateSqlLiteralValue_returns_NonUnicode_String_literal()
        {
            var mapping = new SqlServerTypeMappingSource(
                TestServiceFactory.Instance.Create <TypeMappingSourceDependencies>(),
                TestServiceFactory.Instance.Create <RelationalTypeMappingSourceDependencies>())
                          .FindMapping("varchar(max)");

            var literal = mapping.GenerateSqlLiteral("A Non-Unicode String");

            Assert.Equal("'A Non-Unicode String'", literal);
        }
Пример #6
0
    protected virtual void Test_GenerateCodeLiteral_helper(
        RelationalTypeMapping typeMapping,
        object value,
        string expectedCode)
    {
        var typeMappingSource = new SqlServerTypeMappingSource(
            TestServiceFactory.Instance.Create <TypeMappingSourceDependencies>(),
            TestServiceFactory.Instance.Create <RelationalTypeMappingSourceDependencies>());

        var csharpHelper = new CSharpHelper(typeMappingSource);

        Assert.Equal(expectedCode, csharpHelper.UnknownLiteral(value));
    }
Пример #7
0
        public override IModelValidator CreateModelValidator()
        {
            var typeMappingSource = new SqlServerTypeMappingSource(
                TestServiceFactory.Instance.Create <TypeMappingSourceDependencies>(),
                TestServiceFactory.Instance.Create <RelationalTypeMappingSourceDependencies>());

            return(new SqlServerModelValidator(
                       new ModelValidatorDependencies(
                           typeMappingSource,
                           new MemberClassifier(
                               typeMappingSource,
                               TestServiceFactory.Instance.Create <IParameterBindingFactories>())),
                       new RelationalModelValidatorDependencies(typeMappingSource)));
        }
Пример #8
0
        private static SqlServerTypeMappingSource CreateTypeMappingSource()
        {
#pragma warning disable EF1001 // Internal EF Core API usage.
            var sqlServerTypeMappingSource = new SqlServerTypeMappingSource(
                new TypeMappingSourceDependencies(
                    new ValueConverterSelector(
                        new ValueConverterSelectorDependencies()
                        ),
                    Enumerable.Empty <ITypeMappingSourcePlugin>()
                    ),
                new RelationalTypeMappingSourceDependencies(Enumerable.Empty <IRelationalTypeMappingSourcePlugin>())
                );
#pragma warning restore EF1001 // Internal EF Core API usage.
            return(sqlServerTypeMappingSource);
        }
Пример #9
0
        private static IMutableModel DbContextModel(Type typeRow)
        {
            // EF Core 2.1
            var typeMappingSource = new SqlServerTypeMappingSource(new TypeMappingSourceDependencies(new ValueConverterSelector(new ValueConverterSelectorDependencies())), new RelationalTypeMappingSourceDependencies());

            var conventionSet = SqlServerConventionSetBuilder.Build();
            var builder       = new ModelBuilder(conventionSet);

            // Build model
            var entity = builder.Entity(typeRow);
            SqlTableAttribute tableAttribute = (SqlTableAttribute)typeRow.GetTypeInfo().GetCustomAttribute(typeof(SqlTableAttribute));

            entity.ToTable(tableAttribute.SqlTableName, tableAttribute.SqlSchemaName); // By default EF maps sql table name to class name.
            PropertyInfo[] propertyInfoList = TypeRowToPropertyList(typeRow);
            bool           isPrimaryKey     = false;                                   // Sql view

            foreach (PropertyInfo propertyInfo in propertyInfoList)
            {
                SqlFieldAttribute columnAttribute = (SqlFieldAttribute)propertyInfo.GetCustomAttribute(typeof(SqlFieldAttribute));
                if (columnAttribute == null || columnAttribute.SqlFieldName == null) // Calculated column. Do not include it in sql select.
                {
                    entity.Ignore(propertyInfo.Name);
                }
                else
                {
                    if (columnAttribute.IsPrimaryKey)
                    {
                        isPrimaryKey = true;
                        entity.HasKey(propertyInfo.Name); // Prevent null exception if primary key name is not "Id".
                    }
                    entity.Property(propertyInfo.PropertyType, propertyInfo.Name).HasColumnName(columnAttribute.SqlFieldName);
                    CoreTypeMapping coreTypeMapping = typeMappingSource.FindMapping(propertyInfo.PropertyType);
                    Debug.Assert(coreTypeMapping != null);
                    entity.Property(propertyInfo.PropertyType, propertyInfo.Name).HasAnnotation(CoreAnnotationNames.TypeMapping, coreTypeMapping);
                }
            }

            if (isPrimaryKey == false)
            {
                entity.HasKey(propertyInfoList.First().Name); // Prevent null exception if name of first field (of view) is not "Id".
            }

            var model = builder.Model;

            return(model);
        }
    public void MaxBatchSize_is_optional()
    {
        var optionsBuilder = new DbContextOptionsBuilder();

        optionsBuilder.UseSqlServer("Database=Crunchie");

        var typeMapper = new SqlServerTypeMappingSource(
            TestServiceFactory.Instance.Create <TypeMappingSourceDependencies>(),
            TestServiceFactory.Instance.Create <RelationalTypeMappingSourceDependencies>());

        var logger = new FakeRelationalCommandDiagnosticsLogger();

        var factory = new SqlServerModificationCommandBatchFactory(
            new ModificationCommandBatchFactoryDependencies(
                new RelationalCommandBuilderFactory(
                    new RelationalCommandBuilderDependencies(
                        typeMapper,
                        new SqlServerExceptionDetector())),
                new SqlServerSqlGenerationHelper(
                    new RelationalSqlGenerationHelperDependencies()),
                new SqlServerUpdateSqlGenerator(
                    new UpdateSqlGeneratorDependencies(
                        new SqlServerSqlGenerationHelper(
                            new RelationalSqlGenerationHelperDependencies()),
                        typeMapper)),
                new TypedRelationalValueBufferFactoryFactory(
                    new RelationalValueBufferFactoryDependencies(
                        typeMapper, new CoreSingletonOptions())),
                new CurrentDbContext(new FakeDbContext()),
                logger),
            optionsBuilder.Options);

        var batch = factory.Create();

        Assert.True(batch.AddCommand(CreateModificationCommand("T1", null, false)));
        Assert.True(batch.AddCommand(CreateModificationCommand("T1", null, false)));
    }
        public void Uses_MaxBatchSize_specified_in_SqlServerOptionsExtension()
        {
            var optionsBuilder = new DbContextOptionsBuilder();

            optionsBuilder.UseSqlServer("Database=Crunchie", b => b.MaxBatchSize(1));

            var typeMapper = new SqlServerTypeMappingSource(
                TestServiceFactory.Instance.Create <TypeMappingSourceDependencies>(),
                TestServiceFactory.Instance.Create <RelationalTypeMappingSourceDependencies>());

            var logger = new FakeRelationalCommandDiagnosticsLogger();

            var factory = new SqlServerModificationCommandBatchFactory(
                new ModificationCommandBatchFactoryDependencies(
                    new RelationalCommandBuilderFactory(
                        new RelationalCommandBuilderDependencies(
                            typeMapper)),
                    new SqlServerSqlGenerationHelper(
                        new RelationalSqlGenerationHelperDependencies()),
                    new SqlServerUpdateSqlGenerator(
                        new UpdateSqlGeneratorDependencies(
                            new SqlServerSqlGenerationHelper(
                                new RelationalSqlGenerationHelperDependencies()),
                            typeMapper)),
                    new TypedRelationalValueBufferFactoryFactory(
                        new RelationalValueBufferFactoryDependencies(
                            typeMapper, new CoreSingletonOptions())),
                    new CurrentDbContext(new FakeDbContext()),
                    logger),
                optionsBuilder.Options);

            var batch = factory.Create();

            Assert.True(batch.AddCommand(new ModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, false, null)));
            Assert.False(batch.AddCommand(new ModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, false, null)));
        }
Пример #12
0
        private IMigrationsScaffolder CreateMigrationScaffolder <TContext>()
            where TContext : DbContext, new()
        {
            var currentContext             = new CurrentDbContext(new TContext());
            var idGenerator                = new MigrationsIdGenerator();
            var sqlServerTypeMappingSource = new SqlServerTypeMappingSource(
                TestServiceFactory.Instance.Create <TypeMappingSourceDependencies>(),
                TestServiceFactory.Instance.Create <RelationalTypeMappingSourceDependencies>());
            var sqlServerAnnotationCodeGenerator = new SqlServerAnnotationCodeGenerator(
                new AnnotationCodeGeneratorDependencies(sqlServerTypeMappingSource));
            var code     = new CSharpHelper(sqlServerTypeMappingSource);
            var reporter = new TestOperationReporter();
            var migrationAssembly
                = new MigrationsAssembly(
                      currentContext,
                      new DbContextOptions <TContext>().WithExtension(new FakeRelationalOptionsExtension()),
                      idGenerator,
                      new FakeDiagnosticsLogger <DbLoggerCategory.Migrations>());
            var historyRepository = new MockHistoryRepository();

            var services = RelationalTestHelpers.Instance.CreateContextServices();
            var model    = new Model().FinalizeModel();

            model.AddRuntimeAnnotation(RelationalAnnotationNames.RelationalModel, new RelationalModel(model));

            return(new MigrationsScaffolder(
                       new MigrationsScaffolderDependencies(
                           currentContext,
                           model,
                           migrationAssembly,
                           new MigrationsModelDiffer(
                               new TestRelationalTypeMappingSource(
                                   TestServiceFactory.Instance.Create <TypeMappingSourceDependencies>(),
                                   TestServiceFactory.Instance.Create <RelationalTypeMappingSourceDependencies>()),
                               new MigrationsAnnotationProvider(
                                   new MigrationsAnnotationProviderDependencies()),
                               services.GetRequiredService <IChangeDetector>(),
                               services.GetRequiredService <IUpdateAdapterFactory>(),
                               services.GetRequiredService <CommandBatchPreparerDependencies>()),
                           idGenerator,
                           new MigrationsCodeGeneratorSelector(
                               new[]
            {
                new CSharpMigrationsGenerator(
                    new MigrationsCodeGeneratorDependencies(
                        sqlServerTypeMappingSource,
                        sqlServerAnnotationCodeGenerator),
                    new CSharpMigrationsGeneratorDependencies(
                        code,
                        new CSharpMigrationOperationGenerator(
                            new CSharpMigrationOperationGeneratorDependencies(
                                code)),
                        new CSharpSnapshotGenerator(
                            new CSharpSnapshotGeneratorDependencies(
                                code, sqlServerTypeMappingSource, sqlServerAnnotationCodeGenerator))))
            }),
                           historyRepository,
                           reporter,
                           new MockProvider(),
                           new SnapshotModelProcessor(reporter, services.GetRequiredService <IModelRuntimeInitializer>()),
                           new Migrator(
                               migrationAssembly,
                               historyRepository,
                               services.GetRequiredService <IDatabaseCreator>(),
                               services.GetRequiredService <IMigrationsSqlGenerator>(),
                               services.GetRequiredService <IRawSqlCommandBuilder>(),
                               services.GetRequiredService <IMigrationCommandExecutor>(),
                               services.GetRequiredService <IRelationalConnection>(),
                               services.GetRequiredService <ISqlGenerationHelper>(),
                               services.GetRequiredService <ICurrentDbContext>(),
                               services.GetRequiredService <IModelRuntimeInitializer>(),
                               services.GetRequiredService <IDiagnosticsLogger <DbLoggerCategory.Migrations> >(),
                               services.GetRequiredService <IRelationalCommandDiagnosticsLogger>(),
                               services.GetRequiredService <IDatabaseProvider>()))));
        }