Esempio n. 1
0
        private static IModel BuildDbCompiledModel(DatabaseType dbType)
        {
            ConventionSet conventionSet = null;

            switch (dbType)
            {
            case DatabaseType.SqlServer: conventionSet = SqlServerConventionSetBuilder.Build(); break;

            case DatabaseType.MySql: conventionSet = MySqlConventionSetBuilder.Build(); break;

            case DatabaseType.PostgreSql: conventionSet = NpgsqlConventionSetBuilder.Build(); break;

            case DatabaseType.Oracle: conventionSet = OracleConventionSetBuilder.Build(); break;

            default: throw new Exception("暂不支持该数据库!");
            }
            ModelBuilder modelBuilder = new ModelBuilder(conventionSet);

            _entityTypeMap.Values.ForEach(x =>
            {
                modelBuilder.Model.AddEntityType(x);
            });

            return(modelBuilder.FinalizeModel());
        }
Esempio n. 2
0
        private static DbCompiledModelInfo BuildDbCompiledModelInfo(string nameOrConStr, DatabaseType dbType)
        {
            lock (_buildCompiledModelLock)
            {
                ConventionSet conventionSet = null;
                switch (dbType)
                {
                case DatabaseType.SqlServer: conventionSet = SqlServerConventionSetBuilder.Build(); break;

                case DatabaseType.MySql: conventionSet = MySqlConventionSetBuilder.Build(); break;

                case DatabaseType.PostgreSql: conventionSet = NpgsqlConventionSetBuilder.Build(); break;

                case DatabaseType.Oracle: conventionSet = OracleConventionSetBuilder.Build(); break;

                default: throw new Exception("暂不支持该数据库!");
                }
                ModelBuilder modelBuilder = new ModelBuilder(conventionSet);
                _modelTypeMap.Values.ForEach(x =>
                {
                    modelBuilder.Model.AddEntityType(x);
                });

                DbCompiledModelInfo newInfo = new DbCompiledModelInfo
                {
                    ConStr       = nameOrConStr,
                    DatabaseType = dbType,
                    Model        = modelBuilder.FinalizeModel()
                };
                return(newInfo);
            }
        }
Esempio n. 3
0
        public void GenerateFluentApi_IKey_works_when_nonclustered(bool obsolete)
        {
            var generator    = new MySqlAnnotationCodeGenerator(new AnnotationCodeGeneratorDependencies());
            var modelBuilder = new ModelBuilder(MySqlConventionSetBuilder.Build());

            modelBuilder.Entity(
                "Post",
                x =>
            {
                x.Property <int>("Id");

                if (obsolete)
                {
#pragma warning disable 618
                    x.HasKey("Id").ForMySqlIsClustered(false);
#pragma warning restore 618
                }
                else
                {
                    x.HasKey("Id").IsClustered(false);
                }
            });
            var key        = modelBuilder.Model.FindEntityType("Post").GetKeys().Single();
            var annotation = key.FindAnnotation(MySqlAnnotationNames.Clustered);

            var result = generator.GenerateFluentApi(key, annotation);

            Assert.Equal("IsClustered", result.Method);

            Assert.Equal(1, result.Arguments.Count);
            Assert.Equal(false, result.Arguments[0]);
        }
Esempio n. 4
0
        public void GenerateFluentApi_IIndex_works_when_clustered(bool obsolete)
        {
            var generator    = new MySqlAnnotationCodeGenerator(new AnnotationCodeGeneratorDependencies());
            var modelBuilder = new ModelBuilder(MySqlConventionSetBuilder.Build());

            modelBuilder.Entity(
                "Post",
                x =>
            {
                x.Property <int>("Id");
                x.Property <string>("Name");
                if (obsolete)
                {
#pragma warning disable 618
                    x.HasIndex("Name").ForMySqlIsClustered();
#pragma warning restore 618
                }
                else
                {
                    x.HasIndex("Name").IsClustered();
                }
            });
            var index      = modelBuilder.Model.FindEntityType("Post").GetIndexes().Single();
            var annotation = index.FindAnnotation(MySqlAnnotationNames.Clustered);

            var result = generator.GenerateFluentApi(index, annotation);

            Assert.Equal("IsClustered", result.Method);

            Assert.Equal(0, result.Arguments.Count);
        }
