The current version of the EF4 Feature CTP includes yet another way to use Entity Framework: Code Only.
What does this mean?
Entity Framework was written to take advantage of an Entity Data Model. The model came first and then EF was created. The inner workings of EF depend on the XML of the conceptual model as well as the additional metadata that describes the database schema (SSDL) and the mappings(MSL) between the model and the database schema.
Various ways to approach EF:
- Many developers use the EDM Wizard to define a model from the database then let the code generator create classes and go from there. This was the out of the box solution for version 1 of EF.
- Some people customize those code generated classes with additional partial classes.
- You can also customize how the classes are generated. With EFv1 there was a proprietary API for this which was cumbersome to learn and use. With EF4, the code generation now depends on T4. T4 is much simpler and commonly used for various code gen tasks in Visual Studio.
- Starting with EF4, you can completely skip the code generation and use your own classes. As long as the classes line up with the model (entity name= class name, entity properties= class properties) then EF4 will do the rest. You do have to create an ObjectContext to serve up the entities for this to work. The context lets you write queries and benefit from change tracking, etc.
- And now, with the Feature CTP, there is Code Only.
Code Only
Code Only lets you skip the model completely. But how is that possible if, as I said above, the inner workings of EF depend on the XML to do its job? With Code Only, the needed XML is created on the fly at run time. Simple enough, but there’s much more to it.
Using the first round of the Feature CTP, the capabilities use the “convention over configuration” pattern, where EF relies on some assumptions to do its work. If the database exists, it assumes that your classes align with the database tables. If the database doesn’t exist, it will create it using what it learns from the classes.
At the end of one of the team’s intro blog posts on Code Only, a walkthrough, there is a list of “missing features”.
It is no surprise that the upcoming features in the next version, described in a new post, Code Only Enhancements, is almost a relisting of those missing features, now no longer missing.
Here is the original list of missing features with my own notes included.
- ComplexTypes are not supported.
This is the one bullet point that is not addressed at all in the enhancements blog post.
Update: via Twitter, Alex James confirmed that there are plans to support ComplexTypes. - Configurable Mapping is not supported.
We’ll get this capability. E.g. specify a table name when it doesn’t match a class name. - Table per Hierarchy is the only inheritance strategy supported.
TPT (per type) and TPC (per class) have been added. - Specifying Facets is not supported, so for example you can't specify the max length of a string column in the database. Instead defaults are used.
Supported - There is no Provider Model, so CodeOnly only supports SqlServer at this point.
In the comments of the new blog post, it is made clear that currently they are only supporting SQL Server. My take on this is that they are working things out against SQL Server and will then build the provider model on top of that further down the road. - CodeOnly provides no way to specify which properties participate in the same relationship but are simply the inverse of each other. So for example if you added a Category property to the Product class used in the example above the result would be 2 relationships between Product & Category, and consequently 2 FKs in the Products table, because the Entity Framework can't infer that Product.Category and Category.Products are simply the inverse of each other.
This is a biggie and will be supported in the next version of the CTP. - ManyToMany relationships are not supported.
Will be supported. “In the case of many to many relationships you must have a join table, so absent mapping information we produce a join table by convention.”
CTP is rapidly evolving
So the first look at the CTP was pretty early. We actually saw an even earlier stab at Code Only design at PDC08 but by the time we got the CTP it had changed dramatically.
There are many more details and lots to learn in the comment threads in the following posts:
EFDesign Blog:
Code Only June 10, 2009
Code Only Enhancements August 3, 2009
ADONET Team Blog:
Feature CTP Walkthrough: Code Only for the Entity Framework June 9, 2009




