1

Let's take a look at the example from the official docs:

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; }
} 

As we can see, we have fully defined one-to-one relationship between Blog and BlogImage, where Blog is parent, and BlogImage is child entity.

But, if we'll take a look at created tables, we'll see this headers:

  • BlogId, Url for Blog
  • BlogImageId, Image, Caption, BlogId for BlogImage

I have some misunderstood with this BlogId in BlogImage table, shouldn't we have defined the BlogImageId foreign key in Blog table, what's the reason, to define foreign keys in child entiities nor in parent's? If we have thousand parent's of one child, there would be thousand of foreign keys in child entity! Very strange.

Chris Pickford
  • 8,642
  • 5
  • 42
  • 73
Yurii N.
  • 5,455
  • 12
  • 42
  • 66
  • look at this thread http://stackoverflow.com/questions/10292355/how-do-i-create-a-real-one-to-one-relationship-in-sql-server – Thomas Mar 15 '16 at 10:13
  • @Thomas looks interesting, but the first comment say _"both tables having a foreign key relationship to each other."_ while we don't have the foreign key defined in Blog table. – Yurii N. Mar 15 '16 at 10:27

2 Answers2

0

The documentation has just explained the one-to-one relationship in this way but it's not the only way. The way you are thinking is also right. Following is also valid one to one relationship in EF.

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
    public int BlogImageId { 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 Blog Blog { get; set; }
} 
Mahbubur Rahman
  • 4,961
  • 2
  • 39
  • 46
  • I've just tried and discovered disadvantage, I can't do cascade insert, like Blog entity with BlogImage inside, I need add them separately to their tables, while in official docs examples it works well. – Yurii N. Mar 15 '16 at 11:00
  • It's more simpler. `public ICollection BlogImages{ get; set; }` will create one to many relationship. No key is needed to define.It will automatically map. – Mahbubur Rahman Mar 15 '16 at 11:30
0

Yes, for exact one-to-one relationship you should use this models (your generated tables schemas correspond to one-to-many relationship):

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }    
    public virtual BlogImage BlogImage { get; set; }
}
public class BlogImage
{
    [Key, ForeignKey("Blog")]
    public int BlogImageId { get; set; }

    public byte[] Image { get; set; }
    public string Caption { get; set; }

    public virtual Blog Blog { get; set; }
} 
AlSki
  • 6,868
  • 1
  • 26
  • 39
Slava Utesinov
  • 13,410
  • 2
  • 19
  • 26