Skip to main content

Posts

Showing posts with the label EF

Cascade delete is not working anymore in EF with SQLite

Two in one. In today post we will talk about two different topics: An example from real life that shows why unit tests are important An odd behavior of SQLite combined with Entity Framework  Why Unit Tests are important This days we had to update an application with the latest and greatest version of Entity Framework and SQLite. After a few hours of working we manage to update SQLite and EF to the new version and we manage to make the application to compile and start. Now was the moment of true, to check if the unit tests are still green. And surprise, some of them are red, even if the application seems to run without any kind of problem. It seems that in the new version of EF or SQLite the behavior or functionality of a specific feature was changed. Odd behavior of SQLite and Entity Framework Before the update: PRAGMA foreign_keys = ON; DELETE FROM CarsHistory WHERE (julianday(datetime('now'))-julianday(CarBuyDate))>360 Executed as a SQL command from Entit...

Entity Framework (EF) TransactionScope vs Database.BeginTransaction

In today blog post we will talk a little about a new feature that is available on EF6+ related to Transactions. Until now, when we had to use transaction we used ‘TransactionScope’. It works great and I would say that is something that is now in our blood. using (var scope = new TransactionScope(TransactionScopeOption.Required)) { using (SqlConnection conn = new SqlConnection("...")) { conn.Open(); SqlCommand sqlCommand = new SqlCommand(); sqlCommand.Connection = conn; sqlCommand.CommandText = ... sqlCommand.ExecuteNonQuery(); ... } scope.Complete(); } Starting with EF6.0 we have a new way to work with transactions. The new approach is based on Database.BeginTransaction(), Database.Rollback(), Database.Commit(). Yes, no more TransactionScope. In the fol...

EF 4.xx and storage procedure

In EF 4.xx operatiile de tip CRUD sutn foarte usor de executat. Dar ce se intampla daca avem nevoie sa executam o procedura stocata. De exemplu dorim sa populam un tabel, iar pentru acest lucru trebuie sa rulam o proceduta stocata. Pentru acest lucru avem doua posibitati. Prima din ele este sa ne cream un obiect de tip SqlQuery care poate sa contina orice comanda sql, iar apoi sa il executam. DataContext.Database       .SqlQuery<int>("EXEC PopulateTable @param1 @param2",             new SqlParameter("param1", "100"),             new SqlParameter("param2", "1/12/2011")); Aceasta modalitate este perfecta cand stocata/comanda pe care o executam ne returneaza date. Cand folosim aceasta comanda, data context stie ca datele au fost modificate. In cazul in care comanda pe care o executam nu ne re...