English 中文(简体)
2. 静态的NHibernate, Un Automapping etherion, Incorrect syntax near the keyworduser
原标题:Fluent NHibernate without Automapping SQL Exception, Incorrect syntax near the keyword User

Just starting in NHibernate and to my eye Everything seems correct but obviously is not. When I ren unit tests shown below I receive back that there is a syntax error near the keyword "User" here is my user class:

namespace Users
    {
        public class User
        {
            public virtual string firstName { get; set; }
            public virtual string lastName { get; set; }
            public virtual int Id { get; set; }
        }
    }

和用户分布图(Also在栏目上没有方括号,结果相同:

namespace Users
{
    class UserMap: ClassMap<User>
    {
        UserMap()
        {
            Table("User");
            Id(x => x.Id).GeneratedBy.Native().Column("[Id]").Not.Nullable();
            Map(x => x.firstName).Not.Nullable().Column("[firstName]");
            Map(x => x.lastName).Not.Nullable().Column("[lastName]");
        }
    }
}

The Config file listed Framework.cs

namespace Users
{
    public class Framework
    {
        private const string ConnectionStr = "Data Source=ERALCH-ESTEPHEN;Initial     
                                Catalog=NHDev;Integrated Security=True;Pooling=False";
        public static ISessionFactory CreateFactory()
        {
            return Fluently.Configure()
                .Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration
                .MsSql2008
                .ConnectionString(ConnectionStr))
                .Mappings(x=>x.FluentMappings.AddFromAssemblyOf<User>())
                .BuildSessionFactory();
        }
    }
}

数据存取-简单检索了Id的用户

namespace Users
{
    public class Accesslayer
    {
        public static IList<User> GetUserById(int Id)
        {
            ISessionFactory provider = Framework.CreateFactory();
            using (ISession session = provider.OpenSession())
            {
                return session.CreateSQLQuery(String
                    .Format("SELECT * FROM User WHERE Id={0}", Id)).List<User>();
            }

        }
    }
}

最后,单元测试层

namespace Users
{
    [TestFixture]
    class AccessLayerTest
    {
        [Test]
        public void CanGetUserById()
        {
            Assert.AreEqual(1, Accesslayer.GetUserById(1).Count());
        }
    }
}

数据库是MSsql,有一个表格“User”与用户特性相匹配。 感谢任何帮助。

问题回答

您应能够在您的组合中这样做:

var factory = Fluently.Configure()
    // ...
    .ExposeConfiguration(c => SchemaMetadataUpdater.QuoteTableAndColumns(c))
    .BuildSessionFactory();

谁应该自动避开你的桌子和栏目。

Did you try putting backticks around your User table name ?

namespace Users
{
    class UserMap: ClassMap<User>
    {
        UserMap()
        {
            Table("`User`");
            Id(x => x.Id).GeneratedBy.Native().Column("`Id`").Not.Nullable();
            Map(x => x.firstName).Not.Nullable().Column("`firstName`");
            Map(x => x.lastName).Not.Nullable().Column("`lastName`");
        }
    }
}

详情见:。 NHibernate - Force escaping on Table name

此外,你应使用NHibernate querying facilities,而不是使用Q:

namespace Users
{
    public class Accesslayer
    {
        public static IList<User> GetUserById(int Id)
        {
            ISessionFactory provider = Framework.CreateFactory();
            using (ISession session = provider.OpenSession())
            {
                return session.Query<User>().Where(x => x.Id == Id ).List<User>();
            }
        }
    }
}

查阅:

在绘制地图时,你应需要[签字]。 Heck,如果他们的名字相同,他们两人的工作相同:

public class UserMap: ClassMap<User>
{
    UserMap()
    {
        Table("User");
        Id(x => x.Id).GeneratedBy.Native().Not.Nullable();
        Map(x => x.firstName).Not.Nullable();
        Map(x => x.lastName).Not.Nullable();
    }
}

public class UserMap: ClassMap<User>
{
    UserMap()
    {
        Table("User");
        Id(x => x.Id, "Id").GeneratedBy.Native().Not.Nullable();
        Map(x => x.firstName, "firstName").Not.Nullable();
        Map(x => x.lastName, "lastName").Not.Nullable();
    }
}




相关问题
nHibernate one-to-many inserts but doesnt update

Instead of getting into code, I have a simple question. Default behavior for a simple one-to-many is that it inserts the child record then updates the foreign key column with the parent key. Has ...

How Do I copy an existing nhibernate object as a new object?

I a persisted NHibernate object that I would like to repersist as a new entity. How do I get NHibernate to save this object as if it was a new? I am thinking I might create a session interceptor to ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

NHibernate Search in a List using ICriteria

I have my class X : public class ClassX { public virtual IList<ClassY> ListY { get; set; } ... } My ClassX mapping (using Fluent) ... HasMany<ClassX>(x => x.ListY ) ....

热门标签