Search code examples
sql-server-2000backwards-compatibilityentity-framework-4.3entity-framework-migrations

EF 4.3.1 Migrations - Conversion failed when converting date and/or time from character string


I am getting the error "Conversion failed when converting date and/or time from character string" when I'm trying to get my database versioned by EF migrations. The problem is the date string that EF generated ('2012-03-21T18:23:13.525Z') for new migration script entries is not supported by my version of MS Sql Server (Microsoft SQL Server Express Edition with Advanced Services (64-bit)). Is this a bug? Is there a work around?

CREATE TABLE [__MigrationHistory] (
    [MigrationId] [nvarchar](255) NOT NULL,
    [CreatedOn] [datetime] NOT NULL,
    [Model] [varbinary](max) NOT NULL,
    [ProductVersion] [nvarchar](32) NOT NULL,
    CONSTRAINT [PK___MigrationHistory] PRIMARY KEY ([MigrationId])
)
BEGIN TRY
    EXEC sp_MS_marksystemobject '__MigrationHistory'
END TRY
BEGIN CATCH
END CATCH
INSERT INTO [__MigrationHistory] ([MigrationId], [CreatedOn], [Model], [ProductVersion]) 
VALUES ('201203210144184_init', '2012-03-21T18:23:13.525Z',  0x33, '4.3.1');

EDIT

Nevermind. This has nothing to do with EntityFramework. If my SQL Server database is configured to be backwards compatible to SQL Server 2000, it won't accept that date format. I guess unless I can get EF to output it's date string in another format (or get SQL Server to both be backwards compatable to 2000 and still understand the EF date string), I won't be able to use EF migrations with my database :-(. Please let me know if someone has worked out a way to use EF with a database with compatibility level SQL Server 2000.


Solution

  • Just so this question has an answer, the answer is:

    Switch the SQL Server database compatibility mode from SQL Server 2000 to at minimum SQL Server 2005 and the issue with the date string parsing will go away.