Strange System.InvalidCastException on insert 3.1.0, vs 2017, net4.6.2
See original GitHub issueI’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:
- Created 6 years ago
- Comments:8 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Your problem lies here:
inLt.Insert(inItem)
returns the Id of the inserted object, which is of course anint
, not abool
. The implicit casting tobool
will fail for this reason.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!