Deleting orphans with Entity Framework

It is common for a single parent entity to be related to many child entities. This relationship may be required or optional. A  required relationship means that the child cannot exist without a parent, and if the parent is deleted or the relationship between the child and the parent is severed, then the child becomes orphaned. In such situations it is often useful to have the orphaned child automatically deleted. Continue reading

Posted in Code First, Data Annotations, DbContext API, Entity Framework, Foreign Keys | Tagged , , , , , , , | Leave a comment

The key to AddOrUpdate

The DbSet.Find method provides an easy way to lookup an entity given its primary key. One place this can be useful is in an AddOrUpdate method when loading an entity which can then be updated with values from another application tier—for example, updating an entity with values from a client in a web application.

However, it isn’t so easy to do this in a generic way on any entity type without specific knowledge of which properties make up the primary key. This is something we will make easier in a future release of EF, but for now this blog post shows how to write some extension methods that make this process easier. Continue reading

Posted in Change Tracking, DbContext API, Entity Framework | Tagged , , , , , , , | 6 Comments

Code First model builder versions

Code First’s convention-based approach to building a model has some interesting implications for breaking changes between different versions of EF. This post describes those implications and shows how model builder versions can be used to deal with potential breaking changes while still retaining a forward-moving developer experience. Continue reading

Posted in Code First, Entity Framework | Tagged , , , , , , | 1 Comment

Code First Building Blocks

There are plenty of examples out there showing how to use DbContext to create a Code First model. But DbContext itself uses some fundamental Code First building blocks to do its stuff. This post shows how these building blocks can be used directly, which can be useful in situations where you need more control over how the model is created or cached. Continue reading

Posted in Code First, Entity Framework | Tagged , , , , , , | Leave a comment

Code First Data Annotations on non-public properties

The Entity Framework supports mapping to public, protected, internal, and private properties. However, there are a few restrictions when mapping to non-public properties. and there are also some things you need to know when attempting to configure such mappings using Code First. In particular, mapping to non-public properties cannot be done using data annotations alone.

Continue reading

Posted in Code First, Data Annotations, Entity Framework | Tagged , , | 4 Comments

EF5: Rejecting changes to a single property

You have probably heard about the high-profile features of EF5—things like enums, spatial types, TVF support, and the perf improvements. But there is one little feature you may not have heard about—the ability to reject changes to an individual property. That is, to reset the IsModified flag of a property such that the value of that property will not be sent to the database.

Continue reading

Posted in Change Tracking, DbContext API, Entity Framework | Tagged , , , , | Leave a comment

Secrets of DetectChanges Part 4: Binary properties and complex types

In parts 1, 2, and 3 of this series we looked at fairly normal, if occasionally advanced, uses of DetectChanges. In this post we’re going to look at some corner cases around complex types and binary properties. While these are corner cases its still worth knowing about them so they don’t catch you out if you ever run into them. Continue reading

Posted in Change Tracking, DbContext API, Entity Framework, Proxies | Tagged , , , , , , , , , , | 2 Comments