数据库
首页 > 数据库> > 关于Abp vnext Mysql 空间数据存储 NetTopologySuite记录

关于Abp vnext Mysql 空间数据存储 NetTopologySuite记录

作者:互联网

最近在项目中突然要用到空间存储、电子围栏,Mysql貌似要5.7以上才支持Geometry相关的存储。

顺便贴上ef core 空间数据的文档地址:https://docs.microsoft.com/zh-cn/ef/core/modeling/spatial
  1. 项目结构:

  abp版本:4.3.3

  Mysql版本: 8.0.18

mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.18 |
+-----------+
1 row in set (0.03 sec)

 

abp的高版本和低版本大同小异!!!

  我记得现在版本的abp venxt之后DbContext,高版本只需要配置一个就行了,低版本 *.EntityFrameworkCore 和 *.EntityFrameworkCore.DbMigrations  配置。

1.添加项目引用

 项目文件添加 ,这里可以Version主要看你当前EFCore版本

<PackageReference Include="Pomelo.EntityFrameworkCore.MySql.NetTopologySuite" Version="5.0.0 />

 

 

或者nuget

Install-Package Pomelo.EntityFrameworkCore.MySql.NetTopologySuite -Version 5.0.0

 

2.之后网上大多数都会直接,这样使用,但是这样是不行的哈。 包括官网的文档也是这样

  var builder = new DbContextOptionsBuilder<CustomerManagerMigrationsDbContext>()
                .UseMySql(configuration.GetConnectionString("Default") 
                    , optionsBuilder => optionsBuilder.UseNetTopologySuite());

数据库迁移的时候会莫名其妙的报一堆错。

正确构造builder

要重写 UseNetTopologySuite 方法 

 

 

扩展方法

    public static class MySqlNetTopologySuiteDbContextOptionsBuilderExtensions
    {
        public static Microsoft.EntityFrameworkCore.Infrastructure.MySqlDbContextOptionsBuilder UseNetTopologySuite(
            this Microsoft.EntityFrameworkCore.Infrastructure.MySqlDbContextOptionsBuilder optionsBuilder)
        {
            DbContextOptionsBuilder optionsBuilder1 =
                ((IRelationalDbContextOptionsBuilderInfrastructure)optionsBuilder).OptionsBuilder;
            ((IDbContextOptionsBuilderInfrastructure)optionsBuilder1)
                .AddOrUpdateExtension(CreateMySqlNetTopologySuiteOptionsExtension(optionsBuilder1));
            return optionsBuilder;
        }

        private static MySqlNetTopologySuiteOptionsExtension CreateMySqlNetTopologySuiteOptionsExtension(
            DbContextOptionsBuilder optionsBuilder)
        {
            return optionsBuilder.Options.FindExtension<MySqlNetTopologySuiteOptionsExtension>() ??
                   new MySqlNetTopologySuiteOptionsExtension();
        }
    }

 

*.EntityFrameworkCore 下的 EntityFrameworkCoreModule

        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            context.Services.AddAbpDbContext<CustomerManagerDbContext>(options =>
            {
                /* Remove "includeAllEntities: true" to create
                 * default repositories only for aggregate roots */
                options.AddDefaultRepositories(includeAllEntities: true);
            });

            Configure<AbpDbContextOptions>(options =>
            {
                /* The main point to change your DBMS.
                 * See also CustomerManagerMigrationsDbContextFactory for EF Core tooling. */
                options.UseMySQL(x => x.UseNetTopologySuite());
            });

        }

*.EntityFrameworkCore.DbMigrations 下的 MigrationsDbContextFactory

        public CustomerManagerMigrationsDbContext CreateDbContext(string[] args)
        {
            CustomerManagerEfCoreEntityExtensionMappings.Configure();

            var configuration = BuildConfiguration();

            var builder = new DbContextOptionsBuilder<CustomerManagerMigrationsDbContext>()
                .UseMySql(configuration.GetConnectionString("Default")
                    , MySqlServerVersion.LatestSupportedServerVersion
                    , optionsBuilder => optionsBuilder.UseNetTopologySuite());

            return new CustomerManagerMigrationsDbContext(builder.Options);
        }

差不多就这样,电子围栏 大家懂就是了 图就这意思:

 如果有其他更好的写法,欢迎大家讨论!!!

标签:vnext,Abp,DbContextOptionsBuilder,UseNetTopologySuite,空间数据,EntityFrameworkCore,o
来源: https://www.cnblogs.com/NorthMeditation/p/15966879.html