-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Comments
Can you also explain what you mean by |
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");
}
}
} |
See #700 (comment) |
@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? |
+1 It should be a unique index. #2868 was done to support this on SQL Server. |
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.
Steps to reproduce
Following the instruction here at the ef core doc.
and then using command line
the created relationship is one to many instead of one to one.
also i have tried to create models like this :
and the related modelbuilder
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:
The text was updated successfully, but these errors were encountered: