Add/Attach and Entity States

This topic will cover how to add and attach entities to a context and how Entity Framework processes these during SaveChanges. Entity Framework takes care of tracking the state of entities while they are connected to a context, but in disconnected or N-Tier scenarios you can let EF know what state your entities should be in. The techniques shown in this topic apply equally to models created with Code First and the EF Designer.

Performance Considerations for Entity Framework 4, 5, and 6

The following topics are covered in this article:

  Read more of this post

Entity Framework and SQL Server View

To force entity framework to use a column as a primary key, use ISNULL.

To force entity framework not to use a column as a primary key, use NULLIF.

An easy way to apply this is to wrap the select statement of your view in another select.

Read more of this post

Cool (and Free) Tools for Entity Framework

Now that Entity Framework is open source, the development community is able to contribute code to it at But don’t limit yourself to looking there for tools and extensions. There are other great tools, both commercial and community-driven, that can help you achieve more with Entity Framework. In this column, I want to highlight some of those tools and extensions that come from the community. They’re all free and available through the Visual Studio Gallery (for IDE extensions) or NuGet (for code libraries). There are even some from members of the Entity Framework team that are pet projects and external to the official bits.

Read more of this post

Tips for Updating and Refactoring Your Entity Framework Code, Part 2

 Last month I discussed updating to EF6, using an old EF4 application as an example ( I also provided tips for breaking out smaller Entity Data Models from large models (EDMX or Code First), because I recommend working with multiple focused models rather than using one large model—laden with the complication of often unnecessary relationships—throughout your entire application.

This month, I’ll use a specific example to share more details about breaking out a small model, and then, using that small model, work through some of the problems you encounter when switching from the ObjectContext API to the newer DbContext API. 

Read more of this post

Tips for Updating and Refactoring Your Entity Framework Code

I have worked with many clients to help refactor existing software that uses Entity Framework (EF). In the case of EF code, refactoring can have a variety of meanings and often involve an update. In this column, I’ll take a look at some of the ways you might be overhauling your EF code, or applications that involve EF. For each of these approaches, I’ll provide some guidance—based on my experiences working with clients’ production applications—that should help you be well-prepared and avoid some headaches.

The changes I’ll discuss are:

  • Updating to a new version of Entity Framework
  • Breaking up a large Entity Data Model
  • Replacing the ObjectContext API with the DbContext API

Read more of this post

Profiling Database Activity in the Entity Framework


EF Designer Code Generation Templates

When you create a model using the Entity Framework Designer your classes and derived context are automatically generated for you. In addition to the default code generation we also provide a number of templates that can be used to customize the code that gets generated. These templates are provided as T4 Text Templates, allowing you to customize the templates if needed.

The code that gets generated by default depends on which version of Visual Studio you create your model in:

  • Models created in Visual Studio 2012 & 2013 will generate simple POCO entity classes and a context that derives from the simplified DbContext.
  • Models created in Visual Studio 2010 will generate entity classes that derive from EntityObject and a context that derives from ObjectContext.
    Note: In Visual Studio 2010 we recommend switching to the DbContext Generator template once you’ve added your model.

This page covers the available templates and then provides instructions for adding a template to your model.

Read more of this post

Understanding Your Language Choices for Developing Modern Apps – from Rachel Appel

Developing modern software using a single language just doesn’t happen in 2013. Programming has evolved to a polyglot model with domain-specific languages (DSLs) for each tier of development. For example, a popular scenario is to use SQL and C# on the back end and HTML, JavaScript and CSS as the UI languages. As a developer, you want to be able to add skills to your skill set, but working in too many languages spreads your skills thin in each. You also want to put products out to market quickly, which often leaves little room for learning a new language and all its quirks, so optimally you want to build on top of skills you already have. When creating Windows Store and Windows Phone apps, you have a variety of choices in languages for every project scenario, and at least one will nicely complement your experience. In this article, I’ll show you what languages go with different development scenarios. In addition, I’ll look at the options you need to weigh and how factors such as data access or porting an app affect the language you need to use.

Read more of this post