0

I'm trying to use a local SqlLite database (in my app_data) folder with LinqToDb.

Anybody an idea why LinqToDb cannot work with the following connectionstring ?

  <connectionStrings>
    <add name="RecyclesDB"
         providerName="System.Data.SqlLite"
         connectionString="data source=|DataDirectory|Recycles.sqlite3;Version=3;" 
         />
  </connectionStrings>

I can perfectly use this connectionstring to retrieve data the old-school way.

  SQLiteConnection sqlConn;
  var sqlCmd = new SQLiteCommand();
    using (sqlConn = new SQLiteConnection(ConfigurationManager.ConnectionStrings["RecyclesDB"].ConnectionString))
    {
      sqlConn.Open();
      sqlCmd.Connection = sqlConn;
 /* .... */               
    }

However, when I try to use LinqToDB with the following t4 template it fails

<#@ template language="C#" debug="True" hostSpecific="True"                           #>
<#@ output extension=".generated.cs"                                                  #>
<#@ include file="$(ProjectDir)LinqToDB.Templates\LinqToDB.SQLite.Tools.ttinclude" #>

<#@ include file="$(ProjectDir)LinqToDB.Templates\PluralizationService.ttinclude"     #>

<#
    NamespaceName = "RecycleDashboard";
    LoadSQLiteMetadata(LoadSQLiteMetadata(@"C:\inetpub\RecycleDashboard\app_data", "Recycles.sqlite3"););
    GenerateModel();
#>

To specify the issue. The T4 template generates the models alright, but when I try to use these with the following code I get a null reference

    using (var db = new RecyclesDB())
    {
        var recycles =
            (from recycle in db.AppRecycles
             select recycle).ToList();
    }

The exception on new RecyclesDB() is :

System.NullReferenceException was unhandled by user code
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=linq2db
  StackTrace:
       at LinqToDB.Data.DataConnection..ctor(String configurationString) in i:\linq2db\Source\Data\DataConnection.cs:line 41
       at LinqToDB.Data.DataConnection..ctor() in i:\linq2db\Source\Data\DataConnection.cs:line 24
       at RecycleDashboard.RecyclesDB..ctor() in 
 ...

Anybody an idea what's wrong here ?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
AardVark71
  • 3,928
  • 2
  • 30
  • 50

1 Answers1

0

OK, just in case anyone else stumbles over issues with the Sqlite Dataprovider too

I resolved this but specifying my DataConnection specifically as follows (GetDataProvider and GetConnection as suggested on https://github.com/linq2db/linq2db):

public partial class RecyclesDB : LinqToDB.Data.DataConnection
    {
    public RecyclesDB() : base(GetDataProvider(), GetConnection()) { }

    private static IDataProvider GetDataProvider()
    {
        return new LinqToDB.DataProvider.SQLite.SQLiteDataProvider();
    }

    private static IDbConnection GetConnection()
    {
        return new SQLiteConnection(ConfigurationManager.ConnectionStrings["RecyclesDB"].ConnectionString);
    }
}

Not sure why the code generated by the T4 template fails, but it looked like the LinqToDB DataConnection class couldn't resolve the dataprovidername 'System.Data.SQLite' automatically.

AardVark71
  • 3,928
  • 2
  • 30
  • 50