Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework 4.3.1 Code First: database created but tables are not
    text
    copied!<p>Today I migrated an old application from EF 4.2 to EF 4.3.1. In my app I was using CodeFirst, but after migration it stopped working, and couldn't find a reason for that. To clear any other possible problem I decided to create a small console application and I used the Data Migration walk-through published by the ADO team:</p> <p><a href="http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx" rel="noreferrer">http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx</a></p> <p>I copied exactly the code of the blog, but instead of working correctly (creating the DB, creating the schema, and inserting the blog) I get some errors:</p> <ul> <li>only the DB is created, but no tables</li> <li>I get this error <code>Conversion failed when converting datetime from character string.</code>"</li> </ul> <p>All of this is on SQL Server 2005 express.</p> <p>I tried the same using SQL Compact, but same result (tho different error):</p> <ul> <li>only the DB is created (in this case a sdf file in the bin folder), but no tables</li> <li>I get the error <code>The format of the specified date or time datepart is not valid. [ String = 2012-04-19T13.21.04.364 ]</code></li> </ul> <p>I think in both cases the problem lies in the line that EF wants to enter as first migration:</p> <pre><code>INSERT INTO [__MigrationHistory] ([MigrationId], [CreatedOn], [Model], [ProductVersion]) VALUES ('201204191321184_init', '2012-04-19T13.21.04.364', ...., '4.3.1'); </code></pre> <p>Apparently the format with the . is wrong, at least in my locale, it should be with :</p> <p>Is this a bug or what? It always worked with other datetime before.</p> <p><strong>UPDATE</strong> I tried running it as explicit migration, and applying the migration with the -verbose flag set, and here is what I get:</p> <pre><code>PM&gt; Update-Database -Verbose Using NuGet project 'ConsoleApplication2'. Using StartUp project 'ConsoleApplication2'. Target database is: '|DataDirectory|ConsoleApplication2.ConsoleApplication1.BlogContext.sdf' (DataSource: |DataDirectory|ConsoleApplication2.ConsoleApplication1.BlogContext.sdf, Provider: System.Data.SqlServerCe.4.0, Origin: Convention). Applying explicit migrations: [201204191356197_Initial]. Applying explicit migration: 201204191356197_Initial. CREATE TABLE [Blogs] ( [BlogId] [int] NOT NULL IDENTITY, [Name] [nvarchar](4000), CONSTRAINT [PK_Blogs] PRIMARY KEY ([BlogId]) ) CREATE TABLE [__MigrationHistory] ( [MigrationId] [nvarchar](255) NOT NULL, [CreatedOn] [datetime] NOT NULL, [Model] [image] NOT NULL, [ProductVersion] [nvarchar](32) NOT NULL, CONSTRAINT [PK___MigrationHistory] PRIMARY KEY ([MigrationId]) ) [Inserting migration history record] System.Data.SqlServerCe.SqlCeException (0x80004005): The format of the specified date or time datepart is not valid. [ String = 2012-04-19T13.56.45.437 ] at System.Data.SqlServerCe.SqlCeCommand.ProcessResults(Int32 hr) at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommandText(IntPtr&amp; pCursor, Boolean&amp; isBaseTableCursor) at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior behavior, String method, ResultSetOptions options) at System.Data.SqlServerCe.SqlCeCommand.ExecuteNonQuery() at System.Data.Entity.Migrations.DbMigrator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement) at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement) at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable`1 migrationStatements) at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, XDocument targetModel, IEnumerable`1 operations, Boolean downgrading) at System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration) at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ApplyMigration(DbMigration migration, DbMigration lastMigration) at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId) at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId) at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration) at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore() at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run() The format of the specified date or time datepart is not valid. [ String = 2012-04-19T13.56.45.437 ] </code></pre> <p><strong>Update 2</strong> I installed SQL Server Profiler, and profiled what is happening over there. I executed all the statements one by one via query analyzer and the one that fails is, as already stated above, the insertion of the migration.</p> <pre><code>INSERT INTO [__MigrationHistory] ([MigrationId], [CreatedOn], [Model], [ProductVersion]) VALUES ('201204231416585_InitialCreate', '2012-04-23T14.16.59.038Z', ...., '4.3.1') </code></pre> <p>When changing the format of the datatime string from <code>2012-04-23T14.16.59.038Z</code> to <code>2012-04-23T14:16:59.038Z</code> the command went through, so I guess somehow EF is sending the datatime in format that is not compatible with my locale.</p> <p>Thank you Simone</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload