In search of a tool for working with a database using LINQ, I came across linq2db, but I’m foul, or the documentation is too meager, but I’m stuck on an elementary Insert. Link to github library: https://github.com/linq2db/linq2db

Here is my table in the db:

enter image description here

[Table(Name = "AnimePosters")] public class AnimePoster { [PrimaryKey, Column(Name = "title_id"), NotNull] public int title_id { get; set; } [Column(Name = "original"), NotNull] public string original { get; set; } [Column(Name = "preview"), NotNull] public string preview { get; set; } [Column(Name = "x96"), NotNull] public string x96 { get; set; } [Column(Name = "x48"), NotNull] public string x48 { get; set; } } public class DBRatesDB : LinqToDB.Data.DataConnection { public DBRatesDB() : base("RatesDB") { } public ITable<AnimePoster> AnimePoster { get { return GetTable<AnimePoster>(); } } } 

Described entities for the table. The compiler does not swear at reading (Select), but when I try to use Insert as written in ReadMe.

Example from README.md

 using (var db = new DbNorthwind()) { db.Insert(product); } 

As I understand it, product is an instance of an entity class that defines a table from a database, that is, for me it should be AnimePoster, but it gives me an incomprehensible error.

enter image description here

Can anyone help with this or you have your own library versions for working with LINQ with Access or SQLite databases (any of them). I would be very grateful if you attach examples to your options for basic queries like Select | Insert | Delete | Update or a link to the documentation where this is all described in detail.

  • And you just put this nuget package linq2db.Access ? - Bulson
  • @Bulson, installed like this: PM> Install-Package linq2db - MrModest
  • And it would be necessary to install like this - Bulson
  • @Bulson, what's the difference? Doesn't my version already include Access? - MrModest

1 answer 1

Library documentation is not bad enough. The library uses Expression trees to provide type safety and error detection at compile time.

First of all, it allows you to get rid of the "magic strings" for the names of database objects and provides a simple C # syntax for generating queries to the database.

Specifically, in your example, you have an error in use (and yes, the documentation has it). And Yes, Insert in this case is a command where you need to specify what to insert is not much different:

 using (var db = new DbNorthwind()) { AnimePoster ap = new AnimePoster(); // get animePoster db.AnimePoster .Value(p => p.original, () => ap.original) .Value(p => p.preview, () => ap.preview) .Value(p => p.x48, () => ap.x48) .Value(p => p.x96, () => ap.x96) .Value(p => p.title_id, () => ap.title_id) .Insert(); } 

As @Bulson correctly noted in an easier way (if you don’t need to choose which fields to insert and which not), it will be simple:

 using (var db = new DbNorthwind()) { AnimePoster ap = new AnimePoster(); // get animePoster db.Insert(ap); // Вместо db.AnimePoster.Insert(ap); } 

Or with Identity :

 using (var db = new DbNorthwind()) { AnimePoster ap = new AnimePoster(); // get animePoster db.InsertWithIdentity(ap); } 

Actually under the heading Insert there are more complex and flexible examples and more simple ones. In the code that you have, you can also insert several elements into the database at once:

 using (var db = new DbNorthwind()) { var animePosters = db.AnimePoster.Select( /*Ваш Select*/x => x); db.BulkCopy(animePosters); } 
  • Something I did not understand, so this is something wrong: db.Insert(product); ? - Bulson
  • @Bulson, no, not mistaken. - Anton Komyshan
  • @Bulson, updated the answer, thanks. Immediately did not want to bring simply .Insert due to the fact that it is not Identity-insert. - Anton Komyshan
  • one
    db.InsertWithIdentity(ap); According to the documentation, the ID of the newly added element should be given, as I understood from the documentation. - Bulson
  • one
    @ Mr.Modest, not really. He himself will not understand anything. Just in this example, the indication of the table is not explicit. To do this, you can clearly write: db.Insert<AnimePoster>(animePoster) . But in C # this is not necessary, because the generic type ( <AnimePoster> ) can be inferred knowing the type of the parameter ( animePoster ). Alternatively, you can also explicitly specify the name of the table with the string like this: db.Insert(product, "AnimePoster"); - Anton Komyshan