Litedb: Strange System.InvalidCastException on insert 3.1.0, vs 2017, net4.6.2

Created on 24 Mar 2017  路  8Comments  路  Source: mbdavid/LiteDB

I'm don't understand why exception happens on last insert

```C#
using LiteDB;

namespace WindowsFormsApp122
{
public class dbTestItem
{
[BsonId]
public int Id { get; set; }
public int SomeValue { get; set; }
}

public static class dbTestItemHelper
{
public static bool New(this dbTestItem inItem, LiteRepository inLt)
{
return inLt.Insert(inItem);
}
}

static class Program
{
static void Main()
{
var s = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestDb");
if (File.Exists(s)) File.Delete(s);
using (var db = new LiteRepository(s))
{
db.Insert(new dbTestItem()); // ok
var wtfitem = new dbTestItem();
wtfitem.New(db); // fail: System.InvalidCastException
}
}
}
}
```

question

Most helpful comment

Your problem lies here:

  public static bool New(this dbTestItem inItem, LiteRepository inLt)
  {
   return inLt.Insert(inItem);
  }

inLt.Insert(inItem) returns the Id of the inserted object, which is of course an int, not a bool. The implicit casting to bool will fail for this reason.

All 8 comments

Please. paste error stack strace to be simple to identify the problem

@mbdavid What is the wtfitem.New(db) doing here? Is the New an extension method? Didn't see this before

Yes, New is an extension method. I don't get why this code are not working....

Sorry I didn't see the middle of the code, I thought the New is LiteDB's extension method which I never see before. :(

Seems the report is correct, here is the stack:

System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.Boolean'.
   at LiteDB.BsonValue.op_Implicit(BsonValue value)
   at App1.dbTestItemHelper.New(Customer inItem, LiteRepository inLt)
   at App1.MainPage.MainPage_Loaded(Object sender, RoutedEventArgs e)

Type Customer is from your example code; In fact the Insert does work, I can see the data in the db file (as json format:)

{
    "_id": 1,
    "Name": "John Doe",
    "Age": 39,
    "Phones": [
        "8000-0000",
        "9000-0000"
    ],
    "IsActive": true
}

Exception: Specified cast is not valid.

LiteDB.BsonValue.op_Implicit(BsonValue value)
WindowsFormsApp122.dbTestItemHelper.New(dbTestItem inItem, LiteRepository inLt)
First [insert] work and on second [via extension method] fail

Your problem lies here:

  public static bool New(this dbTestItem inItem, LiteRepository inLt)
  {
   return inLt.Insert(inItem);
  }

inLt.Insert(inItem) returns the Id of the inserted object, which is of course an int, not a bool. The implicit casting to bool will fail for this reason.

Hi @fdipuma, you right! I didnt get it. Insert returns BsonValue that contains implicit converter to bool/int/....

Hi! With the objective of organizing our issues, we are closing old unsolved issues. Please check the latest version of LiteDB and open a new issue if your problem/question/suggestion still applies. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

axelgenus picture axelgenus  路  3Comments

LiamKenneth picture LiamKenneth  路  3Comments

sandolkakos picture sandolkakos  路  3Comments

darcome picture darcome  路  3Comments

onurhkb picture onurhkb  路  4Comments