Entity Framework – reset migrations

tl;tr; How to reset migrations in Entity framework, and start with raw database

While I was working at the project for Imagine Cup. I came across to one major problem. What if I want to delete all migrations and start over with raw migration (but at same database). In my case it was due to one of my update-database, it doesn’t run correctly, some sort of error I couldn’t figure out, how to fix. I decited to start with raw migrations.

  1. First of all, you have to type this commandn “Update-Database -TargetMigration:0 -Force” (in Package Manager Console) it wipe out all your changes at db but models stays in it current state.
  2. Then you have to delete Migrations folder
  3. Delete __MIgrationHistory at your database it can be located in SystemTables
  4. and all others tables in traget db.
  5. (Please keep the order of steps, because if you delete first migrations folder, it’s over make new project…)

After all these steps, now we can start do your raw migration “Enable-Migration” if there is need, use “Enable-Migration -Force”. Visit here to learn how to migrate.

Entity Framework – missing feature

When I was working on this test
I saw big disadvantage, there is no method created to delete collection of items at our database, on the other hand other ORMs have this feature. I think it’s good idea to have this feature, so I created topic with my idea. If you need this feature, please vote.

Now :

After :

 

ORM Series : Entity Framework Migration

Entity Framework, has given with last update, migrations at code first, undoubtedly it’s the best feature in this update. This tool is very helpful if we change something at our model, and then we can simply upgrade our database. Moreover it can fulfill role as a database versioning, because we can easily to manage our updates and freely manage between them.

Whole system is based on Package Manager Console. Basic commands

  • Enable-Migrations
  • Add-Migration {name of commit}
  • Update-Database

Enable-Migration we are using only once. It’s command which create basic files need to start work with migrations. Add-Migration is command we will use if we want to add some changes to database, and it generate file with . Update-Database – execute migrations files

First we have to create our model, I created simple music database:

 

 

 

Now we will run our Packer Manager Console with Enable-Migrations command.

If executed command was properly , we will see ‘Migrations’ folder.

image

Now we can execute next command, Add-Migration BaseOfDatabase. ‘BaseOfDatabase’ is name of commit so choose wisely because it should describe in one – two words what you have done here. Moreover there is a new generated file.

image1

That file include code that will be executed at final step, code sample one from few methods.

 

Now we can execute all of updates. By using Update-Database command. If everything goes properly, let’s change our model to show you, power of Migrations.

Album.cs

Song.cs

Let’s now commit our changes, execute Add-Migration command, result of execution

Update database, our database has given these two columns. Now we can freely downgrade or upgrade our version of database. 🙂 so if we decided after thousands of commit to delete these columns

Score : For this feature I will give EF two stars in category “Uniqueness”
summary_sec

ORM Series : Performance Part I : NHibernate, EF, OpenAccess, XPO Devexpress

In one of my companies on of the first tasks was to create appllication, that will show which ORM will be the best choice for my team ( in terms of performance, support and features). Main focus of this task was performance. My company has a online shop that is generating a huge ammout of queries. This was an important task, because we planned to redesign whole project with ORM in order to improve efficiency and get rid off all the complex stored procedures with business logic.

I selected four popular ORMs (Entity framework, OpenAccess, XPO Devexpress, NHibernate). Each test was ran ten times to reject gross error (values were averaged)

Local machine – Summary

summary

Observations

  • Insert, probably you have wondering why single insert record has more time than inserting ten records. Probably its cost of first connection with database. And this problem is not only shown in insert but it’s happend in Update and Delete. The best performance has NHibernate 1 minute and 20 seconds with inserting 10k records! either OpenAccess has scored 1 minute and 39 seconds, opposite site  Entity Framework with 3 minutes and 5 second and it’s the worst score.
  • Update, undeniably, the fastest update has got NHibernate, it has won updating all of values but in the 10k records NHibernate just crushed his competition! 5 second . The worst time has got OpenAccess 1 minute 12 second.
  • Delete, it’s very good example how syntax and features influence in speed of our queries just one ORM reached this horrible time – Entity Framework, there is no something like deleting collection and I was forced to using foreach statement to delete all items in collection (10k)! 3 minutes and 49 seconds. Other ORM done well their job but the best is XPO.

Conclusion

summaryFinal

Undoubtedly the best time has NHibernate, it hasn’t lost any category and it won most of them.
After NHibernate, XPO Devexpress ranks in the second position. The worst was Entity Framework it lost the most time and its deleting time is just killing for our database.

EF version : 5.0.0.0
XPO version : 12.2.4.0
NHibernate version : 3.3.1.4000
OpenAccess version : 2012.3.1209.1