示例#1
0
        public void ConfigureReactivatedDate_TwoGeneric_Success_Should_AddReactivatedDateAndReactivatedByConventionToBuilder()
        {
            var conventionSet = ConventionSet.CreateConventionSet(_Context);
            var modelBuilder  = new ModelBuilder(conventionSet);

            var entityTypeBuilder = modelBuilder.Entity <TestAggregateRoot>();

            entityTypeBuilder.ConfigureReactivatedDate <TestAggregateRoot, Guid?>();

            var inactiveProperty = entityTypeBuilder.Metadata
                                   .FindDeclaredProperty(nameof(TestAggregateRoot.Inactive));

            inactiveProperty.IsNullable.Should().BeFalse();

            var deactivatedDateProperty = entityTypeBuilder.Metadata
                                          .FindDeclaredProperty(nameof(TestAggregateRoot.DeactivatedDate));

            deactivatedDateProperty.IsNullable.Should().BeTrue();

            var deactivatedByProperty = entityTypeBuilder.Metadata
                                        .FindDeclaredProperty(nameof(TestAggregateRoot.DeactivatedBy));

            deactivatedByProperty.IsNullable.Should().BeTrue();

            var reactivatedDateProperty = entityTypeBuilder.Metadata
                                          .FindDeclaredProperty(nameof(TestAggregateRoot.ReactivatedDate));

            reactivatedDateProperty.IsNullable.Should().BeTrue();

            var reactivatedByProperty = entityTypeBuilder.Metadata
                                        .FindDeclaredProperty(nameof(TestAggregateRoot.ReactivatedBy));

            reactivatedByProperty.IsNullable.Should().BeTrue();
        }
示例#2
0
        public void ConfigurationBase_ValidateConfigProperties_ConfigDefaults()
        {
            // Arrange
            var builder = new DbContextOptionsBuilder <CodeDesignPlusContextInMemory>();

            var options = builder.UseInMemoryDatabase(nameof(EFCoreExtensionsTest)).Options;

            var context = new CodeDesignPlusContextInMemory(options);

            var modelBuilder = new ModelBuilder(ConventionSet.CreateConventionSet(context));

            var entityTypeBuilder = modelBuilder.Entity <Permission>();

            var customerEntityConfiguration = new PermissionEntityConfiguration();

            customerEntityConfiguration.Configure(entityTypeBuilder);

            // Act
            var idProperty            = entityTypeBuilder.Metadata.FindDeclaredProperty(nameof(Permission.Id));
            var idUserCreatorProperty = entityTypeBuilder.Metadata.FindDeclaredProperty(nameof(Permission.IdUserCreator));
            var stateProperty         = entityTypeBuilder.Metadata.FindDeclaredProperty(nameof(Permission.State));
            var dateCreatedProperty   = entityTypeBuilder.Metadata.FindDeclaredProperty(nameof(Permission.DateCreated));

            // Assert
            Assert.True(idProperty.IsPrimaryKey());
            Assert.False(idProperty.IsNullable);
            Assert.Equal(ValueGenerated.OnAdd, idProperty.ValueGenerated);
            Assert.False(idUserCreatorProperty.IsNullable);
            Assert.False(stateProperty.IsNullable);
            Assert.False(dateCreatedProperty.IsNullable);
        }
示例#3
0
        public static DynamicDbContext Create(DbContext dbContext, DbContextOptions options, DbConnection connection, Type[] typesOfModel)
        {
            var conventionSet = ConventionSet.CreateConventionSet(dbContext);

            var modelBuilder = new ModelBuilder(conventionSet);

            foreach (var type in typesOfModel)
            {
                modelBuilder.Entity(type);
            }

            foreach (var t in modelBuilder.Model.GetEntityTypes().ToList())
            {
                if (!typesOfModel.Contains(t.ClrType))
                {
                    // workaround return-type change between 3.1.11 and 5.0.2
                    dynamic model = modelBuilder.Model;
                    model.RemoveEntityType(t.ClrType);
                }
            }

            modelBuilder.BuildIndexesFromAnnotations();

            return(new DynamicDbContext(
                       options,
                       connection,
                       modelBuilder.Model.FinalizeModel()
                       ));
        }
        public void ConfigureReactivatedDate_OneGeneric_Success_Should_AddReactivatedDateConventionToBuilder()
        {
            var conventionSet = ConventionSet.CreateConventionSet(_Context);
            var modelBuilder  = new ModelBuilder(conventionSet);

            var entityTypeBuilder = modelBuilder.Entity <TestAggregateRoot>();
            var ownedBuilder      = entityTypeBuilder.OwnsMany <TestEntity>(nameof(TestAggregateRoot.TestEntities));

            ownedBuilder.ConfigureReactivatedDate <TestAggregateRoot, TestEntity, Guid?>();

            var inactiveProperty = ownedBuilder.OwnedEntityType
                                   .FindDeclaredProperty(nameof(TestAggregateRoot.Inactive));

            inactiveProperty.IsNullable.Should().BeFalse();

            var reactivatedDateProperty = ownedBuilder.OwnedEntityType
                                          .FindDeclaredProperty(nameof(TestAggregateRoot.DeactivatedDate));

            reactivatedDateProperty.IsNullable.Should().BeTrue();

            var deactivatedDateProperty = ownedBuilder.OwnedEntityType
                                          .FindDeclaredProperty(nameof(TestAggregateRoot.ReactivatedDate));

            deactivatedDateProperty.IsNullable.Should().BeTrue();
        }
