Using Entity Framework DbContext with Dependency Injection

I'm a huge fan of Dependency Injection (DI). It's gotten to the point where it's almost in my blood (pun intended). DI is standard issue on all my team's projects, and I fail any code reviews that don't include it unless there's a really solid reason why it doesn't work for that particular scenario. For quite a long time, I didn't include Entity Framework (EF) DbContext items in this mental pool of "things that should be injected". This... Read more >

Fixing SQL Server Spatial "Not a Valid Instance of Geography" Errors in C#

The project we're working on needs the ability for a user to define a polygon on a map, like this: Then, we need to save that defined polygon as a SQL Geography type in our database, which we access using Entity Framework. EF supports the DbGeography type, so we are using that to actually send the polygons to the database. NOTE: for this post, the database server is SQL Server 2008 R2, though I am not sure if the error... Read more >

Entity Change Tracking using DbContext in Entity Framework 6

We've been searching for a way to audit changes made to our database, and one method we found was doing so via Entity Framework change tracking. Here's how to do it, what's required to set it up, and some drawbacks of this method. Let's get started! Background Entity Framework tracks changes made to entities when those entities are loaded from the Context. So, if we load an entity and modify it, like so: var movie = context.Movies.Find(2); movie.... Read more >

Loading Entity Framework Related Entities with AutoMapper and Reflection

In a previous post I mentioned how we would be using AutoMapper to create Data-Transfer Objects out of Entity Framework entities; the DTOs would then be transmitted over a WCF service. Well, as often happens, a new requirement surfaced recently, and the solution we came up with turned out to be pretty useful, so now I'm sharing it with you all. The Problem The issue we had was this: because this app needs to keep performance in mind (and because... Read more >

Mapping Entity Framework Entities to DTOs with AutoMapper

One of the projects that my team is working on is a major WCF service that will serve as a hub for our company-specific data. This hub will allow multiple other projects to query for and receive certain data. Because we want to use Entity Framework for the data-access layer and WCF for the service layer, we figured we could just use the created Entity Framework Code First classes as DataContracts over WCF. Something like this: [Table("Team"... Read more >

Dapper vs Entity Framework vs ADO.NET Performance Benchmarking

We have an upcoming project in my group that is going to need to be very, very performant. This project will involve us querying for data from a SQL database, transforming that data into strongly-typed objects, then returning those objects to the calling system through a service layer. Eventually, this service will be a central pillar in our organization's service-oriented architecture (SOA), and as such it absolutely has to be fast. We generally want to use Entity Framework for our... Read more >

Using Code-First Migrations in Entity Framework

Entity Framework Code-First includes a new feature for maintaining database changes in code files: Migrations. Migrations are code files that describe what changes were made to the database schema at different points in time. You create the migrations when you need them, and the code will use those files (and a corresponding table in the target database) to keep the schema changes in sync with what the code expects. In other words, you can manage your database schema without ever... Read more >

Building a Code-First Model Using FluentAPI in Entity Framework

Now that we've created a Code-First model using Attributes, let's walk through the other manner by which we can create a model: FluentAPI. The Model Once again, we will be using the same CourseCatalog model: To start off, let's create the classes necessary to represent these entities: public class Student { public int ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public DateTime DateOfBirth { get; set; } public virtual StudentAddress Address { get; set; } public virtual ICollection<... Read more >