Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

Receiving NHibernate with MySQL Syntax error during table creation in C#

$
0
0

hi,

I am new to NHibernate, while doing simple crude operation with NHibernate and MySQL database, I am getting error as:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Character (ID INTEGER NOT NULL AUTO_INCREMENT, name varchar(45), primary key (ID' at line 1

error screenshot

My code is as follows:

NHibernate Helper Class:

namespace NHibernateMysql
{
    public class NHibernateHelper
    {
        private static ISessionFactory _sessionFactory;
        private static ISessionFactory SessionFactory
        {
            get
            {
                if (_sessionFactory == null)
                {
                    var configuration = new Configuration();
                    configuration.Configure();
                    configuration.AddAssembly(typeof(Character).Assembly);
                    _sessionFactory = configuration.BuildSessionFactory();
                }
                return _sessionFactory;
            }
        }
        public static ISession OpenSession()
        {
            return SessionFactory.OpenSession();
        }
    }
}

Repository Class:

namespace NHibernateMysql
{
    public class CharacterRepository
    {
        public void Add(Character newCharacter)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Save(newCharacter);
                    transaction.Commit();
                }
            }
        }

    }
}

Domain Class

namespace NHibernateMysql.Domain
{
    public class Character
    {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
    }
}

Character.hbm.xml

<?xml version="1.0" encoding="utf-8" ?><hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="NHibernateMysql"
                   namespace="NHibernateMysql.Domain"><class name="Character"><id name="Id" column="ID" type="int"><generator class="native"></generator></id><property name="Name"><column name="name" sql-type="varchar(45)" not-null="false"/></property></class></hibernate-mapping>

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8" ?><hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"><session-factory><property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property><property name="dialect">NHibernate.Dialect.MySQL5Dialect</property><property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property><property name="connection.connection_string">Server=192.168.200.35;Database=NHibernate;User ID=root;Password=1234;</property></session-factory></hibernate-configuration>

Program.cs

using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NHibernateMysql.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NHibernateMysql
{
    class Program
    {
        static void Main(string[] args)
        {
            LoadHibernateCfg();
            /*CRUD*/
            //CREATE!
            CharacterRepository repo = new CharacterRepository();
            var MikeAbyss = new Character { Name = "MikeAbyss" };
            repo.Add(MikeAbyss);


            Console.ReadKey();
        }
        public static void LoadHibernateCfg()
        {
            var cfg = new Configuration();
            cfg.Configure();
            cfg.AddAssembly(typeof(Character).Assembly);
            new SchemaExport(cfg).Execute(true, true, false);
        }
    }
}

please help!


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>