Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLite - Can't insert null into TIMESTAMP column #20475

Open
Tracked by #22950 ...
Swellenator opened this issue Mar 31, 2020 · 9 comments
Open
Tracked by #22950 ...

SQLite - Can't insert null into TIMESTAMP column #20475

Swellenator opened this issue Mar 31, 2020 · 9 comments

Comments

@Swellenator
Copy link

Using in memory EF Core SQLite for integration tests. When creating an entity with the following column definition:

              entity.Property(e => e.TxTimestamp)
                    .IsRequired()
                    .IsRowVersion()
                    .IsConcurrencyToken();

Setting with

TxTimestamp = new byte[8] { 0, 1, 2, 3, 4, 5, 6, 7 }

I get the following error:

Microsoft.Data.Sqlite.SqliteException : SQLite Error 19: 'NOT NULL constraint failed: [TableName].TxTimestamp'.

Any ideas? It is like SQLite isn't inserting the value. Perhaps because with real SQL Server it wouldn't be able to? This model is generated from a SQL Server database and we are using database first, so I can't just change the model builder definition. (unless there is an easy way to override the one generated form the scaffold).

Additional context

Microsoft.Data.Sqlite version: 3.1.3
Target framework: netcoreapp3.1
Operating system: Win 10

@ajcvickers
Copy link
Contributor

Notes for team: In this case the property is configured for automatic concurrency with an expectation that this will work like it does in SQL Server. This is tracked by #2195. Within this, the insert of null happens because the SQLite provider doesn't support computed properties--tracked by #19682.

Should we check for one or both of these patterns in the model validator?

Model: 
  EntityType: Customer
    Properties: 
      Id (int) Required PK AfterSave:Throw ValueGenerated.OnAdd
      TxTimestamp (byte[]) Required Concurrency BeforeSave:Ignore AfterSave:Ignore ValueGenerated.OnAddOrUpdate
    Keys: 
      Id PK

/cc @bricelam

@Swellenator
Copy link
Author

My work around is:

 var sql = context.Database.GenerateCreateScript();

 // HACK: ef core wont put data into a rowversion column, but it is not null, so create a fake default
 sql = sql.Replace("\"TxTimestamp\" BLOB NOT NULL", "\"TxTimestamp\" BLOB NOT NULL DEFAULT (randomblob(8))");         
 await context.Database.ExecuteSqlRawAsync(sql);

to create a default value for the column. Certainly not ideal

@bricelam
Copy link
Contributor

bricelam commented Apr 3, 2020

See also #12260 (comment)

@ajcvickers
Copy link
Contributor

@bricelam I couldn't find an issue about comments/warnings in the generated code.

@bricelam
Copy link
Contributor

bricelam commented Apr 7, 2020

Found it: #2461

@bricelam
Copy link
Contributor

bricelam commented Apr 7, 2020

This exact scenario was previously discussed in #7295

@ajcvickers
Copy link
Contributor

Putting this on the backlog to consider generating some form of inline warning.

@ajcvickers ajcvickers added this to the Backlog milestone Apr 13, 2020
@AndriySvyryd AndriySvyryd changed the title SQLLite - EF Core 3.1 - Can't insert null into TIMESTAMP coulmn SQLite - Can't insert null into TIMESTAMP column Jan 14, 2022
@foreverhot1019
Copy link

Use [ConcurrencyCheck] to Manual management ConcurrencyCheck Key。
Sqlite not support [Timestamp] automaticlly(Timestamp Include RowVersion & ConcurrencyCheck)

@dorssel
Copy link

dorssel commented Mar 1, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants