2

Is it possible to have two (or more) navigation properties of the same type?

My model looks like this...

public class Agreement
{
    public int Id { get; set; }
    public Guid? BuyerId { get; set; }
    public Guid? SellerId { get; set; }

    public AgreementInfo ByerAgreementInfo { get; set; }
    public AgreementInfo SellerAgreementInfo { get; set; }
}

public class AgreementInfo
{
    // PK is AgreementId and OwnerActorId combined.
    public int AgreementId { get; set; }
    public Guid OwnerActorId { get; set; }
    public string Name { get; set; }
}

... and i'm trying to include the navigation properties by matching the AgreementId and the ByerId/SellerId...

modelBuilder.Entity<Agreement>().HasOne(x => x.ByerAgreementInfo).WithOne().HasForeignKey<Agreement>(x => new {x.Id, x.ProviderId});
modelBuilder.Entity<Agreement>().HasOne(x => x.SellerAgreementInfo).WithOne().HasForeignKey<Agreement>(x => new { x.Id, x.RequesterId });

... but this results in a circural dependecy.

Is there any way to include these properties without using a foreign key? Or is there another solution (except for adding an id-column to the info table) that allows me to use the info-table rows as navigation properties in the agreement class?

1
  • Check my answer. It should work for you. Jul 12, 2019 at 3:20

2 Answers 2

0

... but this results in a circural dependecy

Yes! It will. To overcome this you have to specify .OnDelete(DeleteBehavior.Restrict); in your Fluent API configuration as follows but first you have to write your Agreement model class as follows too:

public class Agreement
{
    public int Id { get; set; }
    public Guid? BuyerId { get; set; }
    public Guid? SellerId { get; set; }

    public int AgreementIdForBuyer { get; set; }
    public Guid OwnerActorIdForBuyer { get; set; }

    public int AgreementIdForSeller { get; set; }
    public Guid OwnerActorIdForSeller { get; set; }

    public AgreementInfo ByerAgreementInfo { get; set; }
    public AgreementInfo SellerAgreementInfo { get; set; }
}

Now in Fluent API configuration:

modelBuilder.Entity<Agreement>()
    .HasOne(x => x.ByerAgreementInfo)
    .WithOne()
    .HasForeignKey<Agreement>(p => new {p.AgreementIdForBuyer, p.AgreementIdForBuyer})
    .OnDelete(DeleteBehavior.Restrict); // <-- Here it is

modelBuilder.Entity<Agreement>()
    .HasOne(x => x.SellerAgreementInfo)
    .WithOne()
    .HasForeignKey<Agreement>(p => new {p.AgreementIdForSeller, p.OwnerActorIdForSeller})
    .OnDelete(DeleteBehavior.Restrict); // <-- Here it is
0

There are more than a handful of ways to achieve what you want. You could use Data Annotations with InverseProperty attribute on your child, ForeignKey attribute on your parent, or fluent syntax either way in your model builder. I tend to use attributes (Data Annotations) where I can (it's just my personal taste), I find it need to see the relationships in the actual mode itself (though others might not).

Using ForeignKey attribute:

public class Agreement
{
    ...

    [ForeignKey("ByerAgreementInfo ")]
    public int ByerAgreementInfoId { get; set; }
    public AgreementInfo ByerAgreementInfo { get; set; }

    [ForeignKey("SellerAgreementInfo ")]
    public int SellerAgreementInfoId { get; set; }
    public AgreementInfo SellerAgreementInfo { get; set; 
}

Using InverseProperty attribute:

public class AgreementInfo
{

    ...

    [InverseProperty("ByerAgreementInfo ")]
    public ICollection<Agreement> Sellers { get; set; }
    [InverseProperty("SellerAgreementInfo ")]
    public ICollection<Agreement> Buyers { get; set; }
}

If you want to use Fluent Syntax, I believe the following will work (though I haven't used it for a while):

modelBuilder.Entity<Agreement>()
    .HasOne(x => x.ByerAgreementInfo)
    .WithOne()
    .HasForeignKey<Agreement>(p => p.ByerAgreementInfoId);

modelBuilder.Entity<Agreement>()
    .HasOne(x => x.SellerAgreementInfo)
    .WithOne()
    .HasForeignKey<Agreement>(p => p.SellerAgreementInfoId);

Note: I'm think you may need the actual Id's in your model, though I can't remember.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.