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.