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

EF7 One to one relation results tables associated with simple one to many relationship #4792

Closed
BehnamAbdy opened this issue Mar 14, 2016 · 8 comments

Comments

@BehnamAbdy
Copy link

Even when I try the sample code in Microsoft ef7 one to one docs the result is blog and blogimage as one to many
Sample from Ms site https://docs.efproject.net/en/latest/modeling/relationships.html#one-to-one

class MyContext : DbContext
{
public DbSet Blogs { get; set; }
public DbSet 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; }
}
@BehnamAbdy BehnamAbdy changed the title One to one relation results tables associated with simple one to many relationship EF7 One to one relation results tables associated with simple one to many relationship Mar 14, 2016
@rowanmiller
Copy link
Contributor

Are you using RC1? If so, the work to introduce indexes on FK properties wasn't yet done. We've since implemented this so that you'll get a unique index on the FK property of a one-to-one (#3764). This will ship in RC2.

@BehnamAbdy
Copy link
Author

yes im using the "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final"
thank u anyway and i hope u release the RC2 soon

@amirjalali1
Copy link

amirjalali1 commented May 31, 2016

the problem still exist in rc2 even with fluent api its not possible to create one to one relation

@divega divega reopened this May 31, 2016
@divega
Copy link
Contributor

divega commented May 31, 2016

Reopening. From talking to @ajcvickers and @smitpatel it seems we don't affect the uniqueness of the FK side when setting up a 1:1 relationship.

@ajcvickers
Copy link
Member

@divega I don't think we talked about this. The code above explicitly creates a one to one relationship using the fluent API. The original bug was the Migrations ignores our metadata about this and instead relies on the idea that we will by convention also create a unique index for the FK column. Therefore if you use Migrations to create the database you will get a unique index on the FK column, unless you remove that unique index, in which case you won't. Regardless, the relationship is always one to one in our metadata and treated as such.

What we discussed on Friday was that if the relationship would be one to many in our metadata because it has only one reference nav property, then we won't check to see if the app put an explicit unique index on the FK property and use this to change the relationship to one to one.

@rowanmiller
Copy link
Contributor

@1amirjalai can you open a new issue with details of the issue you are seeing (be sure to include code to reproduce the issue etc.).

@rowanmiller rowanmiller added this to the 1.0.1 milestone May 31, 2016
@amirjalali1
Copy link

@rowanmiller the issue is created here #5623

@divega
Copy link
Contributor

divega commented Jun 2, 2016

Closing this as all the relevant details are in #5623.

@divega divega closed this as completed Jun 2, 2016
@divega divega removed this from the 1.0.1 milestone Jun 2, 2016
@divega divega removed their assignment Jun 2, 2016
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants