Search This Blog

My Entity Framework Cheat Sheet

This is a list of tools I use to get generated code and get work done quickly with Entity Framework.

Generate SQL/Database from a DbContext

Tip: If you are doing this a second time to rebuild your database (after requirements changes), you can...

... unload your Web or Api, Services, and Tests projects and just have your DbModels and DbContext projects open. That way you can rebuild the solution easily targetting just your Entity Framework related code. (Assuming you may have code referencing your DbModels which are changing).

... or if you are rebuilding only a subset of new tables, run a "Reset" migration first using this trick to omit the tables you are working on. Then remove the Ignore methods and run a second migration for those tables in question.

For the entities, create a models directory and create classes in it.

If your primary key does not follow convention name, use the [Key] attribute.

Create navigation properties for many to one relationships; create a property for the ID and the object.

If your nav prop ID does not follow convention, use the [ForeignKey] attribute on the object.