简体   繁体   中英

In-memory Entity Framework HasComputedColumnSql with the function replicate inside of unit test

I can define in my SQL Server a column with the following definition:

[Barcode] AS ([Prefix] + CONCAT(REPLICATE('0', (4)-LEN([ContainerId])), [ContainerId])) PERSISTED, 

This basically allows me to store a value on the database row with a given prefix like this:

ContainerId     Prefix    Barcode
----------------------------------
23              TUB       TUB0023

This works great but when I try to describe this for Entity Framework in the OnModelCreating :

entity.Property(f => f.Barcode).HasComputedColumnSql("[Prefix]+CONCAT(REPLICATE('0',(4)-LEN([ContainerId])),[ContainerId])");

and use it in a unit test, I get the following error:

Microsoft.Data.Sqlite.SqliteException: SQLite Error 1: 'no such function: replicate'.

Is there a limitation on using in-memory Entity Framework computed columns such that we can't have this call to HasComputedColumnSql in the OnModelBuilding event?

Assuming that you are using SQLLite in-memory, I should say SQLLite does not have REPLICATE function. There is a workaround here for SQLLite to use instead:

-- X = string
-- Y = number of repetitions

replace(hex(zeroblob(Y)), '00', X)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM