Since EF4 and Visual Studio 2010 you have been able to use T4 templates to generate code from models created using the EF Designer. Starting with EF5 and Visual Studio 2012 T4 based code generation is the default for new models created using the EF Designer. When you create a new model using VS2012 or later … Continue reading Running EF T4 Code Generation Templates from Command Line
Author: romiller.com
EF6: Switching Identity On/Off with a Custom Migration Operation
Here is the scenario, I have a class in my Code First model that has a primary key configured as an identity column. I want to change it so that I can generate values in my code. Alternatively I may want to change an existing column - that my code is generating values for - … Continue reading EF6: Switching Identity On/Off with a Custom Migration Operation
Processor named ‘T4VSHost’ could not be found for the directive named ‘CleanupBehavior’
There are two scenarios in which you may see the 'Processor named ‘T4VSHost’ could not be found for the directive named ‘CleanupBehavior’' error after installing Visual Studio 2012 Update 1. Inside Visual Studio when code is being generated from an EF model - this isn't supposed to happen and something probably went wrong during install. … Continue reading Processor named ‘T4VSHost’ could not be found for the directive named ‘CleanupBehavior’
EF6: Writing Your Own Code First Migration Operations
Migrations provides a set of strongly typed APIs for performing common operation, for example CreateIndex("dbo.Blogs", "Url"). We also provide a method that allows you to run arbitrary SQL when you want to do something that isn’t supported by the API, for example Sql("GRANT SELECT ON dbo.Blogs to guest"). There are some disadvantages to the Sql … Continue reading EF6: Writing Your Own Code First Migration Operations
Extending And Customizing Code First Models – Part 2 of 2
Here is the scenario, your company ships a library or application that contains a Code First model for accessing the database. Your customers want to extend this model to include extra types/properties to meet their specific business needs. These types/properties will be stored in additional tables/columns in the application database. In Part 1 we looked at … Continue reading Extending And Customizing Code First Models – Part 2 of 2
Extending and Customizing Code First Models – Part 1 of 2
Here is the scenario, your company ships a library or application that contains a Code First model for accessing the database. Your customers want to extend this model to include extra types/properties to meet their specific business needs. These types/properties will be stored in additional tables/columns in the application database. In this post I’ll walk … Continue reading Extending and Customizing Code First Models – Part 1 of 2
EF6 Code First: Configuring Unmapped Base Types
If your object model contains inheritance, Code First gives you two options for the base type – it can either be mapped or unmapped. A mapped base type means the inheritance hierarchy is represented in the database using either the TPH, TPT or TPC pattern. An unmapped base type means Code First effectively ignores your … Continue reading EF6 Code First: Configuring Unmapped Base Types
EF6 Code First: Mapping All Private Properties Using Custom Conventions
A while back I blogged about mapping to private properties with Code First. The approach shown in that post works with EF4.1 onwards but it requires you to explicitly configure every private property that you want included in your model. That’s going to get tiresome if you have a big model and want all private … Continue reading EF6 Code First: Mapping All Private Properties Using Custom Conventions
Code First Migrations – Customizing Scaffolded Code
A while back I blogged about customizing the SQL that is generated by Code First Migrations. In this post I’m going to step back up the pipeline a bit and look at customizing the code that gets scaffolded when you call Add-Migration. When we built Code First Migrations we made an effort to make everything … Continue reading Code First Migrations – Customizing Scaffolded Code
//build/ 2012 Talk Video and Source Code
This year at the //build/ conference I presented a session titled ‘Building data centric applications for web, desktop and mobile using EF5’. In addition to EF5, I also covered the what and when of EF6 and did a quick demo of my favorite EF6 feature (custom code first conventions). You can view the session recording, … Continue reading //build/ 2012 Talk Video and Source Code
Mapping to Private Properties with Code First
By default Code First will just include public properties in your model, but there may be times that you want to include private properties. Arthur Vickers wrote a great detailed post on working with private properties so I'm just going to cover enough to get you started in this post. For example, the following model … Continue reading Mapping to Private Properties with Code First
Code First Stored Procedures with Multiple Results
One of the most common request we get for Code First is to natively support stored procedures (sprocs). That item is right at the top of our backlog now – but it didn’t make it into EF5. While using sprocs for insert, update and delete really isn’t possible with Code First yet, you can use … Continue reading Code First Stored Procedures with Multiple Results
Customizing ‘Reverse Engineer Code First’ in the EF Power Tools
Beta 2 of the Entity Framework Power Tools was recently released. By far the most popular feature out of these power tools is the ability to reverse engineer a Code First model from an existing database. Admittedly ‘Code First’ wasn’t the best choice of names… Code First is really just a code-based alternate to the … Continue reading Customizing ‘Reverse Engineer Code First’ in the EF Power Tools
What tables are in my EF model? And my Database?
Today I was writing some code that needed to know what tables are present in an EF model… and I realized I’ve written this code multiple times and never remember all the little nuances of it… just in case you ever need to do the same I thought I’d turn it into a blog post … Continue reading What tables are in my EF model? And my Database?
Dynamically Building a Model with Code First
I’ve answered a few emails recently on this topic so it felt like time to turn it into a blog post. In this scenario the set of classes that make up your model isn’t known at compile time and is discovered dynamically at runtime. An example of such a scenario is a WordPress/Orchard/etc. style website … Continue reading Dynamically Building a Model with Code First
Testing With a Fake DbContext
One of the most popular posts on my blog has been “EF CTP4 Tips & Tricks: Testing With Fake DbContext”. That post was built on a pre-release version of Entity Framework so I thought I’d provide an updated post on top of the DbContext API surface we ended up shipping. In this post we are … Continue reading Testing With a Fake DbContext
Running & Scripting Migrations from Code
Code First Migrations is included as part of Entity Framework starting with the EF 4.3 release. Migrations are normally created and run from the Package Manager Console in Visual Studio. These commands are just thin wrappers over public APIs that you can call directly from your own code. In this post you’ll see how to … Continue reading Running & Scripting Migrations from Code
Customizing Code First Migrations Provider
Code First Migrations is included as part of Entity Framework starting with the EF 4.3 release. One feature we haven’t really blogged about yet is the ability to include additional arguments in a migration operation. These arguments are then made available to the SQL generator. You can then derive from the SQL generators that we … Continue reading Customizing Code First Migrations Provider
Magic Free Code First Migrations
This post is now out of date. Be sure to check out EF 4.3 Beta 1. <transparency>I work for Microsoft</transparency> (just kidding… but I couldn’t leave the tag open) We recently released a very early preview of Code First Migrations and it’s been getting some mixed feedback, the negative feedback falls into three buckets: Stuff … Continue reading Magic Free Code First Migrations
EF 4.1 Multi-Tenant with Code First
After a long break while we got EF 4.1 (Magic Unicorn Edition) polished up and shipped it felt like time to get some content up on here again. We’ve released some good introductory content for EF 4.1 on the MSDN Data Developer Center so I thought I’d dive into something a little more advanced. The … Continue reading EF 4.1 Multi-Tenant with Code First