Esempio n. 5
0
        public void GenerateFluentApi_IIndex_works_with_includes(bool obsolete)
        {
            var generator    = new MySqlAnnotationCodeGenerator(new AnnotationCodeGeneratorDependencies());
            var modelBuilder = new ModelBuilder(MySqlConventionSetBuilder.Build());

            modelBuilder.Entity(
                "Post",
                x =>
            {
                x.Property <int>("Id");
                x.Property <string>("FirstName");
                x.Property <string>("LastName");
                if (obsolete)
                {
#pragma warning disable 618
                    x.HasIndex("LastName").ForMySqlInclude("FirstName");
#pragma warning restore 618
                }
                else
                {
                    x.HasIndex("LastName").IncludeProperties("FirstName");
                }
            });
            var index      = modelBuilder.Model.FindEntityType("Post").GetIndexes().Single();
            var annotation = index.FindAnnotation(MySqlAnnotationNames.Include);

            var result = generator.GenerateFluentApi(index, annotation);

            Assert.Equal("IncludeProperties", result.Method);

            Assert.Equal(1, result.Arguments.Count);
            var properties = Assert.IsType <string[]>(result.Arguments[0]);
            Assert.Equal(new[] { "FirstName" }, properties.AsEnumerable());
        }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            var connectionString = TestEnvironment.GetDefaultConnectionString(_databaseName);
            var serverVersion    = ServerVersion.AutoDetect(connectionString);

            optionsBuilder
            .UseMySql(
                connectionString,
                serverVersion,
                b =>
            {
                b.ApplyConfiguration();
                _mySqlOptions?.Invoke(b);
            })
            .AddInterceptors(new CommandInterceptor(_commandLogger))
            .UseInternalServiceProvider(_serviceProvider);

            if (_model != null)
            {
                var conventionSet = MySqlConventionSetBuilder.Build();
                var modelBuilder  = new ModelBuilder(conventionSet);

                _model.Invoke(modelBuilder);

                var model = modelBuilder.FinalizeModel();
                optionsBuilder.UseModel(model);
            }

            _options?.Invoke(_serviceProvider, optionsBuilder);
        }
Esempio n. 7
0
        private IModel BuildDbCompiledModel()
        {
            if (ModelTypeMap.Count == 0)
            {
                InitModelType();
            }
            ConventionSet conventionSet = MySqlConventionSetBuilder.Build();
            ModelBuilder  modelBuilder  = new ModelBuilder(conventionSet);

            ModelTypeMap.Values.ForEach(x =>
            {
                modelBuilder.Model.AddEntityType(x);
            });
            return(modelBuilder.FinalizeModel());
        }
        public void GenerateFluentApi_IKey_works_when_clustered()
        {
            var generator    = new MySqlAnnotationCodeGenerator(new AnnotationCodeGeneratorDependencies());
            var modelBuilder = new ModelBuilder(MySqlConventionSetBuilder.Build());

            modelBuilder.Entity(
                "Post",
                x =>
            {
                x.Property <int>("Id");
                x.HasKey("Id").ForMySqlIsClustered();
            });
            var key        = modelBuilder.Model.FindEntityType("Post").GetKeys().Single();
            var annotation = key.FindAnnotation(MySqlAnnotationNames.Clustered);

            var result = generator.GenerateFluentApi(key, annotation);

            Assert.Equal("ForMySqlIsClustered", result.Method);

            Assert.Equal(0, result.Arguments.Count);
        }
        public void GenerateFluentApi_IIndex_works_when_nonclustered()
        {
            var generator    = new MySqlAnnotationCodeGenerator(new AnnotationCodeGeneratorDependencies());
            var modelBuilder = new ModelBuilder(MySqlConventionSetBuilder.Build());

            modelBuilder.Entity(
                "Post",
                x =>
            {
                x.Property <int>("Id");
                x.Property <string>("Name");
                x.HasIndex("Name").ForMySqlIsClustered(false);
            });
            var index      = modelBuilder.Model.FindEntityType("Post").GetIndexes().Single();
            var annotation = index.FindAnnotation(MySqlAnnotationNames.Clustered);

            var result = generator.GenerateFluentApi(index, annotation);

            Assert.Equal("ForMySqlIsClustered", result.Method);

            Assert.Equal(1, result.Arguments.Count);
            Assert.Equal(false, result.Arguments[0]);
        }
 public override ModelBuilder CreateModelBuilder()
 => new ModelBuilder(MySqlConventionSetBuilder.Build());
 protected ModelBuilder CreateConventionalModelBuilder() => new ModelBuilder(MySqlConventionSetBuilder.Build());