Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EF core RC2 One to One relationship #5623

Closed
amirjalali1 opened this issue Jun 2, 2016 · 6 comments
Closed

EF core RC2 One to One relationship #5623

amirjalali1 opened this issue Jun 2, 2016 · 6 comments
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@amirjalali1
Copy link

Steps to reproduce

Following the instruction here at the ef core doc.
and then using command line

dotnet ef migrations add
dotnet ef database update

the created relationship is one to many instead of one to one.

also i have tried to create models like this :

   public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }
        public int BlogImageId { get; set; }
        public virtual 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 virtual Blog Blog { get; set; }
    } 

and the related modelbuilder


  modelBuilder.Entity<Blog>()
                .HasOne(p => p.BlogImage)
                .WithOne(i => i.Blog)
                .HasForeignKey<BlogImage>(b => b.BlogId);

with no success

The issue

One to Many relationship is created instead of one to one

Further technical details

EF Core version: "1.0.0-rc2-final"
Operating system:
Visual Studio version: 2015 update 2 enterprise

Other details about my project setup:

@smitpatel
Copy link
Contributor

Can you also explain what you mean by One to Many relationship is created instead of one to one ?

@ajcvickers
Copy link
Member

Confirmed that the index created for the FK is not unique.
image

@divega
Copy link
Contributor

divega commented Jun 2, 2016

Same here.

using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Xunit;

namespace Repro5623
{
    public class Tests
    {
        [Fact]
        public void One_to_one_relationships_are_one_to_one()
        {
            using (var context = new MyContext())
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();
            }

            using (var context = new MyContext())
            {
                var blog = new Blog { Url = "http://blogs.msdn.com/diego" };
                var blogImage1 = new BlogImage { Caption = "Image1", Blog = blog };
                context.BlogImages.Add(blogImage1);
                context.SaveChanges();
            }

            using (var context = new MyContext())
            {
                var blog = context.Blogs.First();
                var blogImage2 = new BlogImage { Caption = "Image2", Blog = blog };
                context.BlogImages.Add(blogImage2);
                context.SaveChanges();
            }

            using (var context = new MyContext())
            {
                var images = context.BlogImages.Include(i => i.Blog).ToList();
                Assert.Same(images[1], images[1].Blog.BlogImage);
                Assert.Same(images[0], images[0].Blog.BlogImage); //fails
            }
        }
    }

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

    public class MyContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<BlogImage> BlogImages { get; set; }


        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder
                .UseSqlServer("Data Source = (localdb)\\mssqllocaldb;Initial Catalog=Repro5623;Integrated Security=yes");
        }
    }
}

@smitpatel
Copy link
Contributor

See #700 (comment)
This was our decision not to make unique index for one-to-one relationship. Our code treats the one-to-one relationship in its way and never add duplicates but we don't enforce that in database.

@ajcvickers
Copy link
Member

@smitpatel I'm not sure where on that issue there was a decision to not make the index unique, but I may be missing something, @divega @rowanmiller @bricelam @AndriySvyryd Thoughts?

@ajcvickers ajcvickers self-assigned this Jun 2, 2016
@bricelam
Copy link
Contributor

bricelam commented Jun 2, 2016

+1 It should be a unique index. #2868 was done to support this on SQL Server.

@ajcvickers ajcvickers added this to the 1.0.0 milestone Jun 2, 2016
ajcvickers added a commit that referenced this issue Jun 2, 2016
Issue #5623

The index created to go alongside the relationship was not getting its uniqueness from the relationship, and hence the database created by Migrations did not have a unique index for the FK column. The fix is to propagate uniqueness from the relationship metadata to the index.
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 15, 2022
@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

5 participants