在NHibernate 4中使用Guid作为主键时生成的Select语句不正确

时间:2016-02-25 14:12:34

标签: sql nhibernate

我在LinqPad中使用以下代码来演示问题:

void Main()
{
    Configuration cfg = new Configuration()
        .DataBaseIntegration(db =>
        {
            db.ConnectionString = @"Data Source=xxxxxxxx\SQLEXPRESS;Initial Catalog=Testarossa;Integrated Security=True";
            db.Dialect<MsSql2012Dialect>();
        });

    /* Add the mapping we defined: */
    var mapper = new ModelMapper();
    mapper.AddMappings(Assembly.GetExecutingAssembly().GetExportedTypes());

    HbmMapping mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();

    cfg.AddMapping(mapping);

    /* Create a session and execute a query: */
    using (ISessionFactory factory = cfg.BuildSessionFactory())
    using (ISession        session = factory.OpenSession())
    using (ITransaction    tx      = session.BeginTransaction())
    {
        session.Get<Person>(2).Dump();
        session.Get<User>(new Guid("2952A2BB-DAFE-4221-A102-798A2EB12626")).Dump();

        tx.Commit();
    }
}

public class PersonMap : ClassMapping<Person>
{
    public PersonMap()
    {
        this.Table("person");
        this.Id(p => p.Id);
        this.Property(p => p.FirstName);
        this.Property(p => p.LastName);
        this.Property(p => p.BirthDate);
    }
}

public class UserMap : ClassMapping<User>
{
    public UserMap()
    {
        this.Table("user");

        this.Id(p => p.Id, id =>
        {
            id.Type(NHibernateUtil.Guid);
        });

        this.Property(p => p.FirstName);
        this.Property(p => p.LastName);
        this.Property(p => p.Created);
    }
}

public class Person
{
    public virtual int      Id        { get; set; }
    public virtual string   FirstName { get; set; }
    public virtual string   LastName  { get; set; }
    public virtual DateTime BirthDate { get; set; }
}

public class User
{
    public virtual Guid     Id        { get; set; }
    public virtual string   FirstName { get; set; }
    public virtual string   LastName  { get; set; }
    public virtual DateTime Created   { get; set; }
}

Person的查询效果很好。查询User生成了以下SQL:

SELECT userquery_0_.Id as Id11_0_, userquery_0_.FirstName as FirstName11_0_,
     userquery_0_.LastName as LastName11_0_, 
     userquery_0_.Created as Created11_0_ 
   FROM user userquery_0_ WHERE userquery_0_.Id=?

出现以下错误消息:

Incorrect syntax near the keyword 'user'.

好吧,我理解SQL错误的原因,我不明白的是,为什么NHibernate生成这个不正确的SQL语句。

有人可以帮忙解释一下吗?我做错了吗?

为方便起见,这里有表ddl:

CREATE TABLE Testarossa.dbo.Person (
    Id        INT PRIMARY KEY NOT NULL,
    FirstName NVARCHAR        NOT NULL,
    LastName  NVARCHAR        NOT NULL,
    BirthDate DATETIME DEFAULT (getdate())
);

CREATE TABLE Testarossa.dbo.[User] (
    Id        UNIQUEIDENTIFIER PRIMARY KEY NOT NULL,
    FirstName NVARCHAR                     NOT NULL,
    LastName  NVARCHAR                     NOT NULL,
    Created   DATETIME DEFAULT (getdate())
);

1 个答案:

答案 0 :(得分:4)

单词/名称 user 是关键字(至少在SQL Server中)。所以,这样的表名是......

至少我们应该像这样逃避它

this.Table("[user]");

这种风格适用于SQL Server,NHibernate的一般转义语法是:

this.Table("`user`");