无法使用 Castle ActiveRecord 创建架构

发布于 2024-09-18 12:31:57 字数 893 浏览 4 评论 0原文

我正在尝试开始使用 Castle ActiveRecord (遵循 "入门与 ActiveRecord” 截屏)。

我创建了两个 ActiveRecord 类,其中一个名为 User。我将相关字符串添加到 app.config 文件中,并使用以下命令进行初始化:

var config = ActiveRecordSectionHandler.Instance;
ActiveRecordStarter.Initialize(typeof(User).Assembly, config);

ActiveRecordStarter.CreateSchema();

不幸的是,当我运行此命令时,出现以下异常:

未处理的异常: Castle.ActiveRecord.Framework.ActiveRecordException: 无法创建架构 ---> NHibernate.HibernateException: 关键字附近的语法不正确 '用户'。 ---> System.Data.SqlClient.SqlException: 关键字附近的语法不正确 “用户”

我将一个小复制品上传到 http://bitbucket.org/hmemcpy/general,可以有人请告诉我我做错了什么吗?

谢谢!

I'm trying to get started with Castle ActiveRecord (following the "Getting started with ActiveRecord" screencast).

I created two ActiveRecord classes, one of them named User. I added the relevant strings to the app.config file, and I initialize using:

var config = ActiveRecordSectionHandler.Instance;
ActiveRecordStarter.Initialize(typeof(User).Assembly, config);

ActiveRecordStarter.CreateSchema();

Unfortunately, when I run this, I get the following exception:

Unhandled Exception:
Castle.ActiveRecord.Framework.ActiveRecordException:
Could not create the schema --->
NHibernate.HibernateException:
Incorrect syntax near the keyword
'User'. --->
System.Data.SqlClient.SqlException:
Incorrect syntax near the keyword
'User'

I uploaded a small reproduction to http://bitbucket.org/hmemcpy/general, could someone please tell me what have I done wrong?

Thanks!

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

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

发布评论

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

评论(1

梦醒灬来后我 2024-09-25 12:31:57

'User'是SQL Server中的保留字,执行的SQL命令是:

create table User (Id INT IDENTITY NOT NULL,
                   UserName NVARCHAR(255) null unique,
                   primary key (Id))

在SQL Server中将表命名为User是非法的,但将其命名为[User]< /em>。

create table [User] (Id INT IDENTITY NOT NULL,
                   UserName NVARCHAR(255) null unique,
                   primary key (Id))

作为解决方案,您可以为由保留字命名的表和列定义不同的名称:

[ActiveRecord("[User]")]
public class User : ActiveRecordLinqBase<User>
{
    [PrimaryKey]
    public int Id { get; set; }

    [Property(Unique = true)]
    public string UserName { get; set; }

    [HasMany]
    public IList<Activity> Activities { get; set; }
}

当在表名称周围使用 [ ] 时,允许保留名称。这同样适用于关系列:

[ActiveRecord]
public class Activity : ActiveRecordLinqBase<Activity>
{
    [PrimaryKey]
    public int Id { get; set; }

    [BelongsTo("[User]")]
    public User User { get; set; }
}

当然,任何其他名称,如 [ActiveRecord("SomeUser")] 都可以。

'User' is a reserved word in SQL server, the SQL command being executed is:

create table User (Id INT IDENTITY NOT NULL,
                   UserName NVARCHAR(255) null unique,
                   primary key (Id))

It's illegal to name a table User in SQL server, but it is legal to name it [User].

create table [User] (Id INT IDENTITY NOT NULL,
                   UserName NVARCHAR(255) null unique,
                   primary key (Id))

As a solution you can define different names for tables and columns named by reserved words:

[ActiveRecord("[User]")]
public class User : ActiveRecordLinqBase<User>
{
    [PrimaryKey]
    public int Id { get; set; }

    [Property(Unique = true)]
    public string UserName { get; set; }

    [HasMany]
    public IList<Activity> Activities { get; set; }
}

When using a [ ] around the table name, reserved names are allowed. The same is relevant for relation columns:

[ActiveRecord]
public class Activity : ActiveRecordLinqBase<Activity>
{
    [PrimaryKey]
    public int Id { get; set; }

    [BelongsTo("[User]")]
    public User User { get; set; }
}

Of course, any other name like [ActiveRecord("SomeUser")] will work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文