InsertAll, UpdateAll and DeleteAll 的参数要是IEnumerables
Each关键字 返回 IEnumerable 并且是延迟加载的
全局设置 当字符串为null时候,设置一个默认值
OrmLiteConfig.OnDbNullFilter = fieldDef = >
fieldDef.FieldType == typeof(string) ? "NULL" : null;
---
执行sql的时候过滤
public class MockStoredProcExecFilter : OrmLiteExecFilter
{
public override T Exec (IDbConnection dbConn, Func filter)
{
try
{
return base.Exec(dbConn, filter);
}
catch (Exception ex)
{
if (dbConn.GetLastSql() == "exec sp_name @firstName, @age")
return (T)(object)new Person { FirstName = "Mocked" };
throw;
}
}
}
OrmLiteConfig.ExecFilter = new MockStoredProcExecFilter();
using (var db = OpenDbConnection())
{
var person = db.SqlScalar ("exec sp_name @firstName, @age",
new { firstName = "aName", age = 1 });
person.FirstName.Print(); //Mocked
}
CaptureSqlFilter
跟踪 可以在调试窗口看到了
using (var captured = new CaptureSqlFilter())
using (var db = OpenDbConnection())
{
db.Where (new { Age = 27 });
captured.SqlStatements[0].PrintDump();
}
同上 重复执行
public class ReplayOrmLiteExecFilter : OrmLiteExecFilter
{
public int ReplayTimes { get; set; }
public override T Exec<T>(IDbConnection dbConn, Func<IDbCommand, T> filter)
{
var holdProvider = OrmLiteConfig.DialectProvider;
var dbCmd = CreateCommand(dbConn);
try
{
var ret = default(T);
for (var i = 0; i < ReplayTimes; i++)
{
ret = filter(dbCmd);
}
return ret;
}
finally
{
DisposeCommand(dbCmd);
OrmLiteConfig.DialectProvider = holdProvider;
}
}
}
OrmLiteConfig.ExecFilter = new ReplayOrmLiteExecFilter { ReplayTimes = 3 };
using (var db = OpenDbConnection())
{
db.DropAndCreateTable ();
db.Insert(new PocoTable { Name = "Multiplicity" });
var rowsInserted = db.Count<PocoTable>(q => q.Name == "Multiplicity"); //3
}
单元测试 模拟返回的数据
下面返回的数据都是假的,山寨的
using (new OrmLiteResultsFilter {
PrintSql = true,
SingleResult = new Person {
Id = 1, FirstName = "Mocked", LastName = "Person", Age = 100
},
})
{
db.Single (x => x.Age == 42).FirstName // Mocked
db.Single(db.From ().Where(x => x.Age == 42)).FirstName // Mocked
db.Single (new { Age = 42 }).FirstName // Mocked
db.Single ("Age = @age", new { age = 42 }).FirstName // Mocked
}
一看就懂 字符串过滤 还有这玩意居然
OrmLiteConfig.StringFilter = s => s.TrimEnd();
db.Insert(new Poco { Name = "Value with trailing " });
db.Select ().First().Name // "Value with trailing"
可插入 可拔出的 复杂类型序列化
据说ServiceStack's的json序列化效率最高
//ServiceStack's JSON and JSV Format
SqliteDialect.Provider.StringSerializer = new JsvStringSerializer();
PostgreSqlDialect.Provider.StringSerializer = new JsonStringSerializer();
//.NET's XML and JSON DataContract serializers
SqlServerDialect.Provider.StringSerializer = new DataContractSerializer();
MySqlDialect.Provider.StringSerializer = new JsonDataContractSerializer();
//.NET XmlSerializer
OracleDialect.Provider.StringSerializer = new XmlSerializableSerializer();
---
code first的时候会用到吧
public class PocoTable
{
public int Id { get; set; }
[CustomField("CHAR(20)")]
public string CharColumn { get; set; }
[CustomField("DECIMAL(18,4)")]
public decimal? DecimalColumn { get; set; }
}
db.CreateTable ();
结果:CREATE TABLE "PocoTable"
(
"Id" INTEGER PRIMARY KEY,
"CharColumn" CHAR(20) NULL,
"DecimalColumn" DECIMAL(18,4) NULL
);
可以这样调用存储过程的
List results = db.SqlList ("EXEC GetAnalyticsForWeek 1");
List results = db.SqlList (
"EXEC GetAnalyticsForWeek @weekNo", new { weekNo = 1 });
List results = db.SqlList ("EXEC GetTotalsForWeek 1");
List results = db.SqlList (
"EXEC GetTotalsForWeek @weekNo", new { weekNo = 1 });
int result = db.SqlScalar ("SELECT 10");
搞个带参数的存储过程
string spSql = @"DROP PROCEDURE IF EXISTS spSearchLetters;
CREATE PROCEDURE spSearchLetters (IN pLetter varchar(10), OUT pTotal int)
BEGIN
SELECT COUNT() FROM LetterFrequency WHERE Letter = pLetter INTO pTotal;
SELECT FROM LetterFrequency WHERE Letter = pLetter;
END";
db.ExecuteSql(spSql);
using (var cmd = db.SqlProc("spSearchLetters", new { pLetter = "C" }))
{
var pTotal = cmd.AddParam("pTotal", direction: ParameterDirection.Output);
var results = cmd.ConvertToList<LetterFrequency>();
var total = pTotal.Value;
}
换个姿势加参数
IDbDataParameter pTotal = null;
var results = db.SqlList ("spSearchLetters", cmd => {
cmd.CommandType = CommandType.StoredProcedure;
cmd.AddParam("pLetter", "C");
pTotal = cmd.AddParam("pTotal", direction: ParameterDirection.Output);
});
var total = pTotal.Value;
设置字段的默认值 g点在这里OrmLiteVariables
public class Poco
{
[Default(OrmLiteVariables.SystemUtc)] //= {SYSTEM_UTC}
public DateTime CreatedTimeUtc { get; set; }
}