Back to EF FAQs Table of Contents


Does the Entity Framework support lazy loading and deep loading, and how is the performance for these features?

The EF supports both lazy loading and deep loading, and the team has done work to optimize performance. When you create a query it will, by default, use lazy loading. You can, however, opt in on a per-query basis for eager loading of one or more relationships. The performance for these features depends on your particular usage scenario. For more information about patterns for loading related entities and performance considerations, see Loading Related Objects.



Is it possible to make the EF implicitly load related entities? Is lazy loading enabled by default?

In EF 4, lazy loading is available and is enabled by default in the EntityObjects generated classes. Lazy loading is off in STEs.

In EF 3.5 SP1, there is also a way to implement lazy loading, but it requires some work on your part. Check out this series of blog posts:


Is there a topic that compares foreign key association and independent association?

The Defining and Managing Relationships topic on MSDN talks about the differences between these types of associations.


How do I change independent associations to foreign key associations?

There is no automatic way of changing independent associations to foreign key associations. You can do it by hand without regenerating the models. You can go to each entity, add and map the foreign key property, and then add a referential constraint to the relationship. If you want to automate it, you need to write a program that directly modifies the XML in the EDMX file.

For more information, see


Are changes to relationships in my objects always synchronized in the state manager?

When you make changes to navigation properties on your objects, the Entity Framework will synchronize its state manager for certain types of eligible objects:

For more information, see Defining and Managing Relationships.


Can I load related objects on a detached entity?

If the entity was created as a result of a NoTracking query, you can load related data by using either lazy loading or the Load method. If, however, you explicitly detach an entity from the context, calling Load will cause an exception to be thrown, and lazy loading will not load related entities.


What are different ways to create and modify relationships?

In the Entity Framework, you can create and modify relationships in several ways:
  1. By assigning a new object to a navigation property. The following code creates a relationship between an order and the customer. If the objects are attached to the ObjectContext, the order is added to the customer.Orders collection, and the corresponding foreign key property on the order object is set.        

    order.Customer = newCustomer
  2. By deleting or adding an object in an entity collection. For example, you can use the Add method to add an object of type Order to the customer.Orders collection or the Remove method to remove an object from the collection (Remove deletes only the relationship, not the object). This operation creates/removes a relationship between a particular order and customer. If the objects are attached to the object context, the customer reference and the foreign key property on the order object will be set to the appropriate customer:         

  3. In foreign key associations, you can assign a new value to a foreign key property, as in the following example. If the reference is in the added state, the reference navigation property will not be synchronized with the key values of a new object until SaveChanges is called. Synchronization does not occur because the object context does not contain permanent keys for added objects until they are saved. For more information, see Working with Entity Keys (Entity Framework). If you must have new objects fully synchronized as soon as you set the relationship, use one of the previous two methods to create the relationship.         

    order.CustomerID = newCustomer.CustomerID
    order.CustomerID = null 
  4. By creating the entity key for a specific object. If the object with this key already exists in the object context, the CreateEntityKey method will return the EntityKey of the existing object. This method is provided for backward compatibility with EF 3.5 SP1.         

    order.CustomerReference.EntityKey = ctx.CreateEntityKey("EntitySetName", newObject)


What is the difference between the Load method and the LoadProperty method?

ObjectContext's LoadProperty method is new in the Entity Framework 4. The method loads related objects, the same as EntityCollection.Load or EntityReference.Load. You can use the LoadProperty method with any entity type. However, POCO only works with LoadProperty. For more information, see Loading Related Objects.



When I call LoadProperty with an overload that takes a lambda expression in Visual Basic, I get the "The selector expression for LoadProperty must be a MemberAccess for the property" error. Is this a known issue?

This is an issue with the way the EF parses the expression in Visual Basic. The team has said it will work on a fix. For now the workaround is to use the overload of the LoadProperty method that takes a string parameter.



How do I get notified when an item is added to the collection?

The AssociationChanged event is raised when the association is changed or modified. Because the association changes whenever you add an object to the collection, you can register for the AssociationChanged event. For more information, see How to: Execute Business Logic During Association Changes.



Does the Entity Framework support associations with payload?

An association with payload refers to a many-to-many relationship, where the link table contains additional columns other than foreign keys. The Entity Framework does not support associations with payload. In the following blogs Alex explains the problem and possible workaround:



Why don't navigation properties raise the PropertyChanged event?

If you look at the generated code for your entity, you will see that simple properties have a call to ReportPropertyChanged("PropertyName"), and this is where the OnPropertyChanged method is called to raise the event. Navigation properties don't call that method. The following Connect bug documents the issue:

There are two ways to get around this: (1) Wrap the navigation property with your own property in the partial class where you can fire the event or perform other actions in OnPropertyChanged, and use that wrapped property for scenarios where you need the extra logic, or (2) Modify the T4 template that produces the generated code and add what you want to. To do this you would open the EDMX file in the designer, right-click a blank space on the design surface, and choose `Add Code Generation Item`. This will add a copy of the T4 template to your project, and you can modify it so it will include what you want in the property. For example, find the following code in the generated .tt file:

         ((IEntityWithRelationships) this).RelationshipManager
             .Value = value;

You can add your code there as well. More information about the templates can be found here:


Is there a way to access navigation properties that point to deleted objects within the SavingChanges event or SaveChanges virtual method?

When you mark an object for deletion, the following happens: if the deleted object is a reference, the navigation property of the related object will be set to null; if the deleted object is part of the collection, the object is deleted from the collection. The deleted object is not deleted from the object context and if you know its ID, you could find the object in the ObjectStateEntry by using the GetObjectStateEntry method. If you don't know the object's ID, you could register for the ObjectStateManagerChanged event and perform validation when the state of the object changes to Deleted. For example:

    static void StateChanged(object sender, CollectionChangeEventArgs e)
         if (e.Action == CollectionChangeAction.Remove)
             var category = e.Element as Category;
             if (category != null && category.Products.Count() > 0)
                 throw new ApplicationException("Cannot delete category because it has products.");

Back to EF FAQs Table of Contents