First time here? You are looking at the most recent posts. You may also want to check out older archives or the tag cloud. Please leave a comment, ask a question and consider subscribing to the latest posts via RSS. Thank you for visiting! (hide this)

April 2012 Blog Posts

Entity Framework 4.3.1 Migrations and non English locale

A few days ago I started migrating a web app I was working on from ASP.NET MVC 3 and EF4 to ASP.NET MVC 4 and EF 4.3.1. All went well, except for some problems with the automatic generation of the DB scheme using EF Code First: it was creating the database, but no tables and not even the __MigrationHistory table added in 4.3; and during the execution of the schema generation an error was raised with a weird datetime conversion error. I tried this both on SQL Express 2005 and SQL Compact and I got the same outcome, just different errors.

In SQL Express 2005 I got:

Conversion failed when converting datetime from character string.

In SQL Compact I got slightly more helpful error message:

The format of the specified date or time datepart is not valid.
[ String = 2012-04-19T13.21.04.364 ]

The cause of the problem

A bit of Googling, and I found out that this problem is related to how the __MigrationHistory is created and filled in with rows: for every “migration” a new row with, among other info, the timestamp of the migration is added.

INSERT INTO [__MigrationHistory] ([MigrationId], [CreatedOn], [Model], [ProductVersion])
VALUES ('201204231416585_InitialCreate', '2012-04-23T14.16.59.038Z', ...., '4.3.1')

The problem is that, probably due to the local of my local machine (Italian) and of my test database (French), the format in which the datatime value has been serialized was wrong: instead of 2012-04-23T14.16.59.038Z it should have been 2012-04-23T14:16:59.038Z

I asked the question on StackOverflow but even there nobody was able to find a solution for the problem, but at least a guy from the ADO.NET Team looked into it and, a few days later, came out with a solution.

It’s a bug in EF

The answer is that it is a bug, as they didn’t specify InvariantCulture when they do the ToString of the data to generated the SQL script. And it will be fixed in the next version of Entity Framework. But until it comes out, and you encounter the same bug, here is how to fix it.

The workaround

First thing you have to do is specify a custom SqlServerMigrationSqlGenerator, override the Generate(DateTime) method which the one responsible for generating the value of datetime objects, and specify the InvariantCulture option and the correct colon based format.

class FixedSqlGenerator : SqlServerMigrationSqlGenerator
{
    protected override string Generate(DateTime defaultValue)
    {
        return "'" + defaultValue.ToString("yyyy-MM-ddTHH:mm:ss.fffK"
                      , CultureInfo.InvariantCulture) + "'";
    }
}

How to inject the workaround

Then you have to configure EF to use your new migration generator instead of the default one. Where to put this configuration depends on what you are using.

Using Migrations

If you are using the Migrations it’s pretty easy: just call the SetSqlGenerator method in the Configuration class for the migration.

class Configuration : DbMigrationsConfiguration<ConsoleApplication3.BlogContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
            SetSqlGenerator("System.Data.SqlClient", new FixedSqlGenerator());
        }
    }

But bear in mind that this works only if you enabled migrations.

Using Code First

If instead, like me, your are just using Code First, and just want your DB automatically created (and then updated) you’ll never go via that configuration step, so the first automatic migration will not use your new Sql generator. Another small step is needed: you have to tell EF where the Configuration is:

Database.SetInitializer(
   new MigrateDatabaseToLatestVersion<BlogContext, Migrations.Configuration>());

Where to put this line really depends on the hosting application: you can put it in the Context class, at the beginning of your console application, or in the Global.asax.cs file in a ASP.NET MVC application.

More info

If you never used EF migrations I really recommend you play around with them: it’s a really neat way to keep track of your database, even if you are not using EF as your ORM. Here are two very nice blog posts from the ADO.NET EF team that show how migrations work, but in automatic and manual manner.

And finally I’d like to thank Brice from the ADO.NET EF for quickly responding to the issue and sending me the workaround.

Announcing the Web.NET Conference Europe, this fall in Italy

webnetconftitle

The web is evolving, and so are the techniques, technologies and frameworks used in web development. Together with Ugo Lattanzi we are organizing the Web.NET Conference Europe, this fall, in North Italy.

Logistics is not 100% certain yet: it will be in North Italy, probably in Milano, and will be this fall, probably toward the mid/end of October.

What is sure, instead, are the topics: Advanced JavaScript frameworks, REST frameworks, Cloud, lightweight http servers, real-time web, low-ceremony web frameworks, both in .NET and in other technologies.

We’ll probably launch a call for speakers around of May/beginning of June, but if you already have a idea, start thinking about. We are expecting a European audience, so all talks should be in English.

To get an email when things becomes more defined you can register on the launch page of the conference, http://webnetconf.eu. You’ll get a personal sharing URL with which you can help us share the good news, and get in return pre-emption right on the seats available, and might even win a free event t-shirt.

We also have a twitter account, @webnetconf, and a facebook page, and the hashtag for the conference is #webnet12

Now some background on how this conference came to existence: organizing conferences takes a lot of effort, especially if done with little help, and the organization of that last conference left me without strength. That’s also one of the reasons why UGIALT.net had its last conference this January. But during and after that day I had many people asking me why we quit, that the conference was the best conference they’ve been ever, and that the community still needed a objective view on the .NET development. I was a bit reluctant at first, but all that messages, and the active push of Ugo Lattanzi made me change my mind. And so here I am again, organizing another conference.

Using github behind a firewall

If you are behind a firewall that blocks port 22 you’ll not be able to access github using the standard procedure described in the Set Up Git help page.

If you are looking for more introductory information on using github and git I recommend you read my other post on Getting started with Git and GitHub.

When trying to login to github over ssh you get this error if you are behind a firewall that block port 22.

$ ssh -vT git@github.com
OpenSSH_4.6p1, OpenSSL 0.9.8e 23 Feb 2007
debug1: Connecting to github.com [207.97.227.239] port 22.
debug1: connect to address 207.97.227.239 port 22: Attempt to connect timed out
without establishing a connection
ssh: connect to host github.com port 22: Bad file number

Luckily there is a solution for that: Smart HTTP. Basically it will use HTTP to pull and push updates to github, thus using the standard SSL (443) port instead of using the blocked SSH port (22).

Opting in is pretty easy: when cloning a repo just use the https url instead of the git url, for example:

$ git clone https://username@github.com/Haacked/Subtext.git

If instead of cloning a repository you are creating a new one you have to specify the https instead of the standard git one:

$ git remote add origin https://username@github.com/username/project.git

But it’s not done yet: using SSH the first time you connect to github you get a message asking you to validate the SSH certificate of github. Unfortunately this does not happen when using SSL. The only thing you get is this error message:

$ git push -u origin master
error: SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate
verify failed while accessing https://username@github.com/username/project.git/info/refs
fatal: HTTP request failed

This means that the SSL certificate is not trusted. You could either manually install the certificate or simpler just tell him not to verify the HTTPS certification:

git config --global http.sslVerify false

HTH

Tags: , , ,