Linq2db create table with DateTime2(3) fails when using in memory db and SQLiteDataProvider

0

I am trying to create table in memory db using Linq2Db, and SQLiteDataProvider in a netcore3.1 application. And if mapping class has a property with attribute

[Column(DataType=DataType.DateTime2, Precision=3), Nullable ]

it gives me the following syntax error :

Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 1: 'near ")": syntax error'.

I dig for the query it generates and its this:

CREATE TABLE [testTable]
(
    [Id]      INTEGER         NOT NULL PRIMARY KEY AUTOINCREMENT,
    [Created] DateTime2(3, )      NULL
)

Here is an example that I'm trying:

using System;
using LinqToDB;
using LinqToDB.Data;
using LinqToDB.Mapping;

namespace InMemoryDb
{
    class Program
    {
        static void Main(string[] args)
        {
            DataConnection.AddConfiguration("default", "Data Source=Sharable;Mode=Memory;Cache=Shared", 
                new LinqToDB.DataProvider.SQLite.SQLiteDataProvider("SQLite.MS"));
            DataConnection.DefaultConfiguration = "default";
            using var db = new DataConnection("default");
            db.CreateTable<TestTable>();
        }
        
        [Table(Schema="dbo", Name="testTable")]
        public class TestTable
        {
            [Column(DataType=DataType.Int32), PrimaryKey, Identity] 
            public int       Id             { get; set; }
            [Column(DataType=DataType.DateTime2, Precision=3), Nullable] 
            public DateTime? Created { get; set; } 
        }
    }
}

why its generates query with DateTime2(3, ) and not a correct one DateTime2(3)?

sqlite
create-table
linq2db
asked on Stack Overflow Dec 21, 2020 by MonstriK • edited Dec 22, 2020 by Svyatoslav Danyliv

2 Answers

0

Try this as workaround

[Column(DbType="DateTime2(3)",    Nullable          ]
answered on Stack Overflow Dec 22, 2020 by IT.
0

You can use possibility of linq2db to define schema for several databases. Note that there is no DateTime type in SQLite.

[Table(Schema="dbo", Name="testTable")]
public class TestTable
{
    [Column(DataType=DataType.Int32), PrimaryKey, Identity] 
    public int       Id             { get; set; }

    [Column(Configuration=ProviderName.SQLite, DataType=DataType.DateTime2), Nullable] 
    [Column(DataType=DataType.DateTime2, Precision=3), Nullable] 
    public DateTime? Created { get; set; } 
}
answered on Stack Overflow Dec 22, 2020 by Svyatoslav Danyliv • edited Dec 24, 2020 by Svyatoslav Danyliv

User contributions licensed under CC BY-SA 3.0