question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

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
   }
  }
 }
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
fdipumacommented, Mar 28, 2017

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.

0reactions
lbnascimentocommented, Feb 5, 2020

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

InvalidCastException Class (System)
The exception that is thrown for invalid casting or explicit conversion.
Read more >
visual studio - How do I get VS 2019 to break on System. ...
Open your solution and in the Exception Settings window (VS menu item Debug -> Windows -> Exception Settings ), type System.
Read more >
The strange case of System.InvalidCastException (“Unable to ...
The strange case of System.InvalidCastException (“Unable to cast COM object of type 'System.__ComObject' to class type System.Windows.Forms.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found