In case you haven’t heard, the Entity Framework team published a release candidate (RC1) last week. You can find the details in Rowan Miller’s blog post here. Enough with the unicorns…I am naming this version of Entity Framework Magic Narwhal Edition.
I was excited to try out the new bits and I thought I would run a test today to see if EF6 provided any noticeable performance difference in my test application. Unfortunately, I ran into a couple issues that stopped me from finding out.
1) Many-to-Many Table Naming Convention
This one threw me for a loop. After updating to EF6, I got the common “your model has changed since the database was created” message. I used the usual “Add-Migration” command and was surprised by what I saw. Entity Framework created a migration that deleted a few indexes, foreign keys and tables and created some new tables. Upon further inspection, I saw that something seems to have changed with naming conventions for tables used in many-to-many relationships.
For example, my model has a relationship between Users and Groups. A user belongs to many groups, and a group can have many users. In my EF5 database, this was represented with a table named UserGroups. For some reason, EF6 wanted the table name to be GroupUsers.
What is really dangerous about the migration created by EF6 is that the data was deleted. The new table was created and the old table was deleted, without ever moving the data across. To fix this, I added a step before dropping the old table:
Sql(“INSERT INTO dbo.GroupUsers SELECT Group_Id, User_Id FROM dbo.UserGroups”)
I had to repeat this for each table that was renamed. I’m not sure if this is expected behaviour, but watch out for it. You might lose data when you were not expecting to.
2) Running MiniProfiler
I like to use MiniProfiler to profile the SQL that is executed by Entity Framework. Unfortunately, I could not get MiniProfiler to work properly with EF6. No matter what configuration I used, I would always get this error:
Unable to cast object of type ‘StackExchange.Profiling.Data.EFProfiledDbConnection’ to type ‘System.Data.SqlClient.SqlConnection’.
I will continue exploring EF6 RC1 and report on my findings here. I am curious to hear from anyone else who has tried it out.