0

Assume the next data model scenario with two entities:

BusinessRule

  • Id : int
  • Name : string
  • Rule : string
  • RuleType: RuleType

RuleType

  • Id : int
  • Name : string
  • GroupingName : string

The model is mapped using Entity Framework 4.3 with a model first approach and a T4 POCO template, on an MVC 3 project.

Now, assume that in the "Create" method of a controller class you receive a BusinessRule instance, but you need to change the behavior depending on the GroupingName property associated with the RuleType of the business rule.

What is an optimal approach to obtain the value of GroupingName?

So far I've been thinking in something like mapping the RuleType property as eager loading, but to my surprisse (I've worked previously with nHibernate) it seems that it is not possible.

Is the best way to just query the datacontext for the RuleType (by Id) or is there a better way?

EDIT:

The solution I have so far is something like:

public ActionResult Create(BusinessRule businessrule)
{
    // It will be nice if I don't have to do this.
    // I will be much happier if something like businessrule.RuleType.GroupingName
    // would be possible
    RuleType businessRuleRuleType = db.RuleType.Where(rt => rt.Id == businessrule.RuleTypeId).Single();
    string businessRuleGroupingName = businessRuleRuleType.GroupingName; 
}

So, I hope that this makes clear that using Include (as far as I know) is not a option. Also I know that this solution works, but I'm not sure if it is the "best", so that's the qestion. If the answer is "No, there's no better way", OK, it's fine.

Manuel Navarro
  • 1,789
  • 2
  • 16
  • 18

1 Answers1

0

Eager loading definitely is possible with Entity Framework, using the .Include() extension.

In 4.0 this wasn't particularly elegant

MyObjectContext.BusinessRules.Where(br => br.Id = someId)
                         .Include("RuleType")
                         .FirstOrDefault();

In 4.1 and later this is now typed:

MyDbContext.BusinessRules.Where(br => br.Id = someId)
                         .Include(br => br.RuleType)
                         .FirstOrDefault();
StuartLC
  • 104,537
  • 17
  • 209
  • 285
  • Tanks for answering nonnb. I'm aware of the Include() method for eager load, but if you read the context of the escenario you will notice that you have an already created instance of a BusinessRule entity (MVC created it for you), so how can I tell MVC to "include" RuleType? – Manuel Navarro Sep 11 '12 at 16:02
  • @ManuelNavarro Do you mean that if the View passes some part (e.g. Id) of `BusinessRule` back to a controller method, that you want the ModelBinder to automagically eager load `BusinessRule` and `RuleType`? – StuartLC Sep 11 '12 at 16:06
  • Or do you mean that EF should have the ability to set a default level of Include on a per entity basis (something like .LoadsWith() on Linq2SQL). Unfortunately it seems it [doesn't](http://stackoverflow.com/questions/6042023/entity-framework-4-1-default-eager-loading) – StuartLC Sep 11 '12 at 16:11
  • :) sort of... I'm not sure, but I have the BusinessRule model associated to the view, so one way or the other MVC is creating an instance of BusinessRule from the Request data and binding that instance to de datacontext. The object of my question is to know if there is a way to tell MVC that when the instance is being created it "include" RuleType, and if there is no way to accomplish that, then how is the best way to do it?... sorry, for the long post... and thanks! – Manuel Navarro Sep 11 '12 at 16:17