简体   繁体   English

NHibernate mysql更新错误,GenericADOException,服务器错误代码1064

[英]NHibernate mysql error on update, GenericADOException, Server Error Code 1064

I get an exception when updating via mysql, this same exact code works for microsoft sql. 通过mysql更新时出现异常,此完全相同的代码适用于Microsoft sql。

mysql server version 5.6.28 mysql服务器版本5.6.28

string connectionString = "Server=xxxx.is;Database=thebase_beta;Uid=userman1;Pwd=notherealpassword;sslmode=none"; 字符串连接字符串=“ Server = xxxx.is;数据库= thebase_beta; Uid = userman1; Pwd = notherealpassword; sslmode = none”; The code: 编码:

var cfg = new NHibernate.Cfg.Configuration();
cfg.DataBaseIntegration(x =>
{
    x.ConnectionString = connectionString;
    x.Driver<NHibernate.Driver.MySqlDataDriver>();
    x.Dialect<NHibernate.Dialect.MySQLDialect>();
});
cfg.AddAssembly(cfg.GetType().Assembly);

var mapper = new ModelMapper();
mapper.AddMappings(Assembly.GetExecutingAssembly().GetExportedTypes());
HbmMapping domainMapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
cfg.AddMapping(domainMapping);
cfg.SetProperty(NHibernate.Cfg.Environment.ShowSql, "true");
var sefact = cfg.BuildSessionFactory();     
using (var session = sefact.OpenSession())  
    using (var tx = session.BeginTransaction())
    {           
        var t = session.Query<oc_setting>()
           .Where(c => c.store_id> 0).First();//This works, the first record is fetched.               
        t.key = "fk";
        session.Save(t); 
        tx.Commit();//Error is thrown here.
    }

Exception details: 异常详细信息:

Message:
could not update: [UserQuery+oc_setting#18026][SQL: UPDATE oc_setting SET store_id = ?, code = ?, key = ?, value = ?, serialized = ? WHERE setting_id = ?] 
SqlString
UPDATE oc_setting SET store_id = ?, code = ?, key = ?, value = ?, serialized = ? WHERE setting_id = ? 

Innerexception (MySqlException):
Message:
    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 'key = 'fk', value = '1', serialized = 0 WHERE setting_id = 18026' at line 1

Server Error Code 1064 
    actual-sql-query UPDATE oc_setting SET store_id = ?p0, code = ?p1, key = ?p2, value = ?p3, serialized = ?p4 WHERE setting_id = ?p5 

This is the NHibernate logged sql output: 这是NHibernate记录的sql输出:

NHibernate: 
UPDATE oc_setting SET store_id = ?p0, code = ?p1, key = ?p2, value = ?p3, serialized = ?p4 WHERE setting_id = ?p5;?p0 = 1 [Type: Int32 (0:0:0)], ?p1 = 'custom_email_templates' [Type: String (22:0:0)], ?p2 = 'fk' [Type: String (2:0:0)], ?p3 = '1' [Type: String (1:0:0)], ?p4 = 0 [Type: Int32 (0:0:0)], ?p5 = 18026 [Type: Int32 (0:0:0)]

If I execute the command replacing the parameters in sql console it works. 如果我执行命令替换sql控制台中的参数,则它可以工作。

Mapping code: 映射代码:

public partial class OcSettingMap : ClassMapping<oc_setting>
{
    public OcSettingMap()
    {   
        Table("oc_setting");
        Lazy(false);
        Id(x => x.setting_id, map => { map.Column("setting_id"); map.Generator(Generators.Assigned); });
        Property(x => x.store_id, map => { map.Column("store_id"); map.NotNullable(true); });
        Property(x => x.code, map => { map.Column("code"); map.NotNullable(true); });
        Property(x => x.key, map => { map.Column("key"); map.NotNullable(true); });
        Property(x => x.value, map => { map.Column("value"); map.NotNullable(true); });
        Property(x => x.serialized, map => { map.Column("serialized"); map.NotNullable(true); });
    }
}
public partial class oc_setting
{
    public int setting_id { get; set; }
    [Required]
    public int store_id { get; set; }
    [Required]
    public string code { get; set; }
    [Required]
    public string key { get; set; }
    [Required]
    public string value { get; set; }
    [Required]
    public int serialized { get; set; }
}

I also an error when updating. 更新时我也出错。

Not sure if this matters, but these are my "usings" 不确定这是否重要,但这是我的“用法”

NHibernate.Cfg,NHibernate.Mapping.ByCode,NHibernate.Mapping.ByCode.Conformist,Sytem.Data.Entity.ModelConfiguration,System.Net,System.Threading.Tasks,FluentNHiber,ate.Mapping,NHibernate.Cfg.MappingSchema,System.ComponentModel.DataAnnotations,N,ibernate,NHibernate.Driver NHibernate.AdoNet NHibernate.Cfg,NHibernate.Mapping.ByCode,NHibernate.Mapping.ByCode.Conformist,Sytem.Data.Entity.ModelConfiguration,System.Net,System.Threading.Tasks,FluentNHiber,ate.Mapping,NHibernate.Cfg.MappingSchema,System。 ComponentModel.DataAnnotations,N,ibernate,NHibernate.Driver NHibernate.AdoNet

Adding SchemaMetadataUpdater.QuoteTableAndColumns(cfg); 添加SchemaMetadataUpdater.QuoteTableAndColumns(cfg);

Here: 这里:

cfg.SetProperty(NHibernate.Cfg.Environment.ShowSql, "true");
var sefact = cfg.BuildSessionFactory();     
SchemaMetadataUpdater.QuoteTableAndColumns(cfg); //here
using (var session = sefact.OpenSession())
...snip

Solved it. 解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM