/// <summary>
        /// Configures the context to use NHibernate.
        /// </summary>
        /// <param name="source">The repository options builder.</param>
        /// <param name="cfgAction">A configuration action used to configure NHibernate.</param>
        /// <returns>The same builder instance.</returns>
        public static RepositoryOptionsBuilder UseNHibernate([NotNull] this RepositoryOptionsBuilder source, [NotNull] Action <Configuration> cfgAction)
        {
            Guard.NotNull(source, nameof(source));
            Guard.NotNull(cfgAction, nameof(cfgAction));

            var cfg = new Configuration();

            cfgAction(cfg);

            source.UseNHibernate(cfg.BuildSessionFactory());

            return(source);
        }
        protected IRepositoryOptions BuildOptions(ContextProviderType provider)
        {
            var builder = new RepositoryOptionsBuilder();

            switch (provider)
            {
            case ContextProviderType.InMemory:
            {
                builder.UseInMemoryDatabase(Guid.NewGuid().ToString());
                break;
            }

            case ContextProviderType.Json:
            {
                builder.UseJsonDatabase(Path.GetTempPath() + Guid.NewGuid().ToString("N"));
                break;
            }

            case ContextProviderType.Xml:
            {
                builder.UseJsonDatabase(Path.GetTempPath() + Guid.NewGuid().ToString("N"));
                break;
            }

            case ContextProviderType.AdoNet:
            {
                builder.UseAdoNet(_connection);
                break;
            }

            case ContextProviderType.NHibernate:
            {
                builder.UseNHibernate(cfg =>
                    {
                        cfg.DataBaseIntegration(x =>
                        {
                            x.Dialect <MsSql2012Dialect>();
                            x.Driver <SqlClientDriver>();
                            x.ConnectionString = ConnectionString;
                            x.LogSqlInConsole  = true;
                            x.LogFormattedSql  = true;
                        });

                        var mapper = new ModelMapper();

                        mapper.AddMappings(Assembly.GetExecutingAssembly().GetExportedTypes());

                        var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();

                        cfg.AddMapping(mapping);

                        var exporter = new SchemaExport(cfg);

                        exporter.Execute(true, true, false);
                    });

                break;
            }

            case ContextProviderType.EntityFramework:
            {
                builder.UseEntityFramework <EfDbContext>(_connection);
                break;
            }

            case ContextProviderType.EntityFrameworkCore:
            {
                builder.UseEntityFrameworkCore <EfCoreDbContext>(x => x.UseSqlServer(ConnectionString));
                break;
            }

            default:
                throw new ArgumentOutOfRangeException(nameof(provider));
            }

            return(builder.Options);
        }
示例#3
0
        protected RepositoryOptionsBuilder GetRepositoryOptionsBuilder(ContextProviderType provider)
        {
            var builder = new RepositoryOptionsBuilder();

            switch (provider)
            {
            case ContextProviderType.InMemory:
            {
                builder.UseInMemoryDatabase(Guid.NewGuid().ToString());
                break;
            }

            case ContextProviderType.Json:
            {
                builder.UseJsonDatabase(Path.GetTempPath() + Guid.NewGuid().ToString("N"));
                break;
            }

            case ContextProviderType.Xml:
            {
                builder.UseXmlDatabase(Path.GetTempPath() + Guid.NewGuid().ToString("N"));
                break;
            }

            case ContextProviderType.AdoNet:
            {
                builder.UseAdoNet(TestDbConnectionHelper.CreateConnection(), ensureDatabaseCreated: true);
                break;
            }

            case ContextProviderType.NHibernate:
            {
                builder.UseNHibernate(cfg =>
                    {
                        var currentFile      = TestPathHelper.GetTempFileName();
                        var connectionString = $"Data Source={currentFile};Persist Security Info=False";

                        cfg.DataBaseIntegration(x =>
                        {
                            x.Dialect <TestFixedMsSqlCe40Dialect>();
                            x.Driver <SqlServerCeDriver>();
                            x.ConnectionString = connectionString;
                            x.LogSqlInConsole  = true;
                            x.LogFormattedSql  = true;
                        });

                        var mapper = new ModelMapper();

                        mapper.AddMappings(Assembly.GetExecutingAssembly().GetExportedTypes());

                        var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();

                        cfg.AddMapping(mapping);

                        var exporter = new SchemaExport(cfg);

                        exporter.Execute(true, true, false);
                    });

                break;
            }

            case ContextProviderType.EntityFramework:
            {
                builder.UseEntityFramework <TestEfDbContext>(TestDbConnectionHelper.CreateConnection());
                break;
            }

            case ContextProviderType.EntityFrameworkCore:
            {
                builder.UseEntityFrameworkCore <TestEfCoreDbContext>(options =>
                    {
                        options
                        .UseInMemoryDatabase(Guid.NewGuid().ToString())
                        .ConfigureWarnings(x => x.Ignore(InMemoryEventId.TransactionIgnoredWarning));
                    });
                break;
            }

            default:
                throw new ArgumentOutOfRangeException(nameof(provider));
            }

            builder.UseLoggerProvider(TestXUnitLoggerProvider);

            return(builder);
        }