Designer Support for One-Way navigations in Entity Framework 4

In EF v1, it is possible to have one way navigations where you have an association between two entities but a navigation in only one of them. But the designer didn’t support this. If you deleted a navigation, the association was automatically removed.

You could, however, go into the XML and remove the navigation property manually, leaving the association, association mappings and the partner navigation property in tact.

The new designer now supports one way relationships – both for models with foreign keys and models without foreign keys.

Here is a model that does not use foreign keys. The associations, by the way, are called “independent associations” when there is no foreign key involved.

I start with a Customers property in the CustomerType entity and a CustomerType property in the Customer entity.

TwoWayA

Delete CustomerType.Customers and association and Customer.CustomerType property are still there.

twoWayB 

Next, I delete the CustomerType.Customer navigation property but the association/relationship still remains in tact. The navigations are no longer exposed in the classes but you the model is still valid as are the generated classes (I’m using the default code gen).

TwoWayC

If you want the association to be gone, you have to explicitly delete it. But for this case, you could just delete the association to begin with and the navigations will automatically be removed. That is the same behavior as in EFv1.

Although the screenshots are from a model with independent associations, the behavior is the same for foreign key associations.

Why the “no-way” association? Either there is some unknown-to-me value to having the association defined in the metadata, or it is the only way the designer is able to pull off keeping the association intact as a result of removing a navigation.

I have asked about this last bit and will share what I learn.

In the meantime, now that it is more discoverable and simpler to create one way relationships, I bet more people will take advantage of it.

  Sign up for my newsletter so you don't miss my conference & Pluralsight course announcements!  

4 thoughts on “Designer Support for One-Way navigations in Entity Framework 4

  1. Thanks!

    I’ve been wondering how to handle "types" like your CustomerType, having a CustomerType.Customers just feels wrong. Now with one-way navigation, having types as entities in the model feels much better.

    I wonder if there’s a typo in the entry though, i think "Next, I delete the CustomerType.Customer" should be "Next, I delete the Customer.CustomerType". Am I right?

    Cheers

    /Fredrik

  2. "Why the “no-way” association?"

    I think it can be useful when you have a 1 to something association for the INSERT. Like this, you will have an exception before sending TSQL to the DB if you forget to also add the associated entity.

    Matthieu

Leave a Reply to Indonesia Furniture Handicraft Wholesale Marketplace Cancel reply

Your email address will not be published. Required fields are marked *

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