Exemplo n.º 1
0
        public void Should_throw_if_expression_is_null()
        {
            // ReSharper disable once AssignNullToNotNullAttribute
            Action action = () => EntityMembersProvider.From <TestEntity>(null);

            action.Should().Throw <ArgumentNullException>();
        }
Exemplo n.º 2
0
        public void Should_extract_property_accessor()
        {
            var propertiesProvider = EntityMembersProvider.From <TestEntity>(entity => entity.Id);

            var properties = propertiesProvider.GetMembers();

            properties.Should().HaveCount(1);
            properties.Should().Contain(typeof(TestEntity).GetProperty(nameof(TestEntity.Id)));
        }
Exemplo n.º 3
0
        public void Should_extract_property_accessor()
        {
            var propertiesProvider = EntityMembersProvider.From <TestEntity>(entity => entity.Id);

            var properties = propertiesProvider.GetMembers();

            properties.Should().HaveCount(1);
            var idProperty = typeof(TestEntity).GetProperty(nameof(TestEntity.Id)) ?? throw new Exception($"Property {nameof(TestEntity.Id)} not found.");

            properties.Should().Contain(idProperty);
        }
Exemplo n.º 4
0
        public static Task BulkInsertAsync <T>([NotNull] this DbContext ctx,
                                               [NotNull] IEnumerable <T> entities,
                                               [NotNull] Expression <Func <T, object> > propertiesToInsert,
                                               CancellationToken cancellationToken = default)
            where T : class
        {
            var options = new SqlBulkInsertOptions {
                EntityMembersProvider = EntityMembersProvider.From(propertiesToInsert)
            };

            return(BulkInsertAsync(ctx, entities, options, cancellationToken));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Copies <paramref name="entities"/> into a table.
        /// </summary>
        /// <param name="ctx">Database context.</param>
        /// <param name="entities">Entities to insert.</param>
        /// <param name="propertiesToInsert">Properties to insert.</param>
        /// <param name="cancellationToken">Cancellation token.</param>
        /// <typeparam name="T">Entity type.</typeparam>
        /// <exception cref="ArgumentNullException"> <paramref name="ctx"/> or <paramref name="entities"/> is <c>null</c>.</exception>
        public static Task BulkInsertAsync <T>(this DbContext ctx,
                                               IEnumerable <T> entities,
                                               Expression <Func <T, object> > propertiesToInsert,
                                               CancellationToken cancellationToken = default)
            where T : class
        {
            var bulkInsertExecutor = ctx.GetService <IBulkOperationExecutor>();

            var options = bulkInsertExecutor.CreateOptions();

            options.MembersToInsert = EntityMembersProvider.From(propertiesToInsert);

            return(BulkInsertAsync(bulkInsertExecutor, entities, options, cancellationToken));
        }
Exemplo n.º 6
0
        public async Task Should_create_temp_table_with_provided_column_only()
        {
            ConfigureModel = builder => builder.ConfigureTempTableEntity <CustomTempTable>();

            _optionsWithNonUniqueName.MembersToInclude = EntityMembersProvider.From <CustomTempTable>(t => t.Column1);

            // ReSharper disable once RedundantArgumentDefaultValue
            await using var tempTable = await SUT.CreateTempTableAsync(ActDbContext.GetEntityType <CustomTempTable>(), _optionsWithNonUniqueName);

            var columns = AssertDbContext.GetTempTableColumns <CustomTempTable>().ToList();

            columns.Should().HaveCount(1);

            ValidateColumn(columns[0], nameof(CustomTempTable.Column1), "int", false);
        }
Exemplo n.º 7
0
        public void Should_throw_if_some_pk_columns_are_missing()
        {
            _optionsWithNonUniqueName.PrimaryKeyCreation = PrimaryKeyPropertiesProviders.EntityTypeConfiguration;
            _optionsWithNonUniqueName.MembersToInclude   = EntityMembersProvider.From <CustomTempTable>(t => t.Column1);

            ConfigureModel = builder =>
            {
                var entity = builder.ConfigureTempTableEntity <CustomTempTable>(false);
                entity.Property(s => s.Column2).HasMaxLength(100);
                entity.HasKey(s => new { s.Column1, s.Column2 });
            };

            // ReSharper disable once RedundantArgumentDefaultValue
            SUT.Awaiting(sut => sut.CreateTempTableAsync(ActDbContext.GetEntityType <CustomTempTable>(), _optionsWithNonUniqueName))
            .Should().Throw <ArgumentException>().WithMessage(@"Cannot create PRIMARY KEY because not all key columns are part of the temp table.
You may use other key properties providers like 'PrimaryKeyPropertiesProviders.AdaptiveEntityTypeConfiguration' instead of 'PrimaryKeyPropertiesProviders.EntityTypeConfiguration' to get different behaviors.
Missing columns: Column2.");
        }
Exemplo n.º 8
0
        public async Task Should_not_throw_if_some_pk_columns_are_missing_and_provider_is_Adaptive()
        {
            _optionsWithNonUniqueName.PrimaryKeyCreation = PrimaryKeyPropertiesProviders.AdaptiveForced;
            _optionsWithNonUniqueName.MembersToInclude   = EntityMembersProvider.From <CustomTempTable>(t => t.Column1);

            ConfigureModel = builder =>
            {
                var entity = builder.ConfigureTempTableEntity <CustomTempTable>(false);
                entity.Property(s => s.Column2).HasMaxLength(100);
                entity.HasKey(s => new { s.Column1, s.Column2 });
            };

            // ReSharper disable once RedundantArgumentDefaultValue
            await using var tempTable = await SUT.CreateTempTableAsync(ActDbContext.GetEntityType <CustomTempTable>(), _optionsWithNonUniqueName);

            var keyColumns = await AssertDbContext.GetTempTableKeyColumns <CustomTempTable>().ToListAsync();

            keyColumns.Should().HaveCount(1);
            keyColumns[0].COLUMN_NAME.Should().Be(nameof(CustomTempTable.Column1));
        }
Exemplo n.º 9
0
        public async Task Should_write_all_provided_column_values_as_is_despite_dotnet_default_value()
        {
            var testEntity = new TestEntityWithDotnetDefaultValues
            {
                Id             = Guid.Empty,
                Int            = 0,
                String         = null !,
                NullableInt    = null,
                NullableString = null
            };
            var testEntities = new[] { testEntity };

            var options = new SqliteBulkInsertOptions
            {
                // we skip TestEntityWithDefaultValues.String
                MembersToInsert = EntityMembersProvider.From <TestEntityWithDotnetDefaultValues>(e => new
                {
                    e.Id,
                    e.Int,
                    e.NullableInt,
                    e.NullableString
                })
            };

            await SUT.BulkInsertAsync(testEntities, options);

            var loadedEntity = await AssertDbContext.TestEntitiesWithDotnetDefaultValues.FirstOrDefaultAsync();

            loadedEntity.Should().BeEquivalentTo(new TestEntityWithSqlDefaultValues
            {
                Id             = Guid.Empty,                           // persisted as-is
                Int            = 0,                                    // persisted as-is
                NullableInt    = null,                                 // persisted as-is
                String         = "3",                                  // DEFAULT value constraint
                NullableString = null                                  // persisted as-is
            });
        }
Exemplo n.º 10
0
        public void Should_throw_if_no_properties_provided()
        {
            Action action = () => EntityMembersProvider.From <TestEntity>(entity => new { });

            action.Should().Throw <ArgumentException>();
        }
Exemplo n.º 11
0
        public void Should_throw_if_expression_return_constant()
        {
            Action action = () => EntityMembersProvider.From <TestEntity>(entity => null);

            action.Should().Throw <NotSupportedException>();
        }