Free NHibernate without automatic exception SQL Exception, Invalid syntax next to the keyword "User"

Just starting with NHibernate and with my eye. Everything seems right, but obviously not. When I show the unit tests shown below, I get back that there is a syntax error next to 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; }
        }
    }

and user mappings (also performed without square brackets around column names with the same results:

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]");
        }
    }
}

Configuration file named 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();
        }
    }
}

Data Access Level - just retrieves the user using 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>();
            }

        }
    }
}

and finally the unit test layer

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

A database is an MSsql with one User table with columns corresponding to user properties. Any help would be appreciated thanks

+3
4

:

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

.

+4

?

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 -

, NHibernate SQL:

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>();
            }
        }
    }
}

: http://www.d80.co.uk/post/2011/02/20/Linq-to-NHibernate-Tutorial.aspx

0

[]. , :

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();
    }
}
0

Avoid naming tables or columns with Reserved Keywords . Hibernate generates an SQL Statement that will not be accepted by MS SQL. Had the same problem with NHibernate + Fluent + Automapper and decided to rename it "user" -column to "username". I don’t know how to cope with other databases.

Further comments about this are here .

0
source

All Articles