EF 7 (Beta 7) One-to-One Relationship

c# entity-framework entity-framework-core

Question

I've noticed questions that have answered this problem for earlier beta versions of EF 7 (like here), but I haven't seen it solved for Beta 7, so here it goes:

I have 2 entities, simplified as follows:

public class FirstEntity
{
    public int FirstEntityID { get; set; }
    /*
        Other fields here
    */
    public int? SecondEntityID { get; set; }
    public SecondEntity SecondEntityProperty { get; set; }
}

public class SecondEntity
{
    public int FirstEntityID { get; set; } 
    /*
        Other fields here
    */
    public FirstEntity FirstEntityProperty { get; set; }
}

The way to map everything has changed so much from earlier versions. How do I map these two entities in a One-to-One relationship?

Popular Answer

It has been changed for EF7 rc1-final.

modelBuilder.Entity<FirstEntity>()
            .HasOne(q => q.SecondEntity)
            .WithMany()
            .HasForeignKey(q => q.SecondEntityID);

upd:

modelBuilder.Entity<FirstEntity>()
            .HasOne(q => q.SecondEntity)
            .WithOne(v => v.FirstEntity)
            .HasForeignKey<FirstEntity>(q => q.SecondEntityID);


Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why