Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dealing with SQLite Autoincrement column mapping in LINQtoSQL

Tags:

c#

sqlite

orm

I have folowing entity

    [Table(Name = "Users")]
    public sealed class UserDB
    {
        private Int64 _id = -1;
        private string _username = string.Empty;

        public UserDB() { }

        public UserDB(RepositoryInfo repoInfo)
        {
            UserName = repoInfo.Account;
        }

        [Column(Name = "ID", Storage = "_id",  IsDbGenerated = true, IsPrimaryKey = true, UpdateCheck = UpdateCheck.Never)]
        public Int64 ID { get { return _id; } set { _id = value; } }

        [Column(Name = "UserName", DbType="nvarchar(50)", Storage = "_username")]
        public string UserName { get { return _username; } set { _username = value; } }
    }

ID is mapped to Autoincrement INTEGER type column (actually the only type possible with autoincrement in SQLite)

When I try to add a new user to DB like this, I get an error:

public static Int64 AddUser(DataContext context, RepositoryInfo repoInfo)
{            
    UserDB udb = new UserDB(repoInfo);

    //an ID of udb is -1, but I tried different values too, doesn't change result
    var userstable = context.GetTable<UserDB>();
    userstable.InsertOnSubmit( udb );


    context.SubmitChanges(); // here I get a error, see on screen shot

    return udb.ID;

}

Screen shot of the error:

EDIT After a googling for and checking, seems that SQLite simply doesn't provide any SCOPE_IDENTITY() function. But Linq To SQL injects it!

How can I change this?

like image 625
Tigran Avatar asked Aug 22 '11 15:08

Tigran


People also ask

Does SQLite support Autoincrement?

SQLite AUTOINCREMENT is a keyword used for auto incrementing a value of a field in the table. We can auto increment a field value by using AUTOINCREMENT keyword when creating a table with specific column name to auto increment. The keyword AUTOINCREMENT can be used with INTEGER field only.

Does primary key auto increment SQLite?

In SQLite, an AUTOINCREMENT column is one that uses an automatically incremented value for each row that's inserted into the table. There are a couple of ways you can create an AUTOINCREMENT column: You can create it implicitly when you define the column as INTEGER PRIMARY KEY .

What is Rowid SQLite?

By default, every row in SQLite has a special column, usually called the "rowid", that uniquely identifies that row within the table. However if the phrase "WITHOUT ROWID" is added to the end of a CREATE TABLE statement, then the special "rowid" column is omitted.

How many requests per second can SQLite handle?

Actually, SQLite will easily do 50,000 or more INSERT statements per second on an average desktop computer. But it will only do a few dozen transactions per second. Transaction speed is limited by the rotational speed of your disk drive.


1 Answers

Actually there is no solution found by me to resolve this issue, if not that one architectural one => Change linq query to change outputted SQL.

like image 107
Tigran Avatar answered Sep 27 '22 23:09

Tigran