Entity Framework Archives - Falafel Software Blog

Update-Database SQL Server connection error

Connection Error when Executing Code First Migrations from Class Library

By | Entity Framework | No Comments

I am fairly proficient at using Code First Migrations in Entity Framework, but recently I encountered a problem that was a real head-scratcher. Read on for the full story, or just skip to the end for the important details. I was working on a brand new ASP.NET Web API application, creating entity models and configuring their mappings in a separate class library. I added an App.config to the class library and set up the connection string my DbContext would need to access the database. When the time came to add my first migration I opened the Package Manager Console (PMC)…

Read More

Azure Mobile Apps, Tables with Integer Keys, and You

By | Azure, C#, SQL Server | 5 Comments

This is post 5 of 11 in the series “Azure for Developers” Microsoft Azure Mobile Apps  (MAMA) is one of the four prongs of the Microsoft Azure App Service platform, bringing Mobile, Web, Logic, and API apps all together in a single container. While it doesn’t offer anything that you couldn’t create on your own, it does offer value by providing boilerplate plumbing that simplifies common tasks such as database CRUD, offline data sync, and universal push notifications. In this post, I’m going to discuss some of the behavioral details of database CRUD out of the box. If you create a brand-new Azure Mobile App today,…

Read More

Inspecting your Entity Framework SQL

By | .NET | 5 Comments

If you use Entity Framework, you know it greatly simplifies database access for the developer. But it can also introduce its own problems particularly when you are writing more complex LINQ to Entities queries which may generate poorly-performing SQL or unexpected results. This is no reason to avoid using Entity Framework, but it helps to know how to inspect and troubleshoot the SQL your code is executing. The following are a few ways I like to inspect and troubleshoot my generated SQL when developing new code or troubleshooting data-related issues. 1. LINQPad If you haven’t tried LINQPad, you should go download…

Read More

Automatically Adding or Removing Fields from a Complex Type in Entity Framework

By | .NET | 2 Comments

Every ORM has its pros and cons, but one task I had to do several times before it finally stuck is how to automatically update a pre-existing function import in Entity Framework. I always ended up fumbling around, trying to remember if it is the function or the complex type that needs the updates, or both, and it just never seemed like a smooth process. Maybe I just don’t do it enough, since many procedure changes are transparent once the initial import is complete. Or maybe I’ve just been burned too many times by a combination of Entity Framework and Team Foundation Server causing me to have to make these changes manually during a code merge. But in a perfect world, if you need to add or remove a field, you can do it automatically by just doing an update! Here are the steps, for your quick and easy reference (and mine, too).

Read More

Entity Framework and Enum Flags

By | .NET, C# | 2 Comments

If you have ever had the need to store one or more possible values in a single field, you may have used the [Flags] attribute on an enum in C# which would look something like this:

The numeric values that we assign are important, because it allows us to do bit-wise operations on the number.  The values themselves can easily be calculated by raising 2 to the power of a zero-based sequence of numbers.  So this: 2⁰, 2¹, 2², 2³, 2⁴, 2⁵, 2⁶ yields this (yes, 2 to the zero power is one): 1, 2, 4, 8, 16, 32,…

Read More

You can use LINQ’s ‘let’ keyword to tune the emitted SQL

By | .NET, C# | 10 Comments

Here’s an interesting little factoid I just stumbled across during ordinary development. I wanted to see if projecting a row into an object with a nested object would impact the generated SQL. Here’s the original query:

And here’s what I wanted to test to see if the resulting SQL would be any different:

And it wasn’t. Here’s the output SQL for both LINQ queries above. (Anyone else bothered by the term “LINQ query?” Because if you expand the acronym, you’re saying “Language Integrated Query query.”)

So now I know that I can group together related columns in an…

Read More

The Four Deadly Sins of LINQ Data Access: Part 3–Insufficiently Chatty Queries

By | News | No Comments

This is post 3 of 4 in the series “The Four Deadly Sins of LINQ” Last time in this series, I showed how a common and easy-to-use method of Entity Framework would result in more columns than necessary being returned from the database, and before that, I described the cause and solution for Overly Chatty Queries. At the end of the last post, I said that yes, there is such a thing as an “Underly” chatty query, and I suspect that statement might have sounded odd to some. After all, the first post was all about how bad it is…

Read More

The Four Deadly Sins of LINQ Data Access: Part 1–Overly Chatty Queries

By | News | No Comments

This is post 1 of 4 in the series “The Four Deadly Sins of LINQ” Introduction I love using LINQ to filter, transform, and aggregate collections. LINQ provides a very concise and expressive syntax that lets you do more with fewer lines of code, which in turn means generally higher productivity, reduced maintenance, and fewer bugs. However, all of these things are unconditionally true only regarding in-memory collections. When it comes to querying a relational DB, LINQ has a dark side: it’s almost too good at hiding what’s really happening. This could cause a naïve developer to write poor data…

Read More

Using Code-First Entity Framework with Preexisting Databases

By | Tools | One Comment

