How to configure One-to-One or ZeroOrOne-to-One relationships in Entity Framework 7 Code First using Data Annotations or Fluent Api?
Asked
Active
Viewed 1.4k times
11
-
1Read this [article](http://www.entityframeworktutorial.net/code-first/configure-one-to-one-relationship-in-code-first.aspx) – Slava Utesinov Feb 19 '16 at 14:12
2 Answers
25
You can define OneToOne relationship using Fluent API in Entity Framework 7 as below
class MyContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<BlogImage> BlogImages { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.HasOne(p => p.BlogImage)
.WithOne(i => i.Blog)
.HasForeignKey<BlogImage>(b => b.BlogForeignKey);
}
}
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public BlogImage BlogImage { get; set; }
}
public class BlogImage
{
public int BlogImageId { get; set; }
public byte[] Image { get; set; }
public string Caption { get; set; }
public int BlogForeignKey { get; set; }
public Blog Blog { get; set; }
}

Sangram More
- 247
- 2
- 4
-
And something that wasn't obvious to me, and is no where to be found in the documentation (or I missed it), if you wanted BlogImage to have a one to zero or one relationship to Blog, instead of a one to one (i.e. a BlogImage on it's own that optionally does not reference a Blog, you merely have to make the BlogImage.BlogForeignKey nullable, like this: public int? BlogForeignKey { get; set; } – Jacques Bosch Oct 12 '17 at 21:39
-
4Is this property really required? `public int BlogForeignKey { get; set; }` – Mohammed Noureldin Nov 03 '17 at 12:10
4
The above answer is absolutely correct.
Just for readers information: This has been explained nicely in official documentation
One-to-one
One to one relationships have a reference navigation property on both sides. They follow the same conventions as one-to-many relationships, but a unique index is introduced on the foreign key property to ensure only one dependent is related to each principal.
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public BlogImage BlogImage { get; set; }
}
public class BlogImage
{
public int BlogImageId { get; set; }
public byte[] Image { get; set; }
public string Caption { get; set; }
public int BlogId { get; set; }
public Blog Blog { get; set; }
}
Note
EF will choose one of the entities to be the dependent based on its ability to detect a foreign key property. If the wrong entity is chosen as the dependent, you can use the Fluent API to correct this.

TheKingPinMirza
- 7,924
- 6
- 51
- 81
-
2Do I really need to define this property? `public int BlogId { get; set; }` – Mohammed Noureldin Nov 03 '17 at 12:11
-
1@MohammedNoureldin The property `public int BlogId { get; set; }` is needed because `"EF will choose one of the entities to be the dependent based on its ability to detect a foreign key property."` See the note in https://learn.microsoft.com/en-us/ef/core/modeling/relationships in the One-to-one section of Other Relationship Patterns. – William Ardila Dec 06 '17 at 20:02