本文整理汇总了C#中MappingSchema.SetConvertExpression方法的典型用法代码示例。如果您正苦于以下问题:C# MappingSchema.SetConvertExpression方法的具体用法?C# MappingSchema.SetConvertExpression怎么用?C# MappingSchema.SetConvertExpression使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MappingSchema
的用法示例。
在下文中一共展示了MappingSchema.SetConvertExpression方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Test
public void Test(string context)
{
var firebirdQuote = FirebirdSqlBuilder.IdentifierQuoteMode;
var schema = new MappingSchema();
schema.SetDataType(typeof(MyInt), DataType.Int32);
schema.SetConvertExpression<MyInt, int> (x => x.Value);
schema.SetConvertExpression<int, MyInt> (x => new MyInt { Value = x });
schema.SetConvertExpression<Int64, MyInt> (x => new MyInt { Value = (int)x }); //SQLite
schema.SetConvertExpression<decimal, MyInt> (x => new MyInt { Value = (int)x }); //Oracle
schema.SetConvertExpression<MyInt, DataParameter>(x => new DataParameter { DataType = DataType.Int32, Value = x.Value });
schema.GetFluentMappingBuilder()
.Entity<Entity>()
.HasTableName("Issue464")
.HasColumn(x => x.Id)
.HasColumn(x => x.Value);
using (var db = new DataConnection(context).AddMappingSchema(schema))
{
try
{
FirebirdSqlBuilder.IdentifierQuoteMode = FirebirdIdentifierQuoteMode.Auto;
var temptable = db.CreateTable<Entity>();
var data = new[]
{
new Entity {Id = 1, Value = new MyInt {Value = 1}},
new Entity {Id = 2, Value = new MyInt {Value = 2}},
new Entity {Id = 3, Value = new MyInt {Value = 3}}
};
temptable.BulkCopy(data);
AreEqual(data, temptable.ToList());
}
finally
{
db.DropTable<Entity>();
FirebirdSqlBuilder.IdentifierQuoteMode = firebirdQuote;
}
}
}
示例2: DefaultValue3
public void DefaultValue3()
{
var ms1 = new MappingSchema();
var ms2 = new MappingSchema(ms1);
ms1.SetConvertExpression<int?,int>(i => i.Value * 2);
ms2.SetDefaultValue(typeof(int), -1);
var c1 = ms1.GetConverter<int?,int>();
var c2 = ms2.GetConverter<int?,int>();
Assert.AreEqual( 4, c1(2));
Assert.AreEqual( 0, c1(null));
Assert.AreEqual( 4, c2(2));
Assert.AreEqual(-1, c2(null));
}
示例3: TestDataParameterMapping3
public void TestDataParameterMapping3()
{
var ms = new MappingSchema();
ms.SetConvertExpression<TwoValues,DataParameter>(tv =>
new DataParameter
{
Value = tv == null ? (long?)null : (long)tv.Value1 << 32 | tv.Value2,
DataType = DataType.Int64
},
false);
using (var conn = new DataConnection().AddMappingSchema(ms))
{
var n = conn.Execute<long?>("SELECT @p", new { p = (TwoValues)null });
Assert.AreEqual(null, n);
}
}
示例4: TestDataParameterMapping1
public void TestDataParameterMapping1()
{
var ms = new MappingSchema();
ms.SetConvertExpression<TwoValues,DataParameter>(tv => new DataParameter { Value = (long)tv.Value1 << 32 | tv.Value2 });
using (var conn = new DataConnection().AddMappingSchema(ms))
{
var n = conn.Execute<long>("SELECT @p", new { p = new TwoValues { Value1 = 1, Value2 = 2 }});
Assert.AreEqual(1L << 32 | 2, n);
}
}
示例5: BaseSchema2
public void BaseSchema2()
{
var ms1 = new MappingSchema();
var ms2 = new MappingSchema(ms1);
Convert<DateTime,string>.Lambda = d => d.ToString(DateTimeFormatInfo.InvariantInfo);
ms1.SetConverter<DateTime,string>(d => d.ToString(new CultureInfo("en-US", false).DateTimeFormat));
ms2.SetConverter<DateTime,string>(d => d.ToString(new CultureInfo("ru-RU", false).DateTimeFormat));
{
var c0 = Convert<DateTime,string>.Lambda;
var c1 = ms1.GetConverter<DateTime,string>();
var c2 = ms2.GetConverter<DateTime,string>();
Assert.AreEqual("01/20/2012 16:30:40", c0(new DateTime(2012, 1, 20, 16, 30, 40, 50, DateTimeKind.Utc)));
Assert.AreEqual("1/20/2012 4:30:40 PM", c1(new DateTime(2012, 1, 20, 16, 30, 40, 50, DateTimeKind.Utc)));
Assert.AreEqual("20.01.2012 16:30:40", c2(new DateTime(2012, 1, 20, 16, 30, 40, 50, DateTimeKind.Utc)));
}
Convert<string,DateTime>.Expression = s => DateTime.Parse(s, DateTimeFormatInfo.InvariantInfo);
ms1.SetConvertExpression<string,DateTime>(s => DateTime.Parse(s, new CultureInfo("en-US", false).DateTimeFormat));
ms2.SetConvertExpression<string,DateTime>(s => DateTime.Parse(s, new CultureInfo("ru-RU", false).DateTimeFormat));
{
var c0 = Convert<string,DateTime>.Lambda;
var c1 = ms1.GetConverter<string,DateTime>();
var c2 = ms2.GetConverter<string,DateTime>();
Assert.AreEqual(new DateTime(2012, 1, 20, 16, 30, 40), c0("01/20/2012 16:30:40"));
Assert.AreEqual(new DateTime(2012, 1, 20, 16, 30, 40), c1("1/20/2012 4:30:40 PM"));
Assert.AreEqual(new DateTime(2012, 1, 20, 16, 30, 40), c2("20.01.2012 16:30:40"));
}
}
示例6: GetMyIntSchema
private static MappingSchema GetMyIntSchema()
{
var schema = new MappingSchema();
schema.SetDataType (typeof(MyInt), DataType.Int32);
schema.SetScalarType(typeof(MyInt));
schema.SetCanBeNull (typeof(MyInt), false);
schema.SetConvertExpression<MyInt, int> (x => x.RealValue);
schema.SetConvertExpression<int, MyInt> (x => new MyInt { RealValue = x });
schema.SetConvertExpression<Int64, MyInt> (x => new MyInt { RealValue = (int)x }); //SQLite
schema.SetConvertExpression<decimal, MyInt> (x => new MyInt { RealValue = (int)x }); //Oracle
schema.SetConvertExpression<MyInt, DataParameter>(x => new DataParameter { DataType = DataType.Int32, Value = x.RealValue });
return schema;
}