oysteinkrog / SQLite.Net-PCL

Simple, powerful, cross-platform SQLite client and ORM - Updated version with PCL support
MIT License
353 stars 162 forks source link

With .NET core, Select (only) in SQLite doesn't work #360

Open erossini opened 7 years ago

erossini commented 7 years ago

Hi guys, I updated MobileCenter. MobileCenter added .NET Core in my projects. The bad consequence is SQLite is working but not for select. When I execute this easy code

public List<T> GetItems<T>() where T : ITableEntity, new()
{
    lock (locker)
    {
        return (from i in database.Table<T>()
                select i).ToList();
    }
}

public T GetItem<T>(int id) where T : ITableEntity, new()
{
    lock (locker)
    {
        return database.Table<T>().FirstOrDefault(x => x.Id == id);
    }
}

I receive an error

Cannot compile: Parameter

0ai6w6ge2si2

with this StackTrack

at SQLite.TableQuery1.CompileExpr(Expression expr, List1 queryArgs) at SQLite.TableQuery1.CompileExpr(Expression expr, List1 queryArgs) at SQLite.TableQuery1.CompileExpr(Expression expr, List1 queryArgs) at SQLite.TableQuery1.CompileExpr(Expression expr, List1 queryArgs) at SQLite.TableQuery1.GenerateCommand(String selectionList) at SQLite.TableQuery1.GetEnumerator() at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at SQLite.TableQuery1.FirstOrDefault() at SQLite.TableQuery1.FirstOrDefault(Expression1 predExpr) at MyExpenses.Repository.MyExpensesDatabase.GetItem[T](Int32 id) at MyExpenses.Repository.MyExpensesRepository.GetExpense(Int32 id) at MyExpenses.ViewModels.ExpenseItemViewModel.LoadData() at MyExpenses.ViewModels.ExpenseItemViewModel..ctor(Int32 expenseId, Boolean SaveOnDatabase) at MyExpenses.Views.ExpenseItem.LoadViewModel(Int32 expenseId) at MyExpenses.Views.ExpenseItem..ctor(Int32 expenseId) at MyExpenses.Views.ExpenseList.OnEdit(Object sender, EventArgs e) at Xamarin.Forms.MenuItem.OnClicked() at Xamarin.Forms.MenuItem.Xamarin.Forms.IMenuItemController.Activate() at Xamarin.Forms.Platform.UWP.MenuItemCommand.Execute(Object parameter) at System.Runtime.InteropServices.WindowsRuntime.ICommandToWinRTAdapter.Execute(Object parameter)

I have all my applications that working with the same procedure and it is working well. Tagged: sqlite.net core

erossini commented 7 years ago

I've created a new Xamarin project and I added the latest version of sqlite-net-pcl 1.3.3. I noticed in my project now there is .NET Core. I defined an entity

public interface ITableEntityMyExpenses {
    int Id { get; set; }
    bool IsDeleted { get; set; }
    DateTime UpdatedDate { get; set; }
}

Then a BaseTable

public class BaseTableMyExpenses : ITableEntityMyExpenses {
    [PrimaryKey, AutoIncrement]
    [Indexed]
    public int Id { get; set; } = 0;
}

And then a table

public class Expense : BaseTableMyExpenses {
    public DateTime ExpenseDate { get; set; }
    public int Cost { get; set; }
}

At the first time the app creates correctly the database. The database is empty.

sqlite-db

sqlite-db-empty

If I try to add a new record in the database I receive this error:

An item with the same key has already been added. Key: 3

(TKey key, TValue value, Boolean add) at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable1 source, Func2 keySelector, Func2 elementSelector, IEqualityComparer1 comparer) at SQLite.EnumCacheInfo..ctor(Type type) at SQLite.EnumCache.GetInfo(Type type) at SQLite.SQLiteCommand.BindParameter(sqlite3_stmt stmt, Int32 index, Object value, Boolean storeDateTimeAsTicks) at SQLite.PreparedSqlLiteInsertCommand.ExecuteNonQuery(Object[] source)
at SQLite.SQLiteConnection.Insert(Object obj, String extra, Type objType) at SQLite.SQLiteConnection.Insert(Object obj) at MyExpenses.Repository.MyExpensesDatabase.SaveItem[T](T item) at MyExpenses.Repository.MyExpensesRepository.SaveExpense(Expense item)
at MyExpenses.ViewModels.ExpenseItemViewModel.SaveExpenseOnDB() at MyExpenses.ViewModels.ExpenseItemViewModel.d__42.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at MyExpenses.ViewModels.ExpenseItemViewModel.<b__41_0>d.MoveNext()

What is wrong?

I created an update of this error here on StackOverflow.