Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EntityFramework 6 RC1 Include on Many-to-Many property fails

I have a many-to-many relationship between Agents and AgentGroups (psuedocode, abbreviated).

public class Agent { 
    public virtual List<AgentGroup> AgentGroups { get; set; } 
}

public class AgentGroup { 
    public virtual List<Agent> Agents { get; set; } 
}

At some point in the code, I want to get all AgentGroups, and I want to prefetch/include the Agents for each group. I also want to pre-fill the AgentGroups collection on the Agents. This was working in EF 6 beta, but no longer works in EF 6 rc1:

List<AgentGroup> allGroups = context.AgentGroups.Include("Agents").Include("Agents.AgentGroups").ToList();

The error message I get is

Invalid object name 'dbo.AgentAgentGroups'.

And in fact, there isn't a table AgentAgentGroups, the table is dbo.AgentGroupAgents. Any ideas on getting this to work again?

I currently have no annotations and am not using the fluent API, it's all strictly the default code first conventions.

like image 901
Celeste Avatar asked Sep 20 '13 06:09

Celeste


1 Answers

In rc1 there appears to be a change in naming convention of the junction table in many-to-many associations. When I try your model in various EF versions, this is what I see:

  • EF5 (stable): AgentGroupAgents
  • EF6 (beta): AgentGroupAgents
  • EF6 (rc1): AgentAgentGroups

It wouldn't be bad if the beta was different, but rc1 differs from the last RTM version, which makes this a breaking change. Good catch!

Edit

Answer from the EF team:

Hello,
Prior to EF6 there were some areas of model creation that were non-deterministic - depending on whether you were running on x86 or x64 you could get a different model. The many to many join table name was one of these areas. In EF6 we addressed this to ensure the results would always be the same. Unfortunately, that does mean that for some models (depending on which architecture they were running on) upgrading to EF6 can cause a model change. But the model that is now calculated will be consistent across machines and future versions of EF. If you want to continue using the original name, the best option is to use the Fluent API to specify the join table name.
~Rowan

like image 156
Gert Arnold Avatar answered Nov 10 '22 00:11

Gert Arnold