示例#5
0
        public IModel CreateModel(DbContextOptions options)
        {
            var conventions  = ConventionSet.CreateConventionSet(new KosystemDbContext(options));
            var modelBuilder = new ModelBuilder(conventions);

            OnModelCreating(modelBuilder);
            return(modelBuilder.FinalizeModel());
        }
        public static ConventionSet Build()
        {
            // TODO: is this correct?
            var optionsBuilder = new DbContextOptionsBuilder();
            var context        = new DbContext(optionsBuilder.Options);

            return(ConventionSet.CreateConventionSet(context));
        }
示例#7
0
        public static ModelBuilder GetTestModelBuilder()
        {
            var testdBContext = GetTestDbContext();
            // Get the convention set for this db
            var conventionSet = ConventionSet.CreateConventionSet(testdBContext);

            // Now create the ModelBuilder
            return(new ModelBuilder(conventionSet));
        }
        public static ModelBuilder GetModelBuilder()
        {
            using var serviceScope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope();

            using var context = serviceScope.ServiceProvider.GetService <TestContext>();

            var conventionSet = ConventionSet.CreateConventionSet(context);

            return(new ModelBuilder(conventionSet));
        }
        /// <summary>
        ///     <para>
        ///         Call this method to build a <see cref="ConventionSet" /> for the in-memory provider when using
        ///         the <see cref="ModelBuilder" /> outside of <see cref="DbContext.OnModelCreating" />.
        ///     </para>
        ///     <para>
        ///         Note that it is unusual to use this method.
        ///         Consider using <see cref="DbContext" /> in the normal way instead.
        ///     </para>
        /// </summary>
        /// <returns> The convention set. </returns>
        public static ConventionSet Build()
        {
            var serviceProvider = new ServiceCollection()
                                  .AddEntityFrameworkHarmonyDatabase()
                                  .AddDbContext <DbContext>(
                (p, o) =>
                o.UseHarmonyDatabase(p.GetService(typeof(IDataObjectProvider)) as IDataObjectProvider))
                                  .BuildServiceProvider();

            using var serviceScope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope();
            using var context      = serviceScope.ServiceProvider.GetService <DbContext>();
            return(ConventionSet.CreateConventionSet(context));
        }
示例#10
0
        public void ConfigureUpdatedDate_OneGeneric_Success_Should_AddUpdatedDateConventionToBuilder()
        {
            var conventionSet = ConventionSet.CreateConventionSet(_Context);
            var modelBuilder  = new ModelBuilder(conventionSet);

            var entityTypeBuilder = modelBuilder.Entity <TestAggregateRoot>();

            entityTypeBuilder.ConfigureUpdatedDate <TestAggregateRoot, Guid?>();

            var updatedDateProperty = entityTypeBuilder.Metadata
                                      .FindDeclaredProperty(nameof(TestAggregateRoot.UpdatedDate));

            updatedDateProperty.IsNullable.Should().BeTrue();
        }
