'Prevent EF6 to convert my string to double

I have an SQLite database with a table Product which has a field "Barcode" nvarchar(255) NOT NULL UNIQUE.
In this table, I store barcodes like 8699036753633 and 78954106265.
I have a C# method to get the product by barcode:

public static Product GetProductByBarcode(string barcode)
{
    using (var db = new ApplicationContext())
    {
        db.Database.Log = Console.Write;
        //var sql = $"Select * from Product where Barcode=CAST('{barcode}' as NVARCHAR)";
        //var product = db.Product.SqlQuery(sql).AsNoTracking().FirstOrDefault();
        var product = db.Product
            .Where(x => x.Barcode == barcode)
            .AsNoTracking().FirstOrDefault();

        return product;
    }
}

As you can see, the parameter barcode is a string. I even tried casting it into a NVARCHAR but along the way EF6 'thinks' it is a number so tries to convert it to a double. This fails with an InvalidCastException:

   at System.Data.SQLite.SQLiteDataReader.VerifyType(Int32 i, DbType typ)
   at System.Data.SQLite.SQLiteDataReader.GetDouble(Int32 i)  

When I add a space or character to the parameter, it is working. No conversion is done. But now the product isn't found.

In my model I also added [Column(TypeName = "nvarchar(255)")] and in OnModelCreating I added modelBuilder.Entity<Product>().Property(t=>t.Barcode).IsUnicode(true).HasColumnType("nvarchar");

But nothing works. EF6 or the SQLite driver keeps converting my string to a double.
How can I prevent that?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source