There is a SQLite DB. It has a Studios table with the following structure:

CREATE TABLE Studios ( id INT PRIMARY KEY UNIQUE DEFAULT (0) NOT NULL, name STRING, filtered_name STRING, real BOOLEAN NOT NULL DEFAULT true, image STRING ); 

It is filled correctly and without errors from the List.

Interaction through linq2db. Entity classes are created according to the template that came with the library.

 public partial class FiltersDB : LinqToDB.Data.DataConnection { public ITable<Studio> Studios { get { return this.GetTable<Studio>(); } } public FiltersDB() { InitDataContext(); } public FiltersDB(string configuration) : base(configuration) { InitDataContext(); } partial void InitDataContext(); } [Table("Studios")] public partial class Studio { [PrimaryKey, NotNull ] public int id { get; set; } // int [Column, Nullable] public string name { get; set; } // string(max) [Column, Nullable] public string filtered_name { get; set; } // string(max) [Column, NotNull ] public bool real { get; set; } // boolean [Column, Nullable] public string image { get; set; } // string(max) } public static partial class TableExtensions { public static Studio Find(this ITable<Studio> table, int id) { return table.FirstOrDefault(t => t.id == id); } } 

I try to get the value of the table by the following method:

  public static List<ShikiApiLib.Studio> GetStudiosFromDB() { using (var db = new FiltersDB("Filters")) { var response = new List<ShikiApiLib.Studio>(); var table = db.Studios.Select(x => x); //(!)для дебага var table1 = table.ToList(); //(!)для дебага foreach (var studio in db.Studios.Select(x => x).ToList()) { response.Add(new ShikiApiLib.Studio() { id = studio.id, name = studio.name, filtered_name = studio.filtered_name, real = studio.real, image = studio.image }); } return response; } } 

on line with table1, a System.InvalidCastException: "Заданное приведение является недопустимым." error pops up System.InvalidCastException: "Заданное приведение является недопустимым."

And in the table the following is displayed:

enter image description here

UPD: If you delete from the table all the rows in which the image column is null , then the error does not appear = /

But this is not a solution, since I need these lines = /

UPD2: With this column, some kind of mysticism. For replacing null with an empty string or text like "no_image" before entering into a database table does not help. and deleting these lines eliminates the error again = /

  • real is a type in SQLite, possibly because as - Grundy
  • @Grundy is real for me is the name of the field and it is bool = / - MrModest
  • Well, try renaming it. for example in isReal and see if the error is corrected - Grundy
  • @Grundy changed this line like this: [Column(Name = "real"), NotNull ] public bool isReal { get; set; } // boolean [Column(Name = "real"), NotNull ] public bool isReal { get; set; } // boolean [Column(Name = "real"), NotNull ] public bool isReal { get; set; } // boolean all the same thing = / - MrModest
  • what's the point of change if you specify Column(Name = "real") ? - Grundy

2 answers 2

The string [t1].[real] as [real1] nothing to do with this — it is simply the purpose of the alias.

The real problem is that the database contains data whose types do not correspond to the specified ones. SQLite is an untyped DBMS, and the column types in its tables are no more than a hint (well, and they also set the order of the columns).

Check if your database is in id or real columns instead of a number or a boolean type of masking string.

It is also possible that the id type is not really int, but long. Check out this option too. By the way, in SQLite it is preferable to use long for the primary key, this speeds up access a bit and reduces space.

Another possible option is that in real there is a number, not a boolean type. Try int or long.

  • Comments are not intended for extended discussion; conversation moved to chat . - PashaPash

What was the magic I did not understand, but the problem was solved as follows:

Instead of using a previously prepared database, I created the database and tables directly in the code in the following way:

 using (var db = new FiltersDB("Filters")) { db.CreateTable<Studio>(); } 

With my class entity, I got a table of the following form:

 CREATE TABLE Studio ( id INTEGER PRIMARY KEY NOT NULL, name NVARCHAR (255), filtered_name NVARCHAR (255), real BIT NOT NULL, image NVARCHAR (255), ) ); 

With null in the image also works = /