Jan 282011
 

A little late, but better late than never – the Entity Framework CTP 5 was released last month before Christmas, and now that I’m back, I’ll be doing a series of posts on Code First and CTP 5.  This is mainly to test out the new Object Validation which currently only is supported by Code First architectures.

Before we get into the nitty gritty behind the most recent CTP, why don’t we stop and take a quick look at where things are with the latest release.  First off, you’ll need .Net Framework 4 and a copy of Visual Studio 2010.  Next, grab the CTP from MSDN located here.

Let’s take a look at what is new in the fifth community technical preview (CTP) – from the release notes:

What is new?

This is the list of new and improved features in CTP5:

DbContext API

  • Model-First and Database-First support:
    New T4 templates is included for using DbContext/DbSet

  • Validation of objects on SaveChanges:
    Validation is based on the use of Data Annotations and currently only supported in Code First

  • Change tracking API:
    Allows you to access information and operations applicable to objects that are tracked by a DbContext

    • Access to Original, Current & Store Values

    • Entity state (i.e. Added, Unchanged, Modified, Deleted)

    • Explicit Loading: API to load the contents of a navigation property from the database

  • Databinding:
    DbSet.Local exposes an ObservableCollection representing the local contents of the DbSet. This is particularly useful when performing databinding in WPF applications. The new ToBindingList extension method can be used to obtain a binding list for Windows Forms applications.

  • No-Tracking Queries:
    This can be achieved via the AsNoTracking extension method on IQueryable<T>.

  • DbContext Configuration:
    Allows the following options to be configured for a DbContext instance:

    • Lazy Loading

    • Validate On Save

    • Auto Detect Changes

  • Raw SQL Query/Command execution:
    Allows raw SQL queries and commands to be executed via the SqlQuery & SqlCommand methods on DbContext.Database. The results can optionally be materialized into object instances that are tracked by the DbContext via the SqlQuery method on DbSet.

  • Improved concurrency conflict resolution:
    We have added better exception messages that allow access to the affected object instance and the ability to resolve the conflict using current, original and database values

  • DbContext.ObjectContext has moved:
    Rather than being a protected member we have made the underlying ObjectContext available via an explicitly implemented interface, this allows external components to make use of the underlying context. Getting the context now looks like: ((IObjectContextAdapter)myContext).ObjectContext

  • Excluding EdmMetadata Table:
    If Code First is generating your database and you wish to exclude the EdmMetadata table, this is now done by removing a convention (note that you do no longer need to do this when mapping to an existing database).

  • Types renamed in System.Data.Entity.Database namespace:

CTP4 Name

CTP5 Name

Database

DbDatabase

RecreateDatabaseIfModelChanges

DropCreateDatabaseIfModelChanges

AlwaysRecreateDatabase

DropCreateDatabaseAlways

Code First

  • Better Code First to Existing Database Support:
    CTP5 removes the need to switch off Database Initializers when working with existing databases with Code First. If you map to an existing database that Code First did not create then it will just ‘trust you’

  • Support for additional Data Annotation attributes:
    The full list of supported attributes now encompasses:

    • KeyAttribute

    • StringLengthAttribute

    • MaxLengthAttribute

    • ConcurrencyCheckAttribute

    • RequiredAttribute

    • TimestampAttribute

    • ComplexTypeAttribute

    • ColumnAttribute (placed on a property to specify the column name, ordinal & data type)

    • TableAttribute (placed on a class to specify the table name and schema)

    • InversePropertyAttribute (placed on a navigation property to specify the property that represents the other end of a relationship)

    • ForeignKeyAttribute (placed on a navigation property to specify the property that represents the foreign key of the relationship)

    • DatabaseGeneratedAttribute (placed on a property to specify how the database generates a value for the property, i.e. Identity, Computed or None)

    • NotMappedAttribute: Placed on a property or class to exclude it from the database

  • Fluent API Improvements:

    • Simplified table and column mapping

    • Ability to ignore classes & properties

  • Pluggable Conventions:
    Based on the large amount of feedback requesting this feature we have included an early preview in CTP5. We still have some work to do on this feature but we wanted to give you the chance to provide feedback before we RTM. We’ll provide more details on this feature in the coming weeks.

Other changes

  • New Assembly Name:
    Our assembly name has changed to EntityFramework.dll

There are a bunch of exciting new changes here, most notably around DataContext and Object binding properties.  I’m going to put together a few samples using Code First over the next week or so to show you how these new changes come into play in a real world scenario.

Whilst some of this functionality is new or improved, you might notice a lot of naming changes – it is quite clear that there is a great deal of refactoring going on under the covers including renaming of the assembly file!  This would seem to indicate a number of naming changes which will create significant code changes when porting from EFv4 to EFv5 in the future.

I’ll try to keep an eye on the most widespread changes as EFv5 CTPs are evolving.  Please keep in mind that the CTP releases are not sacrosanct, and sometimes the refactoring is reversed in later releases, so don’t treat it as set in stone.. yet.

Jul 302010
 

Just a quick word to point out that the most recent version of the ADO.NET Entity Framework Feature Community Technology Preview 4 was released recently (on the 13th of July) and is worth a look if you are using the Entity Framework in any meaningful fashion.

Check back soon, we’ll have some notes on the good, the bad and the mischievous features of this CTP.

http://www.microsoft.com/downloads/details.aspx?familyid=4E094902-AEFF-4EE2-A12D-5881D4B0DD3E&displaylang=en

You can also check out news from the team blog

http://blogs.msdn.com/b/adonet/archive/2010/07/14/ctp4announcement.aspx

Some notes lifted from the official announcement:

Productivity Improvements

We recently posted here about a set of productivity improvements we have been working on to simplify the process of writing data access code with the Entity Framework. CTP4 includes the core API surface and functionality that is described in the post. We’d love to hear your feedback on this simplified experience, be sure to check out the walkthrough.

Code First

Updates and enhancements to Code First include:

  • Moved to the System.Data.Entity.ModelConfiguration namespace

  • Refactored ContextBuilder into ModelBuilder and DbModel
    ModelBuilder is a high level component that lets you tweak a model, ModelBuilder then produces an immutable DbModel that can be used to construct a context. In the future DbModel will become a fundamental component of the EF stack as we look at other ways to build models beyond ModelBuilder.

  • More Model Discovery Conventions
    We posted details about the complete set of conventions we plan to support
    here
    In CTP4 we have implemented the following conventions:

    • Primary Key

    • Relationship Inverse

    • Foreign Key

    • Pluralization of Table Names

  • Support for Data Annotations
    We posted details about all the Data Annotations we plan to support
    here
    The annotations supported in CTP4 are:

    • Key

    • StringLength

    • ConcurrencyCheck

    • Required

    • Timestamp

    • DataMember

    • RelatedTo

    • MaxLength

    • StoreGenerated

  • Parameterless overload of MapSingleType()
    This allows you to configure table names without having to explicitly map every property

  • Improved Relationship Configuration API
    We’ve made some changes to the Fluent API methods used to configure relationships to make the API more explicit and intuitive. These changes are covered in the
    Code First Walkthrough .

Road Map

We are still working through the best ship vehicle to deliver a go-live release for these features. Your feedback has had a big impact on the Code First feature so far and we want to be careful not to lock down the API too quickly. That said we are getting consistent asks for a go-live for Code First and we are working to get to an RTM in the earliest feasible ship vehicle.