You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改PostgreSqlConnectionProvider搜索路径访问PostgreSQL非public schema表

解决DevExpress XPO访问PostgreSQL非public Schema表的问题

1. 修正Persistent特性的写法

你之前尝试的["myschema.Custumer_View"]写法错误,正确方式是直接将schema和表名作为字符串传入Persistent特性:

[Persistent("myschema.Custumer_View")]
public class CustomerModel
{
    [Key, Persistent("Number")]
    public string CustomerNumber { get; set; }
    //...
}

2. 配置连接字符串的Search Path

在PostgreSQL连接字符串中添加Search Path参数,让数据库自动在指定schema中查找对象,无需每个实体单独指定:

Server=你的服务器地址;Port=5432;Database=你的数据库名;User Id=用户名;Password=密码;Search Path=myschema,public;

3. 确保数据库用户具备权限

执行以下SQL语句,给访问数据库的用户授予目标schema的使用权限和表的查询权限:

GRANT USAGE ON SCHEMA myschema TO 你的用户名;
GRANT SELECT ON TABLE myschema.Custumer_View TO 你的用户名;

4. 调整ObjectsOwner的设置逻辑

如果要全局指定schema,需确保实体类没有单独设置Persistent特性的schema部分,同时确认ObjectsOwner在连接打开前设置:

public static IDataSource GetDataSource(IDbConnection dbConnection, Type typeOfModel)
{
    var store = XpoDefault.GetConnectionProvider(dbConnection, AutoCreateOption.None);
    HandlePostgres(store);

    return CreateServerCollectionSource(store, typeOfModel);
}

private static void HandlePostgres(IDataStore store)
{
    if (store is PostgreSqlConnectionProvider pgProvider)
    {
        pgProvider.ObjectsOwner = "myschema";
    }
}

注意:如果实体类已经通过Persistent指定了schema,全局的ObjectsOwner设置会被覆盖。

内容的提问来源于stack exchange,提问作者Daniel W.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 08:27:10