带有类型转换器

时间:2015-12-07 19:05:48

标签: c# servicestack ormlite-servicestack

我正在使用一个小例子测试OrmLite的一些功能,并且在使用类型转换器时,在SQL表达式API和原始SQL之间发现了不同的行为。

我有2个域类:

public class Account : Entity
{
    public string Name { get; set; }
    public Money Balance { get; set; }
    public Currency Currency { get; set; }
}

public class Currency : Unit
{
    public string Name { get; set; }
    public string Symbol { get; set; }

    public Currency(string format)
    {
        this.format = format;
    }

    public static implicit operator string (Currency value)
    {
        return value.Symbol;
    }

    public static implicit operator Currency(string value)
    {
        switch (value)
        {
            case "EUR":
                return Euro;
            case "USD":
                return Dollar;
            default:
                throw new NotSupportedException();
        }
    }

    private static Currency euro = new Currency("{0:n2} €") { Name = "Euro", Symbol = "EUR" };
    private static Currency dollar = new Currency("${0:n2}") { Name = "Dollar", Symbol = "USD" };

    public static Currency Euro
    {
        get
        {
            return euro;
        }
    }

    public static Currency Dollar
    {
        get
        {
            return dollar;
        }
    }
}

类型转换器:

public class CurrencyConverter : OrmLiteConverter
{
    public override string ColumnDefinition { get { return "char(3)"; } }

    public override DbType DbType { get { return DbType.StringFixedLength; } }

    public override object ToDbValue(Type fieldType, object value)
    {
        return (value as Currency).Symbol;
    }

    public override object FromDbValue(Type fieldType, object value)
    {
        Currency currency = value as string;
        return currency;
    }
}

测试:

    [TestMethod]
    public void TestMethod()
    {
        var dbFactory = new OrmLiteConnectionFactory("Data Source=Server;Initial Catalog=Test;Integrated Security=True", SqlServerDialect.Provider);

        SqlServerDialect.Provider.RegisterConverter<Money>(new MoneyConverter());
        SqlServerDialect.Provider.RegisterConverter<Currency>(new CurrencyConverter());

        using (IDbConnection db = dbFactory.Open())
        {
            db.DropAndCreateTable<Account>();

            var account = new Account()
            {
                Name = "My account",
                Currency = Currency.Dollar,
                Balance = 200
            };

            db.Save(account);

            // Raw SQL get the correct value
            var result = db.Single<Account>("Currency = @currency", new { currency = Currency.Dollar });
            // SQL expression gets null
            var otherResult = db.Single<Account>(x => x.Currency == Currency.Dollar);
        }
    }

我希望在两个单调用上都收到相同的结果,但似乎SQL表达式查询没有使用类型转换器来获取参数的值。

有没有办法用SQL表达式实现这种行为?

1 个答案:

答案 0 :(得分:2)

默认情况下,键入的SqlExpression使用内联SQL而不是参数化查询来调用TypeConverter的OrmLiteConfig.UseParameterizeSqlExpressions = true; API。

您可以通过设置以下内容为SQL Server和Oracle的SQL表达式中的参数化查询启用预览支持:

312|fotelja snesko|bela|15|2900|fotelja|False
621|digimon tabure|crna|25|850|tabure|False
981|krevet trio|siva|8|5200|trosed|False