views:

17

answers:

1

I've got an ASP.NET MVC app that uses Linq to Sql for data access.

Say I have two objects: An Order object that has a foreign key to a Customer object by CustomerID. So, in my Order class, you would see two properties: an int CustomerID field, and an EntityRef member accessible by a Customer property.

When the edits or submits an Order, my MVC app will update the CustomerID field directly of the Order class, instead of updating the Customer property. This saves us from having to fetch a customer record, and I can use the default model binding code to fill the property automatically as long as the submitted form request has a customerID entry.

This works ok, however, later on in some other part of the code--say a business rules portion, some logic will access the Customer property of the Order object. For example:

if (order.Customer.HasPreviousOrders) then ...

Even though the CustomerID field is set, the Customer field is null, so this business rule throws an exception.

I know Linq 2 Sql uses EntityRefs to do delayed loading. My question is: is there a way to trigger the delayed loading on an object's EntityRef if the ID field has been modified?

We have a dynamic rules engine, so I don't have control of what foreign key objects are going to be needed. I'd rather not have to go through all my controllers to set the EntityRef<> values directly.

Thanks for the help.

A: 

Ok, no takers. It looks like what I'm trying to do is just not doable--or maybe not a good idea.

I went ahead and implemented code so I am setting the association object property instead of the ID property so the business rules can be processed.

Linus