1

When trying to write tests in xUnit I get the following error:

Microsoft.Data.Sqlite.SqliteException : SQLite Error 1: unknown function: NEWID()

This appears due to the line :

modelBuilder.Entity<User>()
            .Property(u => u.EmailActivationGuid)
            .HasDefaultValueSql("NEWID()");

It appears SQLite doesn't support NEWID(). Is there a way to conform to both SQL Server and SQLite syntax?

user4157124
  • 2,809
  • 13
  • 27
  • 42
Scotland141
  • 314
  • 2
  • 13
  • 1
    No - different database products have different functions. *All* database vendors provide custom extensions. No database vendor supports the SQL standard beyond a basic level. SQLite has no GUID type (or data types for that matter, just storage classes) – Panagiotis Kanavos Sep 08 '20 at 12:11

1 Answers1

0

You can create a custom function to generate the Guid for you.

Create something like this code in your app

var connection = new SqliteConnection("connectionString");
connection.Open();
connection.CreateFunction("newid", () => Guid.NewGuid());
connection.Close();
  • How about sth else than Sqlite? – Mr Patience Nov 30 '22 at 08:01
  • Like what? And you can use Google search (How to create a custom Command "for your database type") this is for SQLite because it's not providing a built-in a Guid creation function. – Abdel Moumen Abdel Raouf Nov 30 '22 at 08:55
  • Great, why use stackoverflow, while I can Google everything... I was asking specifically about the tests - sqlite is ofter used as in-memory provider for ef core tests: https://learn.microsoft.com/en-us/ef/core/testing/choosing-a-testing-strategy#overall-comparison, however the code you've provided won't work for the default in-memory provider. – Mr Patience Nov 30 '22 at 09:07
  • I told you to use google search because you didn't mention the database type, any way did you tried [this solution](https://stackoverflow.com/questions/70664095/ef-core-in-memory-database-hasdefaultvalue-is-being-ignored/70664633#70664633) – Abdel Moumen Abdel Raouf Nov 30 '22 at 09:26