Skip to main content

Posts

Showing posts with the label entity framework

Main difference between Code First, Model First, Entities First

In one of my latests post I wrote about  Hybrid Code First . When I started to write about it I realised that it is not very clear and transparent what does Code First, Database First and Model First are and what are differences between them. The main scope of this post is to define in a few words each of this mechanisms and what are the main difference between them. Code First - Define the domain first in code. Based on this domain, the database will be created on the fly by Entity Framework. This approach is used very often in combination with DDD (Domain Driven Development) Database First -  Define the database schema first. Based on it, EF generates the code that maps the database to our entities and the access mechanism Model First - Define the model in a design tool. Based on this design, EF will generate the entities and the database structure that is needed Now, that we know what are the available mechanism to define and map our model, let's see what are the...

Entity Framework - Hybrid Code First

Let's talk about an interesting subject related to Entity Framework (EF). When you are using EF there are different mechanism to map your model: Code First - we write the code first and the DB is generated automatically based on the code Database First - we define the DB structure and POCO entities are created automatically Model First - we design the model in a 'nice' designer. This designer will generate the classes and the database model All of them are perfect and works great. Based on our needs, preferences and team skills we can decide to go with an approach or another, but in the end we will end up with the same thing. I will follow up later on with a different post where I will compare them. Now, let's talk about different scenarios that is used by people. I saw in a lot of implementation where people are afraid of Code First or Model First. Because of this I realize that most of them are using a hybrid solution that I called Hybrid Code First . W...

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: How to get in a generic way entities from EF based on entity IDs

Offtopic: I’m back. After a few weeks when I was blocked with personal projects, I’m back on the field, ready to write technical content. Today I’ll start with something lights that can make our day more beautiful. How we can write a generic method to get an entity from database using Entity Framework? Let’s assume that we have different types of entities and we would like to obtain a specific entity using a generic method. Each entity can have the primary key field called in different ways. Also the type of the field can be different (one can be string, another one int and so on). For this cases we should use Find method. This method accept as input parameters a collections of objects that represents the keys of the entity. Using this method we can get entities from EF in a generic way. For compound keys, you need to specify the keys in the same order you specified them when you mapped the entity in EF. Why a list of keys and not only one input parameter? Well, we can have ...

ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded

Today blog post will be started with the following error when running DB tests on the CI machine: threw exception: System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information. at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName) This error happened only on the Continuous Integration machine. On the devs machines, everything has fine. The classic problem – on my machine it’s working. The CI has the following configuration: TeamCity .NET 4.51 EF 6.0.2 VS2013 It see...

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...