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

Two One-to-One relationships to same table #16172

Closed
PetterT2 opened this issue Jun 20, 2019 · 2 comments
Closed

Two One-to-One relationships to same table #16172

PetterT2 opened this issue Jun 20, 2019 · 2 comments

Comments

@PetterT2
Copy link

PetterT2 commented Jun 20, 2019

First of all it does not seem to work to have to FKs referencing the same table. According to what I understand this might be a limitation in SQL, and handled by triggers instead.

Question:
Is there a proper way to make this work using the fluent api, i.e. using cascade or trigger?

Issue:
No way to make it not throw the exception below. I.e. no way to not generate onDelete: ReferentialAction.NoAction in the migration file. Have tried the different onDelete options but it does not work..The suggested "ON DELETE NO ACTION" does not seem to be available using the fluent api?

(I still think the FKs should be required to at least throw exception if not handled on delete, so i don't think optional is good)

public enum DeleteBehavior
    {
        ClientSetNull = 0,
        Restrict = 1,
        SetNull = 2,
        Cascade = 3
    }

is missing the NoAction which is an option in the migration files:
onDelete: ReferentialAction.NoAction

internal class PageVariationEntity
{
  public int PageId { get; set; }
  public virtual PageEntity Page { get; set; }

  public int VariationPageId { get; set; }
  public virtual PageEntity VariationPage { get; set; }
}

internal class PageEntity: AuditingEntityBase
{
     [Key, Column(Order = 1), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
     public int Id { get; set; }
}
//fluent config:
modelBuilder.Entity<PageVariationEntity>()
            .HasOne(pv => pv.Page).WithOne().HasForeignKey<PageVariationEntity>(pv => pv.PageId).IsRequired();
            modelBuilder.Entity<PageVariationEntity>()
            .HasIndex(pv => pv.PageId);

            modelBuilder.Entity<PageVariationEntity>()
            .HasOne(pv => pv.VariationPage).WithOne().HasForeignKey<PageVariationEntity>(pv => pv.VariationPageId).IsRequired();
            modelBuilder.Entity<PageVariationEntity>()
           .HasIndex(pv => pv.VariationPageId).IsUnique();

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

Steps to reproduce

Create an entity with two required foreign keys to the same table

@ajcvickers
Copy link
Contributor

@PetterT2 Both Restrict and ClientSetNull should work for this and give the same behavior in the database. If that's not the case, then please file a small, runnable project/solution or complete code listing that demonstrates the behavior you are seeing.

See also #12661 (comment)

@ajcvickers
Copy link
Contributor

EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.

BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.

@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

2 participants