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

Model building: Cycles not detected properly #7039

Closed
smitpatel opened this issue Nov 17, 2016 · 5 comments
Closed

Model building: Cycles not detected properly #7039

smitpatel opened this issue Nov 17, 2016 · 5 comments

Comments

@smitpatel
Copy link
Member

Following model should throw exception in model validation.

public class Blog
{
    public int Id { get; set; }
    public int Id2 { get; set; }
    public int Fk { get; set; }
    public Post Post { get; set; }
}

public class Post
{
    public int Id { get; set; }
    public int Fk { get; set; }
    public Blog Blog { get; set; }
}

modelBuilder.Entity<Blog>(b =>
{
    b.HasOne(e => e.Post).WithMany().HasForeignKey(e => e.Id2);
    b.HasMany<Post>()
        .WithOne(p => p.Blog)
        .HasForeignKey(p => new {p.Id, p.Fk})
        .HasPrincipalKey(e => new {e.Id2, e.Fk});
});
@ajcvickers
Copy link
Member

@smitpatel Can you explain why it should throw?

@AndriySvyryd
Copy link
Member

I thought it could be represented by a simpler model, but now I think we should still support it as is.

@smitpatel
Copy link
Member Author

It throws exception while migrating database. Do we detect cycles or let SqlServer throw instead?

Unhandled Exception: System.Data.SqlClient.SqlException: Introducing FOREIGN KEY constraint 'FK_Post_Blog_Id_Fk' on table 'Post' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

@ajcvickers
Copy link
Member

Is this different from the normal issue SQL Server has with cascade paths, as described in #6956?

@smitpatel
Copy link
Member Author

No. I will close this one then.

@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
Projects
None yet
Development

No branches or pull requests

3 participants