SQLite.NET - System.NotSupportedException:无法编译:参数 [英] SQLite.NET - System.NotSupportedException: Cannot compile: Parameter

查看:428
本文介绍了SQLite.NET - System.NotSupportedException:无法编译:参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SQLite.NET异步( HTTP://www.nuget .ORG /包/ SQLite.Net.Async-PCL / )在Xamarin的iOS项目,但我在使用该表的查询谓词的一个问题。

I am using SQLite.NET Async (http://www.nuget.org/packages/SQLite.Net.Async-PCL/) in a Xamarin iOS project, but I am having a problem using the table predicate queries.

每当我使用下面的Get方法,这是很简单的,我收到的异常表达不能编译,System.NotSupportedException:无法编译:参数。

Anytime I use the Get method below, which is very simple, I receive an exception that the expression could not be compiled, System.NotSupportedException: Cannot compile: Parameter.

不过,如果我使用低级别的查询,与GetQuery方法,它工作正常。有什么我在我的表的定义或在阻止sqlite.net从编译表达方法做错了?

However, if I use a low level query, as with the GetQuery method, it works fine. Is there something I am doing wrong in the definition of my table or in the method that is preventing sqlite.net from compiling the expression?

public interface IDataModel
{
    [PrimaryKey, AutoIncrement]
    int Id { get; set; }
}

public class BaseDataModel : IDataModel
{
    [PrimaryKey]
    public virtual int Id { get; set; }
}

[Table("Event")]
public class EventDataModel : BaseDataModel
{
    public string Name { get; set; }
    public int OrganizationId { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime? EndDate { get; set; }
    public bool Active { get; set; }
}

public class DataService<T> : IDataService<T> where T : IDataModel, new()
{

    public virtual async Task<T> Get(int id)
    {
        var connection = await GetConnection();

        return await connection.Table<T>()
                .Where(item => item.Id == id)
                .FirstOrDefaultAsync();
    }

    public virtual async Task<T> GetQuery(int id)
    {
        var connection = await GetConnection();

        return (await connection.QueryAsync<T>("SELECT * FROM Event WHERE Id = ?", id))
             .FirstOrDefault();
    }
}



编辑#1:
这个问题似乎是相关的事实,我的方法是通用的。如果我将其更改为特定的模式connection.Table< EventDataModel>。凡(...它的工作原理将泛型方法行不通

Edit #1: The problem seems to be related to the fact that my methods are generic. If I change them to specific for the model "connection.Table<EventDataModel>.Where(..." it works. Will generic methods not work?

编辑#2。? :
我添加了一个类约束到T搭配现有的IDataModel,新的()约束一起去,这似乎已经解决了问题...这是否有道理

Edit #2: I added a 'class' constraint onto T to go along with the existing 'IDataModel, new()' constraints, and that seems to have fixed the issue...Does that make sense?

推荐答案

这有一定道理,添加一个约束将解决这个问题。

It does make sense that adding a class constraint would solve the issue.

当你写的:

public virtual async Task<T> Get(int id)
    where T : IDataModel, new()
{
    var connection = await GetConnection();

    return await connection.Table<T>()
            .Where(item => item.Id == id)
            .FirstOrDefaultAsync();
}

您看不到它,但是编译器会插入之间的项目 item.Id

You do not see it, but the compiler will insert a cast between item and item.Id.

这是,什么编译器实际上写的是:

That is, what the compiler actually writes is:

public virtual async Task<T> Get(int id)
    where T : IDataModel, new()
{
    var connection = await GetConnection();

    return await connection.Table<T>()
            .Where(item => ((IDataModel)item).Id == id)
            .FirstOrDefaultAsync();
}

这施放插入,因为这将是必要的,如果 ŧ是值类型。

That cast is inserted because it would be necessary if T is a value type.

这是很容易想象,为SQLite.net查询供应商不处理正确插入投,如这样做是不平凡的。

It is easy to imagine that the query provider for SQLite.net does not handle that inserted cast correctly, as doing so is not trivial.

添加约束允许编译器,以免误插入的演员阵容,导致简单的表达了SQLite.net查询供应商显然可以正确转换。

Adding the class constraint allows the compiler to avoid inserting that cast, resulting in a simpler expression that the SQLite.net query provider apparently can translate correctly.

这篇关于SQLite.NET - System.NotSupportedException:无法编译:参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