Category Archives: Code First

EF 6.1: Creating indexes with IndexAttribute

Since EF 4.3 it has been possible to use CreateIndex and DropIndex in Code First Migrations to create and drop indexes. However this had to be done manually by editing the migration because the index was not included anywhere in … Continue reading

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

Database initializer and Migrations Seed methods

Entity Framework contains two different methods both called Seed that do similar things but behave slightly differently. The first was introduced in EF 4.1 and works with database initializers. The second was introduced in EF 4.3 as part of Code … Continue reading

Posted in Code First, Code First Migrations, Database Initializers, DbContext API, Entity Framework | Tagged , , , , , , , , | 6 Comments

EF6: Nested types and more for Code First

Ever since POCO support was introduced in EF4 there have been two limiting restrictions on the types that can be mapped: Types nested inside other types were not supported Types were matched by simple names only Some recent check-ins to … Continue reading

Posted in Code First, Entity Framework | Tagged , , | 2 Comments

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 … Continue reading

Posted in Code First, Data Annotations, DbContext API, Entity Framework, Foreign Keys | Tagged , , , , , , , | 31 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 … 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 … Continue reading

Posted in Code First, Entity Framework | Tagged , , , , , , | 10 Comments

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 … Continue reading

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

Code First Migrations: Making __MigrationHistory not a system table

Code First Migrations uses a table called __MigrationHistory as a place to store metadata about the migrations that have been applied to the database. Code First creates this table when it creates a database or when migrations are enabled. In … Continue reading

Posted in Code First, Code First Migrations, Entity Framework | Tagged , , , , | 16 Comments

Don’t use Code First by mistake

EF Code First is great and I use it all the time, even when mapping to existing databases. However, if your intention is to use a Database First flow, then it’s important that you don’t start to use Code First … Continue reading

Posted in Code First, Database First, DbContext API, Entity Framework | Tagged , , , , , , , , | 37 Comments