nhibernate mysql更新时出错,GenericadException,服务器错误代码1064

dy1byipe  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(359)

我在通过mysql更新时遇到了一个异常,同样的代码也适用于microsoftsql。
mysql服务器版本5.6.28
string connectionstring=“服务器=x.is;数据库=基本β;uid=userman1;pwd=无密码;SSL模式=无”;代码:

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.
    }

异常详细信息:

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

这是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)]

如果我在sql控制台中执行替换参数的命令,它就会工作。
Map代码:

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

更新时我还发现一个错误。
不知道这是否重要,但这些是我的“用途”
nhibernate.cfg,nhibernate.mapping.bycode,nhibernate.mapping.bycode.conformist,system.data.entity.modelconfiguration,system.net,system.threading.tasks,fluentnhiber,ate.mapping,nhibernate.cfg.mappingschema,system.componentmodel.dataannotations,n,ibernate,nhibernate.driver nhibernate.adonnet

ghg1uchk

ghg1uchk1#

添加schemametadataupdater.quotetableandcolumns(cfg);
在这里:

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

解决了。

相关问题