示例#11
0
        public void ConfigureEntity_Success_Should_AddKeyConventionToBuilder()
        {
            var conventionSet = ConventionSet.CreateConventionSet(_Context);
            var modelBuilder  = new ModelBuilder(conventionSet);

            var entityTypeBuilder = modelBuilder.Entity <TestAggregateRoot>();

            entityTypeBuilder.ConfigureEntity <TestAggregateRoot, Guid>();

            var idProperty = entityTypeBuilder.Metadata
                             .FindDeclaredProperty(nameof(TestAggregateRoot.Id));

            idProperty.IsKey().Should().BeTrue();
        }
        /// <summary>
        ///     <para>
        ///         Call this method to build a <see cref="ConventionSet" /> for the in-memory provider when using
        ///         the <see cref="ModelBuilder" /> outside of <see cref="DbContext.OnModelCreating" />.
        ///     </para>
        ///     <para>
        ///         Note that it is unusual to use this method.
        ///         Consider using <see cref="DbContext" /> in the normal way instead.
        ///     </para>
        /// </summary>
        /// <returns> The convention set. </returns>
        public static ConventionSet Build()
        {
            var serviceProvider = new ServiceCollection()
                                  .AddEntityFrameworkInMemoryDatabase()
                                  .AddDbContext <DbContext>(
                (p, o) =>
                o.UseInMemoryDatabase(Guid.NewGuid().ToString())
                .UseInternalServiceProvider(p))
                                  .BuildServiceProvider();

            using var serviceScope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope();
            using var context      = serviceScope.ServiceProvider.GetService <DbContext>();
            return(ConventionSet.CreateConventionSet(context));
        }
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public static ConventionSet Build()
        {
            var serviceProvider = new ServiceCollection()
                                  .AddEntityFrameworkJet()
                                  .AddDbContext <DbContext>(o => o.UseJet("Provider=Microsoft.ACE.OLEDB.15.0;Data Source=_.accdb;"))
                                  .BuildServiceProvider();

            using (var serviceScope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                using (var context = serviceScope.ServiceProvider.GetService <DbContext>())
                {
                    return(ConventionSet.CreateConventionSet(context));
                }
            }
        }
        public void ConfigureEntity_Success_Should_AddKeyConventionToBuilder()
        {
            var conventionSet = ConventionSet.CreateConventionSet(_Context);
            var modelBuilder  = new ModelBuilder(conventionSet);

            var entityTypeBuilder = modelBuilder.Entity <TestAggregateRoot>();
            var ownedBuilder      = entityTypeBuilder.OwnsMany <TestEntity>(nameof(TestAggregateRoot.TestEntities));

            ownedBuilder.ConfigureEntity <TestAggregateRoot, TestEntity, Guid>();

            var idProperty = ownedBuilder.OwnedEntityType
                             .FindDeclaredProperty(nameof(TestAggregateRoot.Id));

            idProperty.IsKey().Should().BeTrue();
        }
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public static ConventionSet Build()
        {
            var serviceProvider = new ServiceCollection()
                                  .AddEntityFrameworkLiteDBDatabase()
                                  .AddDbContext <DbContext>(o => o.UseLiteDB(Guid.NewGuid().ToString()))
                                  .BuildServiceProvider();

            using (var serviceScope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                using (var context = serviceScope.ServiceProvider.GetService <DbContext>())
                {
                    return(ConventionSet.CreateConventionSet(context));
                }
            }
        }
        public static ConventionSet Build()
        {
            var serviceProvider = new ServiceCollection()
                                  .AddEntityFrameworkFirebird()
                                  .AddDbContext <DbContext>(o => o.UseFirebird("database=localhost:_.fdb;user=sysdba;password=masterkey;charset=utf8"))
                                  .BuildServiceProvider();

            using (var serviceScope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                using (var context = serviceScope.ServiceProvider.GetService <DbContext>())
                {
                    return(ConventionSet.CreateConventionSet(context));
                }
            }
        }
示例#17
0
        public static ConventionSet Build()
        {
            var serviceProvider = new ServiceCollection()
                                  .AddEntityFrameworkOracle()
                                  .AddDbContext <DbContext>(o => o.UseOracle("Data Source=."))
                                  .BuildServiceProvider();

            using (var serviceScope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                using (var context = serviceScope.ServiceProvider.GetService <DbContext>())
                {
                    return(ConventionSet.CreateConventionSet(context));
                }
            }
        }
        /// <summary>
        ///     <para>
        ///         Call this method to build a <see cref="ConventionSet" /> for SQL Server when using
        ///         the <see cref="ModelBuilder" /> outside of <see cref="DbContext.OnModelCreating" />.
        ///     </para>
        ///     <para>
        ///         Note that it is unusual to use this method.
        ///         Consider using <see cref="DbContext" /> in the normal way instead.
        ///     </para>
        /// </summary>
        /// <returns> The convention set. </returns>
        public static ConventionSet Build()
        {
            var serviceProvider = new ServiceCollection()
                                  .AddEntityFrameworkAse()
                                  .AddDbContext <DbContext>(
                (p, o) =>
                o.UseAse("Server=.")
                .UseInternalServiceProvider(p))
                                  .BuildServiceProvider();

            using (var serviceScope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                using (var context = serviceScope.ServiceProvider.GetService <DbContext>())
                {
                    return(ConventionSet.CreateConventionSet(context));
                }
            }
        }
示例#19
0
        public static void RegisterInContainer(IServiceCollection services, IEnumerable <Assembly> typeConfigurationAssemblies, GlobalFilters filters = null)
        #endregion
        {
            Guard.AgainstNull(nameof(services), services);

            var builder = new DbContextOptionsBuilder();

            builder.UseSqlServer("fake");
            using (var context = new DbContext(builder.Options))
            {
                var modelBuilder = new ModelBuilder(ConventionSet.CreateConventionSet(context));
                foreach (var assembly in typeConfigurationAssemblies)
                {
                    modelBuilder.ApplyConfigurationsFromAssembly(assembly);
                }

                RegisterInContainer(services, modelBuilder.Model, filters);
            }
        }
