OrmLite:SQLiteExceptionSQL 逻辑错误或“)”附近缺少数据库:语法错误
OrmLite: SQLiteExceptionSQL logic error or missing database near ")": syntax error
您好,我正在尝试测试是否删除父对象,子对象也会使用 OrmLite 和内存数据库 Sqlite 自动删除
这是我的测试代码,但它向我抛出 System.Data.SQLite.SQLiteExceptionSQL 逻辑错误或“)”附近缺少数据库:db.Save() 行的语法错误。
可能出了什么问题?
[Fact]
public void DeleteById_AlsoDeleteChild_Test()
{
var _dbFactory = new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider);
using (var db = _dbFactory.OpenDbConnection())
{
// arrange
db.CreateTableIfNotExists<Foo>();
db.CreateTableIfNotExists<Bar>();
var foo = new Foo
{
Bar = new Bar
{
Name = "Hello"
}
};
db.Save(foo);
db.SaveReferences(foo, foo.Bar);
var saved = db.Select<Foo>();
// act
db.DeleteById<Foo>(saved.First().Id);
// assert
Assert.False(db.Exists<Bar>(c => c.FooId == saved.First().Id));
}
}
public class Foo : IHasIntId
{
[AutoIncrement]
public int Id { get; set; }
[Reference]
public Bar Bar { get; set; }
}
public class Bar : IHasIntId
{
[AutoIncrement]
public int Id { get; set; }
[ForeignKey(typeof(Foo), OnDelete = "CASCADE")]
public int FooId { get; set; }
public string Name { get; set; }
}
System.Data.SQLite.SQLiteExceptionSQL逻辑错误或缺少数据库
near ")": 语法错误
在 System.Data.SQLite.SQLite3.Prepare(SQLiteConnection cnn,String strSql,SQLiteStatement previous,UInt32 timeoutMS,ref String strRemain)
在 System.Data.SQLite.SQLiteCommand.BuildNextCommand()
在 System.Data.SQLite.SQLiteDataReader.NextResult()
在 System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior 行为)
在 System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior 行为)
在 System.Data.SQLite.SQLiteCommand.ExecuteScalar(CommandBehavior 行为)
在 ServiceStack.OrmLite.OrmLiteReadCommandExtensions.LongScalar(IDbCommand dbCmd)
在 ServiceStack.OrmLite.OrmLiteWriteCommandExtensions.Save(IDbCommand dbCmd, T obj)
在 ServiceStack.OrmLite.OrmLiteWriteApi.<>c__DisplayClass39' 1. b__38(IDbCommand dbCmd)
在 ServiceStack.OrmLite.OrmLiteExecFilter.Exec(IDbConnection dbConn, Func`2 过滤器)
在 ClassLibrary2.Class1.DeleteById_AlsoDeleteChild_Test() 中 Class1.cs:第 35 行
这个问题是因为您 Foo
没有任何要插入的列,因为 Id
是一个自动递增的主键,而 Bar
是一个 [Reference]
属性 所以没有列被保存所以 INSERT SQL 最终看起来像:
INSERT INTO "Foo" () VALUES ();
如果您 Foo
有一个列,这将有效,例如:
public class Foo : IHasIntId
{
[AutoIncrement]
public int Id { get; set; }
[Reference]
public Bar Bar { get; set; }
public string Name { get; set; }
}
注意 SQLite 默认不启用外键支持。每次使用 pragma 连接到数据库时都需要手动启用它:
PRAGMA foreign_keys = ON
所以一个工作示例如下所示:
using (var db = OpenDbConnection())
{
db.DropAndCreateTable<Foo>();
db.DropAndCreateTable<Bar>();
db.ExecuteNonQuery("PRAGMA foreign_keys = ON");
var foo = new Foo
{
Bar = new Bar
{
Name = "Hello"
}
};
db.Save(foo);
db.SaveReferences(foo, foo.Bar);
var saved = db.Select<Foo>();
db.DeleteById<Foo>(saved.First().Id);
Assert.False(db.Exists<Bar>(c => c.FooId == saved.First().Id));
}
您好,我正在尝试测试是否删除父对象,子对象也会使用 OrmLite 和内存数据库 Sqlite 自动删除
这是我的测试代码,但它向我抛出 System.Data.SQLite.SQLiteExceptionSQL 逻辑错误或“)”附近缺少数据库:db.Save() 行的语法错误。
可能出了什么问题?
[Fact]
public void DeleteById_AlsoDeleteChild_Test()
{
var _dbFactory = new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider);
using (var db = _dbFactory.OpenDbConnection())
{
// arrange
db.CreateTableIfNotExists<Foo>();
db.CreateTableIfNotExists<Bar>();
var foo = new Foo
{
Bar = new Bar
{
Name = "Hello"
}
};
db.Save(foo);
db.SaveReferences(foo, foo.Bar);
var saved = db.Select<Foo>();
// act
db.DeleteById<Foo>(saved.First().Id);
// assert
Assert.False(db.Exists<Bar>(c => c.FooId == saved.First().Id));
}
}
public class Foo : IHasIntId
{
[AutoIncrement]
public int Id { get; set; }
[Reference]
public Bar Bar { get; set; }
}
public class Bar : IHasIntId
{
[AutoIncrement]
public int Id { get; set; }
[ForeignKey(typeof(Foo), OnDelete = "CASCADE")]
public int FooId { get; set; }
public string Name { get; set; }
}
System.Data.SQLite.SQLiteExceptionSQL逻辑错误或缺少数据库
near ")": 语法错误 在 System.Data.SQLite.SQLite3.Prepare(SQLiteConnection cnn,String strSql,SQLiteStatement previous,UInt32 timeoutMS,ref String strRemain) 在 System.Data.SQLite.SQLiteCommand.BuildNextCommand() 在 System.Data.SQLite.SQLiteDataReader.NextResult() 在 System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior 行为) 在 System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior 行为) 在 System.Data.SQLite.SQLiteCommand.ExecuteScalar(CommandBehavior 行为) 在 ServiceStack.OrmLite.OrmLiteReadCommandExtensions.LongScalar(IDbCommand dbCmd) 在 ServiceStack.OrmLite.OrmLiteWriteCommandExtensions.Save(IDbCommand dbCmd, T obj) 在 ServiceStack.OrmLite.OrmLiteWriteApi.<>c__DisplayClass39' 1. b__38(IDbCommand dbCmd) 在 ServiceStack.OrmLite.OrmLiteExecFilter.Exec(IDbConnection dbConn, Func`2 过滤器) 在 ClassLibrary2.Class1.DeleteById_AlsoDeleteChild_Test() 中 Class1.cs:第 35 行
这个问题是因为您 Foo
没有任何要插入的列,因为 Id
是一个自动递增的主键,而 Bar
是一个 [Reference]
属性 所以没有列被保存所以 INSERT SQL 最终看起来像:
INSERT INTO "Foo" () VALUES ();
如果您 Foo
有一个列,这将有效,例如:
public class Foo : IHasIntId
{
[AutoIncrement]
public int Id { get; set; }
[Reference]
public Bar Bar { get; set; }
public string Name { get; set; }
}
注意 SQLite 默认不启用外键支持。每次使用 pragma 连接到数据库时都需要手动启用它:
PRAGMA foreign_keys = ON
所以一个工作示例如下所示:
using (var db = OpenDbConnection())
{
db.DropAndCreateTable<Foo>();
db.DropAndCreateTable<Bar>();
db.ExecuteNonQuery("PRAGMA foreign_keys = ON");
var foo = new Foo
{
Bar = new Bar
{
Name = "Hello"
}
};
db.Save(foo);
db.SaveReferences(foo, foo.Bar);
var saved = db.Select<Foo>();
db.DeleteById<Foo>(saved.First().Id);
Assert.False(db.Exists<Bar>(c => c.FooId == saved.First().Id));
}