NpgsqlConnection关于TypeMapper已经被弃用疑问 返回

SqlSugar
9 173

在SqlSugar教程板块下,数据库特色模块中`PostgreSql`, 4、geometry/postgis 的描述中

//使用长链接 (如果不使用OpenAways需要将自动释放关闭手动Open Close)

涉及到(db.Ado.Connection as  NpgsqlConnection).TypeMapper

在Npgsql7.0.0中该属性被标记为Obsolete 应如何处理


热忱回答9

  • fate sta fate sta VIP0
    2022/11/28

    你可以百度一下,具体换成什么我也不清楚

    0 回复
  • 仁者 仁者 VIP0
    2022/11/28

    这个版本太新了 目前没有在百度上找到有用的资料,通过官方文档找到了这个

    > 管理类型映射的首选方法是在Npgsql.NpgsqlDataSourceBuilder上。

    但是在

     

     public static readonly SqlSugarScope Instance = new(
                // 读取 appsettings.json 中的 ConnectionConfigs 配置节点
                App.GetConfig<List<ConnectionConfig>>("ConnectionConfigs")
                , db =>
                {
                    using (db.Ado.OpenAlways())
                    {
                       (db.Ado.Connection as NpgsqlConnection)
                        .UseNetTopologySuite(
                           new DotSpatialAffineCoordinateSequenceFactory(ordinates: Ordinates.XY),
                           handleOrdinates: Ordinates.XY); //配置相关代码
                    }
                    // 这里配置全局事件,比如拦截执行 SQL
                });

    我们的这个配置里面需要手动创建连接,建立完成后Sugar的dbcontext怎么才能正常使用


    @fate sta

    0 回复
  • 仁者 仁者 VIP0
    2022/11/28

    @fate sta

    按照官方的说法这样处理可以搞定

    var dataSourceBuilder = new NpgsqlDataSourceBuilder(...);
                    dataSourceBuilder.UseNetTopologySuite(
                           new DotSpatialAffineCoordinateSequenceFactory(ordinates: Ordinates.XY),
                           handleOrdinates: Ordinates.XY);

    但是我不清楚在咱们这个框架里面应该怎么使用


    0 回复
  • fate sta fate sta VIP0
    2022/11/28

     (db.Ado.Connection as NpgsqlConnection)


    .UseNetTopologySuite(


                           new DotSpatialAffineCoordinateSequenceFactory(ordinates: Ordinates.XY),

                           handleOrdinates: Ordinates.XY);


    0 回复
  • fate sta fate sta VIP0
    2022/11/28

    一样用法

    0 回复
  • fate sta fate sta VIP0
    2022/11/28


     public static readonly SqlSugarScope Instance = new(
                // 读取 appsettings.json 中的 ConnectionConfigs 配置节点
                App.GetConfig<List<ConnectionConfig>>("ConnectionConfigs")
                , db =>
                {
                  
                });
                
                
         using (xx.Instance.Ado.OpenAlways())
                    {
                       (xx.Instance.Ado.Connection as NpgsqlConnection)
                        .UseNetTopologySuite(
                           new DotSpatialAffineCoordinateSequenceFactory(ordinates: Ordinates.XY),
                           handleOrdinates: Ordinates.XY);  
                           
                           
                           //你要写的代码
                    }


    0 回复
  • 仁者 仁者 VIP0
    2022/11/29

    image.png

    如上这样子写编译直接不通过(我是用的是furion.sugar脚手架4.8+),我修改为如下的样式结果生成的SQL语句好像有问题,没法直接写入Geometry类型的字段,

     public static class SqlsugarSetup
        {
            public static void AddSqlsugarSetup(this IServiceCollection services, IConfiguration configuration, string dbName = "ConnectionString")
            {
                try
                {
                    var configConnection = new ConnectionConfig()
                    {
                        DbType = SqlSugar.DbType.PostgreSQL,
                        ConnectionString = configuration["ConnectionString"],
                        IsAutoCloseConnection = false,
                    };
    
                    SqlSugarScope sqlSugar = new(config: configConnection,
                        configAction: db => initDataBase(db));
                    sqlSugar.DbMaintenance.CreateDatabase();
                    var path= Environment.ProcessPath;
                    path= path.Replace("Web.Entry.exe", "Core.dll");
                    Type[] types1 = Assembly.LoadFrom(path).GetExportedTypes();
                    Type[] types = Assembly.LoadFrom(path)
                        .GetTypes().Where(it => it.FullName.Contains("DBEntities")).ToArray();
                    sqlSugar.CodeFirst.SetStringDefaultLength(200).InitTables(types); 
    
                    NpgsqlConnection nc = (sqlSugar.Ado.Connection as NpgsqlConnection);
                    var dataSourceBuilder = new NpgsqlDataSourceBuilder(nc.ConnectionString);
                    dataSourceBuilder.UseNetTopologySuite(
                    new DotSpatialAffineCoordinateSequenceFactory(ordinates: Ordinates.XY),
                           handleOrdinates: Ordinates.XY);
                    services.AddSingleton<ISqlSugarClient>(sqlSugar);
                }
                catch (Exception ex)
                {
    
                    throw Oops.Oh($"{ex.Message}\n{ex.StackTrace}"); 
                }
            }
            public static void initDataBase(SqlSugarClient db)
            {
                db.Aop.OnLogExecuting = (sql, pars) =>
                {
    
                    Console.WriteLine(sql);//输出sql
                }; 
            }
        }


    0 回复
  • 仁者 仁者 VIP0
    2022/11/29

    sql及异常如下,按照提示应该是我的TypeMapper没有配置进去

    INSERT INTO "basemapinfo"
               ("id","createdtime","updatedtime","guid","mapdescribe","mapsize","mapname","mapcentor","mapbox","maptype","mapcolor","isload","mapshowindex")
         VALUES
               (@Id,@CreatedTime,@UpdatedTime,@Guid,@MapDescribe,@MapSize,@MapName,@MapCentor,@MapBox,@MapType,@MapColor,@IsLoad,@MapShowIndex) ;
    fail: 2022-11-29 15:26:19.8515886 +08:00 星期二 L GeoManageServiceSugar.Services.BaseMapInfoService[0] #8
          The CLR type NetTopologySuite.Geometries.Point isn't natively supported by Npgsql or your PostgreSQL. To use it with a PostgreSQL composite you need to specify DataTypeName or to map it, please refer to the documentation.
       at Npgsql.TypeMapping.TypeMapper.ResolveByClrType(Type type)


    0 回复
  • 仁者 仁者 VIP0
    2022/11/29

    @fate sta

    麻烦您了  实在是没办法了,跪求作答  感谢感谢

    0 回复