If you’ve been using database-first Entity Framework for a while, it’s time you tried code-first Entity Framework. It’s actually much easier to use in a lot of ways, and creates a lot less cruft than database-first EF. Despite the name “code-first” you don’t actually have to write the model code first before creating your database. If you’ve already got a database, there is a great Visual Studio add-in called Entity Framework Power Tools that will help you do just that. After connecting the wizard to your database, it will create a context class, entity classes, and mappings, all as POCO…

Read More

Entity Framework and Custom Properties

By | News | 2 Comments

Entity Framework makes it very easy to get data from your database through mapped entities and having foreign keys mapped as navigation properties makes it really easy to traverse relationships in your database.  However, there is often a lot of overhead if you “include” your entire child entity along with the primary, parent entity. For example, let’s say we have an Order entity and each Order has a non-null ShippingAddressId that gives us a 1..1 to the Address entity.  We can use Include() to return that related Address entity:

But what if our Address entity has 20 columns in…

Read More

Generic Repository Pattern with Entity Framework and Web API

By | Uncategorized | 5 Comments

If you are like any other coder, cutting and pasting snippets should put up a red flag in your mind. You will quickly hit this threshold when creating CRUD-services for MVC 4 applications over and over again. This lead me to create a repository pattern using generic types and base controller classes that I would like to share with you. In the end, below is how my Web API classes look like, which automatically give me create, retrieve, update, and delete functionality: using MvcWebApp.Helpers; using MvcWebApp.Models.Data;    namespace MvcWebApp.Api {     public class SpeakersController : BaseApiController<Speaker>     {         //THIS IS EMPTY, BUT I GOT…

Read More

A Salute to the Falafel Rock Stars

By | News | No Comments

Every year, October kicks off a very busy season at Falafel : “Conference Season”. Multiple members of the team travel across the country and around world to share their expertise on a number of technologies. All Falafel Software team members spend their days, and often their nights, helping design and build cutting edge solutions for our customers. Their technical presentations draw from these real world experiences and are assembled, practiced, and presented in what little spare time they have. That’s why we call them “Rock Stars”. This weekend at Silicon Valley Code Camp, Falafel’s entire team from around the world…

Read More

WCF RIA Services Toolkit: SOAP Endpoints

By | .NET, C#, Silverlight | No Comments

If you have used WCF RIA Services along with Entity Framework for Silverlight applications, you know how useful they are for performing database operations from your application.  With the WCF RIA Services Toolkit, it gives you even more flexibility to expose your RIA DomainService methods to not only a Silverlight application, but any application that can access a SOAP or JSON endpoint. I will provide some steps here for setting up and consuming data through a SOAP endpoint. 1. Install WCF RIA Services Toolkit You will need to install the toolkit in your development environment.  You can get it here….

Read More

Silverlight Binding on Strings; Fix Blank Strings

By | Uncategorized | No Comments

I have seen an interesting and sometimes annoying side effect of using Silverlight binding on string properties that makes them turn from a null string to an empty (length 0) string. Typically my bindings are bound to WCF RIA Services entities that can have a nullable string. By simply selecting and then unselecting a TextBox that is bound to a nullable string in an entity will cause it to become an empty string.   This wasn’t what I wanted to happen. I need strings that have been changed to empty to stay null if and only if the underlying entity…

Read More

Falafel CEO Interviewed on Popular Podcast

By | News | No Comments

This week our CEO, Lino Tadros, is the special guest on  the popular .NET development show, “Yet Another Podcast”. The show is hosted by Jesse Liberty, a Senior Developer-Community Evangelist on the Windows Phone Team. Jesse and Lino discuss developing applications for Windows Phone, MVVM, Silverlight, Visual Studio 11 and more. Lino also makes a big promotional offer during the interview, so if you are itching to learn a little bit about Windows Phone, you are going to want to tune in. Grab the podcast here: http://jesseliberty.com/2012/02/06/yet-another-podcast-59lino-tadres/

Read More

Calculated Field Queries with LINQ to Entities

By | C# | No Comments

Entity Framework can save you a lot of time when developing data operations in your application.  I am going to demonstrate some techniques to use calculated fields to filter or constrain your results in a LINQ to Entity query. Before we get started, we’ll use a simple example of a Contact entity that has one or more Address entities associated with it: public class Contact { public int ContactId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public DateTime Birthday { get; set; } public List<Address> Addresses { get; set;…

Read More

Visual Studio Error Message: “The project’s target framework does not contain Entity Framework runtime assemblies”

By | Tools | No Comments

My friend Basem has a nice blog post about the most useful features of the Productivity Power Tools extension for Visual Studio 2010.  It’s definitely worth a read if you haven’t read it already.  Like Basem, I love the Productivity Power Tools extension and use most of it’s features on a daily basis.  And, until recently, I’d run into only a very few issues with it, all of which I’d classify as minor annoyances.  Recently though, I ran into one issue that put me off track for a while trying to figure it out.  It happened when I created a…

Read More