Question

We're looking at upgrading our dinosaur of EF 4.2 this week to EF 6.0.2 (Code First). For the most part, aside from some namespace changes, everything works, with one exception.

Using EF 4.2, one particular many-to-many relation had a specific join table name expected. EF 6 expects a different one.

Here is the relevant portions of code

public class MyDbContext : DbContext {
    /* some unrelated collections*/
    public IDbSet<DBFoo> Foos { get; set; }
    /* some unrelated collections */
    public IDbSet<DBBar> Bars { get; set; }
    /* some unrelated collections */
}

public class DBFoo {
    /* DBFoo's properties */
    public virtual ICollection<DBBar> Bars { get; set; }
}

public class DBBar {
    /* DBBar's properties */

    /*some unrelated navigation properties (some single, some collections)*/
    public virtual ICollection<DBFoo> Foos { get; set; }
    /*more unrelated navigation properties*/
}

It's important to note that the "unrelated" properties mentioned are other entities, but themselves have no references to "DBFoo".

Now, in EF 4.2, the join table between these two entities was expected to be DBBarDBFoos. This was without any configuration in the model builder or context or by using any data annotations. After upgrading to EF 6.0.2, the join table is expected to be DBFooDBBars. Why?

Note: I have "fixed" this issue by using the fluent API to bind the relationship to its proper table. What I want to know is why this table(and only this table) changed in it's convention.

Ninja Edit - rearranging the property declaration in the DBContext had absolutely zero effect.

Était-ce utile?

La solution

see the answer from the EF team

https://entityframework.codeplex.com/workitem/1677

Answer by RoMiller

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top