0

I need your help understanding relationships between tables. I´m having a hard time trying to understand the usage/need of using navigation properties with foreign key properties to define relantionships. Given the 2 classes below,

public class Person
{
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid Id { get; set; }

        [Required]
        public string Name{ get; set; }
}

public class Package
{
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid Id { get; set; }

        [Required]
        public Guid AccountId { get; set; }
        public virtual Person Account { get; set; }

        public Guid ShipperId { get; set; }
        public virtual Client Shipper { get; set; }

        [Required]
        public Guid ReceiverId { get; set; }
        public virtual Client Receiver { get; set; }

}

If I try to update the database, I get the error

Introducing FOREIGN KEY constraint 'FK_' on table 'Packages' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

On the other hand if I remove the property public Guid ....Id { get; set; }, the database is created with the foreign keys but the [Required] annotation as no effect.

What´s the difference between including a Guid property and not?

4
  • 1
    Did you try to put [ForeignKey("AccountId")] on Account property? Here is the tutorial Commented Feb 14, 2021 at 16:45
  • And also, could you share your Fluent Api configuration? Commented Feb 14, 2021 at 18:03
  • There are two Id properties. Which one did you remove? I guess it's the one in Person. Please read about the differences in (foreign key) relationships and owned types. Commented Feb 15, 2021 at 8:14
  • After having done that, please note that there's are tons of questions about this SQL Server limitation on foreign keys. Commented Feb 15, 2021 at 8:20

1 Answer 1

1

try to use this classes:


 public class Person
    {
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid Id { get; set; }

        [Required]
        public string Name{ get; set; }

        [InverseProperty(nameof(Package.Account))]
        public virtual ICollection<Package> Packages { get; set; }

    }

    public class Package
    {
        
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid Id { get; set; }

        [Required]
        public Guid AccountId { get; set; }
       
        [ForeignKey(nameof(AccountId))]
        [InverseProperty("Packages")]
        public virtual Person Account { get; set; }
    }

and use this code in dbcontext:

modelBuilder.Entity<Package>(entity =>
{
    entity.HasOne(d => d.Person)
    .WithMany(p => p.Packages)
    .HasForeignKey(d => d.AccountId)
    .OnDelete(DeleteBehavior.ClientSetNull);

});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.