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: StackOverflowException when two properties share the same foreign key #6115

Closed
ntsekouras opened this issue Jul 20, 2016 · 7 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

@ntsekouras
Copy link

ntsekouras commented Jul 20, 2016

public class FileRecord : RelativeEntity
    {
        public Guid DmsId { get; set; }
        public int EntityId { get; set; }
        public string MimeType { get; set; }
        public FileRecordEntity Entity { get; set; }

        [ForeignKey("EntityId")]
        public virtual Decision Decision { get; set; }
        [ForeignKey("EntityId")]
        public virtual Invitation Invitation { get; set; }
    }

Can i have two properties with the same foreign key?

EF Core version: 1.0.0
Operating system: Win10 64
Visual Studio version: VS2015

@ajcvickers
Copy link
Contributor

@ntsekouras Yes. Are you seeing an error? If so, please provider error, stack trace, and full repro code.

@rowanmiller rowanmiller added the closed-no-further-action The issue is closed and no further action is planned. label Jul 20, 2016
@rowanmiller
Copy link
Contributor

Closing as we have answered the question. If you see an error, reply with details and we will reopen.

@xrkolovos
Copy link

We get a big exception "dotnet has stopped working"

dotnet : Process is terminated due to StackOverflowException.
At line:1 char:1

  • dotnet ef migrations add filerecordsTest
  • - CategoryInfo          : NotSpecified: (Process is term...rflowException.:String) [], RemoteException
    - FullyQualifiedErrorId : NativeCommandError
    

@xrkolovos
Copy link

The other 2 classes are like this

public class Invitation : AuditedEntity
{
    ...
    public List<FileRecord> FileRecords { get; set; }
}

We also set up fluent API like this

/*          FileRecorts           */
modelBuilder.Entity<FileRecord>()
    .HasOne(r => r.Decision)
    .WithMany(c => c.FileRecords)
    .OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<FileRecord>()
    .HasOne(r => r.Invitation)
    .WithMany(c => c.FileRecords)
    .OnDelete(DeleteBehavior.Restrict);

@rowanmiller rowanmiller reopened this Jul 22, 2016
@rowanmiller rowanmiller removed the closed-no-further-action The issue is closed and no further action is planned. label Jul 22, 2016
@smitpatel
Copy link
Contributor

StackOverflow in latest codebase too. Setting EntityId as foreign key for one relationship removes the other one and when convention re-discover the other relationship, it will try to use EntityId and same thing repeats.

@rowanmiller rowanmiller changed the title Can i have two properties with the same foreign key? Model Building: StackOverflowException when two properties share the same foreign key Jul 22, 2016
@rowanmiller rowanmiller added this to the 1.1.0 milestone Jul 25, 2016
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jul 26, 2016
@AndriySvyryd AndriySvyryd removed their assignment Jul 26, 2016
@ntsekouras
Copy link
Author

Is there a workaround for v1.0 of efcore?

@AndriySvyryd
Copy link
Member

AndriySvyryd commented Aug 10, 2016

@ntsekouras You would need to remove the annotations, configure one of the relationships using Fluent API and the other one using Model API:

modelBuilder.Entity<FileRecord>().HasOne(f => f.Decision).WithMany().HasForeignKey(f => f.EntityId);
modelBuilder.Entity<FileRecord>().Ignore(f => f.Invitation);

var fileRecordEntityType = modelBuilder.Entity<FileRecord>().Metadata;
var invitationEntityType = modelBuilder.Entity<Invitation>().Metadata;
fileRecordEntityType.AddForeignKey(
    new[] { fileRecordEntityType.FindProperty("EntityId") },
    invitationEntityType.FindPrimaryKey(),
    invitationEntityType)
    .HasDependentToPrincipal(typeof(FileRecord).GetTypeInfo().GetDeclaredProperty("Invitation"));

@ajcvickers ajcvickers modified the milestones: 1.1.0-preview1, 1.1.0 Oct 15, 2022
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

6 participants