示例#20
0
        /// <summary>
        /// Returns the <see cref="EntityTypeBuilder"/> of the given entity
        /// </summary>
        /// <typeparam name="TEntity">The entity</typeparam>
        /// <typeparam name="TEntityConfiguration">The entitie's configuration</typeparam>
        /// <returns>EntityTypeBuilder<TEntity></returns>
        public static EntityTypeBuilder <TEntity> GetEntityTypeBuilder <TEntity, TEntityConfiguration>()
            where TEntity : class
            where TEntityConfiguration : IEntityTypeConfiguration <TEntity>, new()
        {
            var options = new DbContextOptionsBuilder <ShopDbContext>()
                          .UseSqlite(new SqliteConnection("DataSource=:memory:"))
                          .Options;

            var sut           = new ShopDbContext(options);
            var conventionSet = ConventionSet.CreateConventionSet(sut);
            var modelBuilder  = new ModelBuilder(conventionSet);

            var entityBuilder = modelBuilder.Entity <TEntity>();
            var entityConfig  = new TEntityConfiguration();

            entityConfig.Configure(entityBuilder);

            return(entityBuilder);
        }
        public void ConfigureCreatedDate_TwoGeneric_Success_Should_AddCreatedDateAndCreatedByConventionToBuilder()
        {
            var conventionSet = ConventionSet.CreateConventionSet(_Context);
            var modelBuilder  = new ModelBuilder(conventionSet);

            var entityTypeBuilder = modelBuilder.Entity <TestAggregateRoot>();
            var ownedBuilder      = entityTypeBuilder.OwnsMany <TestEntity>(nameof(TestAggregateRoot.TestEntities));

            ownedBuilder.ConfigureCreatedDate <TestAggregateRoot, TestEntity, Guid?>();

            var createdDateProperty = ownedBuilder.OwnedEntityType
                                      .FindDeclaredProperty(nameof(TestAggregateRoot.CreatedDate));

            createdDateProperty.IsNullable.Should().BeFalse();

            var createdByProperty = ownedBuilder.OwnedEntityType
                                    .FindDeclaredProperty(nameof(TestAggregateRoot.CreatedBy));

            createdByProperty.IsNullable.Should().BeFalse();
        }
        public ConfigurationBaseTest()
        {
            // Construct the optionsBuilder using InMemory SqlLite
            var options = new DbContextOptionsBuilder <RepositoryContext>()
                          .UseSqlite(new SqliteConnection("DataSource=:memory:"))
                          .Options;

            var usersConfiguration    = new UsersConfiguration();
            var citizensConfiguration = new CitizensConfiguration();
            var citiesConfiguration   = new CitiesConfiguration();
            var statesConfiguration   = new StatesConfiguration();

            var sut = new RepositoryContext(options, usersConfiguration, citizensConfiguration, citiesConfiguration, statesConfiguration);

            // Get the convention set for this db
            var conventionSet = ConventionSet.CreateConventionSet(sut);

            // Now create the ModelBuilder
            ModelBuilder = new ModelBuilder(conventionSet);
        }
 /// <summary>
 ///     Call this method to build a <see cref="ConventionSet" /> for SQL Server when using
 ///     the <see cref="ModelBuilder" /> outside of <see cref="DbContext.OnModelCreating" />.
 /// </summary>
 /// <remarks>
 ///     Note that it is unusual to use this method. Consider using <see cref="DbContext" /> in the normal way instead.
 /// </remarks>
 /// <returns>The convention set.</returns>
 public static ConventionSet Build()
 {
     using var serviceScope = CreateServiceScope();
     using var context      = serviceScope.ServiceProvider.GetRequiredService <DbContext>();
     return(ConventionSet.CreateConventionSet(context));
 }
 /// <summary>
 ///     Call this method to build a <see cref="ModelBuilder" /> for SQL Server outside of <see cref="DbContext.OnModelCreating" />.
 /// </summary>
 /// <remarks>
 ///     Note that it is unusual to use this method. Consider using <see cref="DbContext" /> in the normal way instead.
 /// </remarks>
 /// <returns>The convention set.</returns>
 public static ModelBuilder CreateModelBuilder()
 {
     using var serviceScope = CreateServiceScope();
     using var context      = serviceScope.ServiceProvider.GetRequiredService <DbContext>();
     return(new ModelBuilder(ConventionSet.CreateConventionSet(context), context.GetService <ModelDependencies>()));
 }
 public static ConventionSet Build()
 => ConventionSet.CreateConventionSet(RelationalTestHelpers.Instance.CreateContext());