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 >

Building a Code-First Model Using Attributes in Entity Framework

In previous posts we have created a database-first model, created a model-first model using an EDMX, and created a code-first model from an existing database. In this post, we will create a code-first model from scratch using DataAnnotations; in a later post we will create the same model using FluentAPI, and in another post we will demonstrate how to manage changes to your database schema using Code-First Migrations. For now, let's build a code first model using attributes! Creating the... Read more >
/* * * DON'T EDIT BELOW THIS LINE * * */ (function () { var s = document.createElement('script'); s.async = true; s.type = 'text/javascript'; s.src = '//' + disqus_shortname + '.disqus.com/count.js'; (document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s); }());