Sie sind auf Seite 1von 18

8: Entity Framework - N-Tier Applications

Essential LINQ with the Entity Framework 4.0 © 2010 DevelopMentor 1/4/2010
8: Entity Framework - N-Tier Applications

Essential LINQ with the Entity Framework 4.0 © 2010 DevelopMentor 1/4/2010
8: Entity Framework - N-Tier Applications

Essential LINQ with the Entity Framework 4.0 © 2010 DevelopMentor 1/4/2010
8: Entity Framework - N-Tier Applications

Essential LINQ with the Entity Framework 4.0 © 2010 DevelopMentor 1/4/2010
8: Entity Framework - N-Tier Applications

Essential LINQ with the Entity Framework 4.0 © 2010 DevelopMentor 1/4/2010
8: Entity Framework - N-Tier Applications

Essential LINQ with the Entity Framework 4.0 © 2010 DevelopMentor 1/4/2010
8: Entity Framework - N-Tier Applications

In an n-tier application, the client to the DAL could be a WCF service.

Essential LINQ with the Entity Framework 4.0 © 2010 DevelopMentor 1/4/2010
8: Entity Framework - N-Tier Applications

* As of .NET 2.0 you can use project settings to store items in an config file associated with a class library.
However, ObjectContext’s default constructor attempts to retrieve the connection string from the config file
of the executing assembly (EXE). If you want to decouple the service layer from DAL, you probably should
use DAL’s project settings to store the connection string in the DAL’s config file, then supply it to the
ObjectContext constructor, like so:

using (NorthwindEntities ctx = new NorthwindEntities


(Settings.Default.NorthwindEntityConnection))
{ …

Essential LINQ with the Entity Framework 4.0 © 2010 DevelopMentor 1/4/2010
8: Entity Framework - N-Tier Applications

Essential LINQ with the Entity Framework 4.0 © 2010 DevelopMentor 1/4/2010
8: Entity Framework - N-Tier Applications

For an example of using Data Transfer Objects with Entity Framework 1.0, see Tony Sneed’s article on SOA
Data Access: http://msdn.microsoft.com/en-us/magazine/dd263098.aspx.

Essential LINQ with the Entity Framework 4.0 © 2010 DevelopMentor 1/4/2010
8: Entity Framework - N-Tier Applications

Danny Simmons, an architect on the EF team, wrote an article highlighting the differences between Data
Transfer Objects and Self-Tracking Entities: http://msdn.microsoft.com/en-us/magazine/ee321569.aspx.

Essential LINQ with the Entity Framework 4.0 © 2010 DevelopMentor 1/4/2010
8: Entity Framework - N-Tier Applications

Entities implement INotifyPropertyChanged both to facilitate change-tracking and to support data binding.
Collection properties use TrackableCollection<T>, which derives from ObservableCollection<T>, for the
same purpose. Entities also include code to handle relationship fix-up, which keeps both sides of an
association in synch when one of the sides changes.

Entities are usually placed in a separate assembly than the DAL assembly and are referenced both by the
DAL and the client. There are two T4 templates added for STE’s, one for the ObjectContext and another for
the entities themselves. Both can reside in the DAL assembly, but the entities T4 template can be added as a
link to the entities assembly. For more info see http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-
self-tracking-entities-for-the-entity-framework.aspx.

Essential LINQ with the Entity Framework 4.0 © 2010 DevelopMentor 1/4/2010
8: Entity Framework - N-Tier Applications

* The IsReference property on the DataContract attribute allows WCF to serialize bidirectional graphs with
cycles.

Essential LINQ with the Entity Framework 4.0 © 2010 DevelopMentor 1/4/2010
8: Entity Framework - N-Tier Applications

Essential LINQ with the Entity Framework 4.0 © 2010 DevelopMentor 1/4/2010
8: Entity Framework - N-Tier Applications

* For the CTP, ApplyChanges can be found in the ObjectContextExtensions class in the
Microsoft.Data.Entity.CTP assembly, which your service DAL should reference. You need to add a using
directive to the Microsoft.Data.Entity namespace in order to bring the extension method into scope.

AcceptChanges is a code-generated method in the ObjectChangeTracker class, located in the xxxTypes.cs


file. It sets ObjectState to Unchanged and removes change-tracking state such as OriginalValues and objects
added to or removed from collection properties.

Essential LINQ with the Entity Framework 4.0 © 2010 DevelopMentor 1/4/2010
8: Entity Framework - N-Tier Applications

Essential LINQ with the Entity Framework 4.0 © 2010 DevelopMentor 1/4/2010
8: Entity Framework - N-Tier Applications

Essential LINQ with the Entity Framework 4.0 © 2010 DevelopMentor 1/4/2010
8: Entity Framework - N-Tier Applications

Essential LINQ with the Entity Framework 4.0 © 2010 DevelopMentor 1/4/2010