Microsoft ADO.NET Entity Framework Feature CTP 5

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.

Leave a comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.