Take the 2-minute tour ×

Ok, so let me explain a little of what I am trying to do.

I have a table called WebsitePage that contains all of the pages on my site with some information about them. Then, I have a Customer table for all of my customers. Then I have another table called CustomerWebsitePage that stores customer values for some of the columns in the WebsitePage table.

So, using the entity framework, I imported these three tables. What I want to be able to do is return a strongly typed WebsitePage list that has any values from CustomerWebsitePage if there are any values for it. So, for example, say one of my customers added a CustomerWebsitePageName for one of my website pages. I want to return a list of WebsitePages that contains the CustomerWebsitePageName instead of the WebsitePage Name in that case. But the original WebsitePage Name for everything else since it wasn't overridden.

The kicker here is that my WebsitePage table has a foreign key to itself for a Parent/Child relationship. So, I also want to return the child WebsitePages at the same time. I tried using a function import to get what I wanted, but then of course I lost the ChildPages.

I have tried just about everything to get what I want using the Entity framework and LINQ. But so far almost everything I try ends up with an exception being thrown. Here are a few:

  • The EntityCollection has already been initialized
  • The entity or complex type 'MyEntityModel.WebsitePage' cannot be constructed in a LINQ to Entities query.

I have one idea of how I can get around all this, and that would be to duplicate the ParentPageID into my WebsitePage table, but this really seems to violate a lot of principles and I really just don't want to add the maintenance headache related to this.

Anyone have any ideas how to accomplish this type of thing?

A simple DB diagram.

share|improve this question
It looks like you wrote a bad LINQ to Entities query, but without actually seeing that query and the code which uses it, it's hard to be sure. At first glance, your DB mapping looks OK as-is. –  Craig Stuntz Aug 17 '09 at 13:32

1 Answer 1

up vote 1 down vote accepted

The object that you need to return is a CustomerWebsitePage and not a WebsitePage. The reason is that whatever object you return must know about the customer since the property will need it to determine which field (CustomerPage or WebsitePage) to use.

Considering that, you can have a function CustomerWebsitePage.GetAllPagesForCustomer(Customer c) that would return an enumeration of pages. However, to achieve the functionality you are looking for, you must implement some read-through properties in the CustomerWebsitePage. Let's take the example of Name. Here would be how to implement it in CustomerWebsitePage:

public string Name
    get{ if( String.IsNullOrEmpty(CustomerWebsitePageName) ) 
            return WebsitePage.Name;
         return CustomerWebsitePageName; }

For the Children pages, you could have a property:

public IEnumerable<CustomerWebPage> Children
      return WebsitePage.Children.Select( it => it.Customer.CustomerID == this.CustomerID );       }

Note that with this setup you couldn't run EF Linq queries on these new fields (because they exist only in the objects themselves, not in the database mapping). You can pepper the code with Load() if you want to seamlessly load all the children, but it will cost you in performance. To ensure that all the children are loaded, try the following loading function:

IEnumerable<CusomterWebPage> GetAllPagesForCustomer(Customer c)  
    return Context.CustomerWebPageSet.Include("WebsitePage").Where( it => it.Customer.CustomerID == c.CustomerID );
share|improve this answer
Thanks for the advice. I have decided to go a different route altogether and not use the strongly typed object to do the overriding. Instead, I'm just handling it myself in the looping of this data. I guess I could say that this is a limitation of using the Entity framework when trying to do this sort of inheritance with overriding certain values. Who knows, I may even be able to use the entity framework to create this inheritance. But for now at least, I'm just handling it manually. Thanks. –  TehOne Aug 20 '09 at 16:30

Your Answer


