ActiveRecord、NHibernate 和 PostgreSQL

发布于 2024-11-16 09:41:40 字数 2028 浏览 5 评论 0原文

我有点被困在这里,无法让它发挥作用。这看起来很简单,但我一定做错了什么。

我有一个简单的类来测试 NHibernate Active Record 和 PostgreSQL,看一下

[ActiveRecord]
public class Accident:ActiveRecordBase<Accident>
{
    [PrimaryKey(PrimaryKeyType.Sequence)]
    public int Id { get; set; }

    [Property]
    public string Address { get; set; }

    [Property]
    public int AddressNumber { get; set; }

    [Property]
    public testAccidentType AccidentType { get; set; }
}

public enum testAccidentType
{
    FRONT,
    BACK,
    SIDE
}

我正在尝试从我的类创建模式,如下所示:

public class Startup
{
    public static void StartActiveRecord()
    {
        XmlConfigurationSource source = new XmlConfigurationSource(@"c:\users\h\documents\visual studio 2010\Projects\TestNHibernate\TestNHibernate\Model\config.xml");
        ActiveRecordStarter.Initialize(source, GetActiveRecordTypes());

        ActiveRecordStarter.CreateSchema();
    }

    public static Type[] GetActiveRecordTypes()
    {
        List<Type> types = new List<Type>()
        {
            typeof(Accident)
        };
        return types.ToArray();
    }
}

ActiveRecord 能够初始化此类,但它始终卡在 CreateSchema 方法中。这是配置文件。专家有什么建议吗?

<?xml version="1.0" encoding="utf-8" ?>

<activerecord isWeb="false">
  <config>
    <add key="connection.driver_class" value="NHibernate.Driver.NpgsqlDriver" />
    <add key="connection.connection_string" value="Server=localhost;initial catalog=nhiber;User ID=postgres;Password=***;" />
    <add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
    <add key="dialect" value="NHibernate.Dialect.PostgreSQLDialect" />
  </config>
</activerecord>

编辑:我想出了这个。问题是文档是错误的。 PostgreSQL 不使用关键字“初始目录”,而是使用“数据库”,如下所示:

    <add key="connection.connection_string" value="Server=localhost;database=nhiber;User ID=postgres;Password=***;" />

谢谢!

I'm kinda stuck here and can't get this to work. It seems so simple, but I must be doing something incredibly wrong.

I have a simple class to test NHibernate Active Record and PostgreSQL, take a look

[ActiveRecord]
public class Accident:ActiveRecordBase<Accident>
{
    [PrimaryKey(PrimaryKeyType.Sequence)]
    public int Id { get; set; }

    [Property]
    public string Address { get; set; }

    [Property]
    public int AddressNumber { get; set; }

    [Property]
    public testAccidentType AccidentType { get; set; }
}

public enum testAccidentType
{
    FRONT,
    BACK,
    SIDE
}

And I'm trying to create the schema from my class, like this:

public class Startup
{
    public static void StartActiveRecord()
    {
        XmlConfigurationSource source = new XmlConfigurationSource(@"c:\users\h\documents\visual studio 2010\Projects\TestNHibernate\TestNHibernate\Model\config.xml");
        ActiveRecordStarter.Initialize(source, GetActiveRecordTypes());

        ActiveRecordStarter.CreateSchema();
    }

    public static Type[] GetActiveRecordTypes()
    {
        List<Type> types = new List<Type>()
        {
            typeof(Accident)
        };
        return types.ToArray();
    }
}

ActiveRecord is able to initialize this class, but it's always stuck in CreateSchema method. Here is the config file. Any tips from the experts?

<?xml version="1.0" encoding="utf-8" ?>

<activerecord isWeb="false">
  <config>
    <add key="connection.driver_class" value="NHibernate.Driver.NpgsqlDriver" />
    <add key="connection.connection_string" value="Server=localhost;initial catalog=nhiber;User ID=postgres;Password=***;" />
    <add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
    <add key="dialect" value="NHibernate.Dialect.PostgreSQLDialect" />
  </config>
</activerecord>

EDIT: I figured this one out. the problem is that the documentation is wrong. PostgreSQL does not use the keyword "initial catalog" but it uses "database", like this:

    <add key="connection.connection_string" value="Server=localhost;database=nhiber;User ID=postgres;Password=***;" />

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我太投入 2024-11-23 09:41:40

您需要更改连接字符串:

<add key="connection.connection_string" value="Server=localhost;database=nhiber;User ID=postgres;Password=***;" />

You need to change the connection string:

<add key="connection.connection_string" value="Server=localhost;database=nhiber;User ID=postgres;Password=***;" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文