Possible Duplicate:
Code First: Independent associations vs. Foreign key associations?
In EF 4 or EF 5 Code First, what is an "independent association" and what is a "foreign key association", as used on MSDN or Foreign Key vs. Independent Relationships - is there improvement with Entity Framework 5? (emphasis added):
2.4.1 Using Foreign Key Associations to reduce view generation cost
We have seen a number of cases where switching the associations in the model from Independent Associations to Foreign Key Associations dramatically improved the time spent in view generation.
So -- now I know which to use. If only I knew what they were and how one would convert to it! My question, then, is, how would you define those terms? What fluent/annotations/conventions invoke each?
When a variable is associated with an outcome after adjusting for multiple other potential prognostic factors (often after regression analysis), the association is an independent association.
Foreign Key Association is where you have a foreign key property in your model in addition to the corresponding Navigation Property.
When you change the relationship of the objects attached to the context by using one of the methods described above, Entity Framework needs to keep foreign keys, references, and collections in sync.
A navigation property is an optional property on an entity type that allows for navigation from one end of an association to the other end. Unlike other properties, navigation properties do not carry data.
Just my opinions about WHY to use independent or foreign key associations:
Pros:
Cons:
EntityObject
base entities where every navigation property to principal entity was paired with another property suffixed Reference
providing some additional details about the relation.Modified
state. Every modification always consists of setting old relation as deleted and creating new relation as added - this is a real mess when you try to use it.Pros:
Cons:
This difference matters only for one-to-many associations because one-to-one are always Foreign key associations and many-to-many are always independent associations.
Foreign Key Association is where you have a foreign key property in your model in addition to the corresponding Navigation Property. Independent Association is when you have a foreign key column in your database but the foreign key property corresponding to this column is not in your model - i.e. you have a NavigationProperty but there is no foreign key property that would tell you what the ID value of the related property is without actually going to the related property.
Here is an example of a model with an Independent Association (notice that the Dependent does not have a foreign key - just navigation property):
public class Dependent
{
public int Id { get; set; }
[Required]
public Principal PrincipalEntity { get; set; }
}
public class Principal
{
public int Id { get; set; }
public ICollection<Dependent> DependentEntities { get; set; }
}
public class MyContext : DbContext
{
public DbSet<Dependent> Dependents { get; set; }
public DbSet<Principal> Principals { get; set; }
}
And here is an example of the same model but with the ForeignKey Association (notice the PrincipalEntity_Id property and the [ForeignKey()] attribute):
public class Dependent
{
public int Id { get; set; }
public int PrincipalEntity_Id { get; set; }
[Required]
[ForeignKey("PrincipalEntity_Id")]
public Principal PrincipalEntity { get; set; }
}
public class Principal
{
public int Id { get; set; }
public ICollection<Dependent> DependentEntities { get; set; }
}
public class MyContext : DbContext
{
public DbSet<Dependent> Dependents { get; set; }
public DbSet<Principal> Principals { get; set; }
}
Note that your database won't change - the underlying database always had the column for the foreign key but with the independent association it was not exposed.
With foreign key associations you can update the relationship just by changing the value of the foreign key. This is handy if you know the value because you don't need to load the entity you want to update the navigation property to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With