1

We are using EF6 code first approach and we have automatic migrations enabled (we are at the beginning of the project).

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

And in the Configuration class we have the following enabled in order for the DB to be updated automatically during each application start:

public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = true;
    }

The DB column names are explicitly mapped like this (with "HasColumnName") because we want to have full control over the column names:

modelBuilder.Entity<User>().Property(u => u.Gender).IsRequired().HasColumnName("Gender");

I've just noticed today that when I changed the name of the mapped column to begin with a lowercase ex:

modelBuilder.Entity<User>().Property(u => u.Gender).IsRequired().HasColumnName("gender");

... the automatic migration does not detect this as a change to the DB and does nothing i.e. the DB column name stays the same ("Gender" with an uppercase g).

It was only after I changed the column name to another word ex:

modelBuilder.Entity<User>().Property(u => u.Gender).IsRequired().HasColumnName("genders");

...that caused the automatic migrations to actually change the column name in the DB, which indicates that somehow the check for column name is done in a case insensitive way.

Does anyone know if this is by design, or is this a bug in EF? Additionally is there a way to force the automatic migrations to perform case sensitive column name checks?

Thanks in advance

aristov
  • 23
  • 1
  • 5

1 Answers1

0

Case sensitivity does not make much difference to DB, so I believe this is intentional.

If you don't like lowercase column name, just work around this:

  1. Rename the column from "gender" to something like "genders". Add migration
  2. Rename column back to "Gender". Add migration

Alternatively, you can manually edit generated migration code

SoftwareFactor
  • 8,430
  • 3
  • 30
  • 34
  • I already did a workaround as you suggested but I really don't like having to do this manually. I would expect that there is a way to force the migration script perform the case change directly. – aristov May 20 '14 at 07:27
  • I don't think there is any other way that would be worth the effort. Although this workaround involves some manual steps, it is quick and needed only once. I better spend the time on more important tasks. But as always, that is just my opinion ;) – SoftwareFactor May 20 '14 at 08:28