What Does Principal End of an Association Means in 1:1 Relationship in Entity Framework

What does principal end of an association means in 1:1 relationship in Entity framework

In one-to-one relation one end must be principal and second end must be dependent. Principal end is the one which will be inserted first and which can exist without the dependent one. Dependent end is the one which must be inserted after the principal because it has foreign key to the principal.

In case of entity framework FK in dependent must also be its PK so in your case you should use:

public class Boo
{
[Key, ForeignKey("Foo")]
public string BooId{get;set;}
public Foo Foo{get;set;}
}

Or fluent mapping

modelBuilder.Entity<Foo>()
.HasOptional(f => f.Boo)
.WithRequired(s => s.Foo);

1:1 relationship in Entity framework

you want a 1 to 1 relation between user and post ? A user can only post one, and only, post ?

Anyway, in EF (at least 6) a 1 to 1 relation can be established between two entities sharing the same PK. That is the PK is the FK. So you must set the PK of posts as a string.

Otherwise you are in a 1 to * relation.

The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations

Entity Framework Code-First conventions are assuming that EntityA.EntityB and EntityB.PreferredEntityA belong to the same relationship and are the inverse navigation properties of each other. Because both navigation properties are references (not collections) EF infers a one-to-one relationship.

Since you actually want two one-to-many relationships you must override the conventions. With your model it's only possible with Fluent API:

modelBuilder.Entity<EntityA>()
.HasRequired(a => a.EntityB)
.WithMany()
.HasForeignKey(a => a.EntityBID);

modelBuilder.Entity<EntityB>()
.HasOptional(b => b.PreferredEntityA)
.WithMany()
.HasForeignKey(b => b.PreferredEntityAID);

(If you use this you can remove the [ForeignKey] attributes.)

You cannot specify a mapping that would ensure that the preferred child is always one of the associated childs.

If you don't want to use Fluent API but only data annotations you can add a collection property in EntityB and relate it to EntityA.EntityB using the [InverseProperty] attribute:

public class EntityB
{
public int ID { get; set; }
public Nullable<int> PreferredEntityAID { get; set; }

[ForeignKey("PreferredEntityAID")]
public virtual EntityA PreferredEntityA { get; set; }

[InverseProperty("EntityB")] // <- Navigation property name in EntityA
public virtual ICollection<EntityA> EntityAs { get; set; }
}

Mapping 1-0..1 Relationship with Navigation Property Without FK

Ended up being able to get this relationship to work like this:

public class Vehicle
{
[Key]
public int Id { get; set; }

public virtual RecVehicle RecVehicle { get; set; }
}
public class RecVehicle
{
[Key]
public int VehicleId { get; set; }

[ForeignKey("VehicleId"), Required] //<--- Required attr fixed the principal/dependent confusion EF was having
public virtual Vehicle Vehicle { get; set; }
}


Related Topics



Leave a reply



Submit