Interview with Julie Lerman on Programming Entity Framework: Code First
Last week, the dotNetMontreal community hosted a seminar by Julie Lerman on the Entity Framework Code First approach. Code First is essentially a way to describe a model for Entity Framework using code plus configuration without being forced to use a designer.
Entity Framework enables developers to create data access applications by programming against a conceptual application model instead of programming directly against a relational storage schema. Entity Framework is built on top of the .Net Framework and as of version 4.2, it is available as a download exclusively through the open source Visual Studio package management system NuGet.
InfoQ spoke with Julie to discuss her newly released book co-authored with Rowan Miller: Programming Entity Framework: Code First published by O’Reilly.
InfoQ: Tell us about your newest book, Programming Entity Framework: Code First, which recently came out last November?
Julie Lerman: It’s essentially an extension to what I now refer to as my “Big Book” which is, Programming Entity Framework 2nd Edition. It’s about a 900-page book that really goes in-depth on the whole core of the Entity Framework with lots of coding samples. So, rather than rewriting the whole book just to talk about Code First and DbContext, I decided to write shorter books just about these new pieces with my co-author Rowan Miller.
The concept of the shorter books fell into place with something that O’Reilly is doing called “real-time books”. Their idea is to have short books that are released quickly. So, we decided to focus on just the Code First part, not even application writing, but just really dive into all of the different kinds of modeling and mapping you can do with Code First and how the database initialization goes. That book came out to 192 pages, which was still a lot, and would’ve made the “Big Book” just a little too big.
So, there’s Code First, that one’s done, and there’s the whole DbContext API. Rowan Miller is program manager on the Entity Framework team at Microsoft and worked on Code First; He’s my co-author on both books.
We are now working on the DbContext book and basically we have one chapter left to write which is my chapter; it will be the advanced chapter. This book really goes in-depth on DbContext and once it gets through its technical reviews and edits, it should be released in just about another month.
Finally, we’ll do a book on solutions. This one is going to be on using Code First and DbContext in an MVC app or here’s a pattern of using it in a WCF data service. It won’t be a recipes book, but it’ll just be some solutions presented with our guidance, lots of examples and practices that we recommend.
InfoQ: What’s the biggest benefit of using Code First over other development styles such as database first or model first?
Julie Lerman: One of the things that’s really super about Code First, especially for people who don’t want to use a (database) designer, is that they can just use their code. With the designer, you are really dependent on the tool to keep your model in place and you really just want to use code generation to create your classes from that. There is so much you can put in there, which is really about the schema of your classes. Then, you have to use partial classes to add additional logic. This approach makes you really reliant on what is available in the designer to provide what you need in the classes. So, the designer is great for people who want to use a designer and Code First, in essence, is just another way of creating a model.
Code First is great if, for example, you already have classes and you just want to use those. Because then, you can just layer the Fluent API configuration on top of that to make sure that your classes map to the database. Or, if you are starting with something new, you might just want to use classes and be done with it.
InfoQ: Are there any pitfalls to using the Code First approach?
Julie Lerman: On a high level, there are a few things that you can do in a designer that are supported in the model itself that you can’t achieve yet with Code First. One of the biggest things for Code First is that you can’t map stored procedures to a model. However, Entity Framework does support such mapping. In the entity data model XML file (EDMX), you can map stored procedures such as add, update, and delete to an entity and then, with those in place, Entity Framework will use them for modifications rather than creating its own commands.
InfoQ: You briefly mentioned the Fluent API during your presentation; can you tell us more on how a developer would take advantage of this within the context of the Code First approach?
Julie Lerman: I think people are more familiar with using data annotations to configure the classes. It lets Code First know how to map the classes to the database in a scenario where the defaults or the Code First conventions don’t figure out the way the developer meant it to be. People are seeing a lot of examples of the data annotations because it is so easy to just throw them in your classes. The data annotations that are specific to the mappings, but not the attributes that are specific to the data validation, those actually rely on the underlying Fluent API.
Basically, the Fluent API lets you apply configuration that lets you give more information to Code First about how you intend for it to map your classes to your database. By doing it “fluently”, you are actually building those instructions into the context rather than applying them directly to you classes. So, your classes are still “clean classes” and by coding-in the configuration with the Fluent API, you keep all of that inside the data layer specific code.
The other thing that is important to understand about the Fluent API is that it represents the universe of all of the configurations that you can do to “fix” Code First’s interpretation of the mapping, to the extend that Code First and Entity Framework support, whereas the data annotations are a subset.
InfoQ: Can the Code First approach be used to produce enterprise-level applications?
Julie Lerman: With first iteration of the Entity Framework, I think some people were a little iffy about that however now, with the latest release of Entity Framework 4.2, you can absolutely do enterprise-level architectures and applications using Code First and DbContext.
The first version of Entity Framework 1.0, that was part of Visual Studio 2008 and .Net 3.5, definitely had limitations for doing really more of an “agile style” architecture, unit testing, repositories and things like that. This was because the way Entity Framework worked with the classes; it was heavily dependent on a class called EntityObject, which was part of the framework. That iteration was much better for people who were moving from database tables and data readers. Although not all of those people will agree because they lost something that was important to a lot of people which was the classes with the data tables able to automatically keep track of its changes. There were a lot of problems and they’re still are.
With the second iteration of the Entity Framework, which was called EF 4 that came with .Net 4 and Visual Studio 2010, they added the POCO (Plain Old Clr Object) support. Now we have the ability to build repositories, units of work and unit testing, not just integration testing. It has gotten easier with Code First and DbContext that are sitting on top of Entity Framework 4. It’s easier because they’ve pulled into their own API some of the things we were doing extra work to do with Entity Framework before to achieve these kinds of architectures.
InfoQ: How can Code First be used to deploy an application on Microsoft’s Cloud Platform using Windows Azure and Sql Azure?
Julie Lerman: Well, you can use Code First with Sql Azure even if you want to use it for the sake of creating the database on Sql Azure. You can run Code First in Visual Studio on your computer and point to an Sql Azure database. You can have all of that interaction for dropping and creating that you might want to do during development if you’re working with a new database. That works really nicely.
With Windows Azure, it basically has .NET in it, so you can write your applications and deploy them right up into the cloud. Typically, I’m using Windows Azure to host WCF services that use Entity Framework as well as WCF data services. So, I’ve got both of those hosted in Windows Azure using Sql Azure. If you are using an application locally and an Sql Azure database, you got that latency as you are making the calls to the database. This can be a problem for people using lazy loading.
The combination of Windows Azure and Sql Azure in the cloud with Entity Framework is totally supported and the way everything’s going.
InfoQ: What’s the main takeaway for developers about Code First and DbContext?
Julie Lerman: You can use the DbContext no matter how you create your model, whether you create it with Code First or with Database First or Model First. I think the big takeaway for Code First is that it’s just another way to get to a model for Entity Framework. You now have three ways to create a model. Once you’ve got the model, regardless of how you’ve created it, then the rest is just about the same; You are using Entity Framework.
And then back to the DbContext, an important thing to understand about DbContext, is that it really should be your first choice. If you’re starting a new application with Entity Framework you should use the DbContext. If you’ve got an application that’s using the ObjectContext then keep using it. It’s still there. It’s part of the core API, but if you’re starting a new application, use DbContext because the Entity Framework team are making that so much more accessible than working down in the core with ObjectContext.
InfoQ: Finally, what are your plans in the future after you finish working on the upcoming books?
Julie Lerman: I’m really eager to get back to doing the videos for Pluralsight. I’ve really enjoyed doing those and people have said they’ve been very helpful. They’re a lot of work and it’s hard to do those while I’m also doing the books so, as soon as I get the books out of the way, I’m going to get back to doing those. There is also a whole lot of content I’ve already done on the MSDN Data Developers Center where you can find short videos and articles Code First and DbContext.
About Julie Lerman
Julie Lerman is the leading independent authority on the Entity Framework and has been using and teaching the technology since its inception in 2006. Julie is the author of the highly acclaimed "Programming Entity Framework (with a 2nd edition released in August 2010). She is well known in the .NET community as a Microsoft MVP, ASPInsider and INETA Speaker. She is a prolific blogger, a frequent presenter at technical conferences large and small around the world, including TechEd and DevConnections and she writes articles for many well-known technical publications, including authoring the Data Points column in MSDN Magazine. Julie has also been busy creating training videos for MSDN and Pluralsight.
Julie lives in Vermont where she has run the Vermont.NET User Group since 2002 and was a founding board member of the Vermont Software Developers Alliance. You can read her blog and her tweets at twitter.com/julielerman.
Doc List Oct 25, 2014