1

I have an Offer entity that provides a link between two users in my system.

public class Offer
{
    public OfferStatus Status { get; set; }
    public decimal Amount {get; set; }

    public virtual User Provider { get; set; }
    public virtual User Receiver { get; set; }

}

This User entity has primary key of:

public string Id { get; set; }

I would like to have the two users ID strings appear in the Offer table like so:

public class Offer
{
    public OfferStatus Status { get; set; }
    public decimal Amount {get; set; }
    public string ProviderId { get; set; }
    public string ReceiverId { get; set; }

    public virtual User Provider { get; set; }
    public virtual User Receiver { get; set; }

}

I have tried:

public class Offer
{
    public OfferStatus Status { get; set; }
    public decimal Amount {get; set; }
    public string ProviderId { get; set; }
    public string ReceiverId { get; set; }

    [ForeignKey(nameof(ProviderId))]
    public virtual User Provider { get; set; }
    [ForeignKey(nameof(ReceiverId))]
    public virtual User Receiver { get; set; }

}

This is how I usually set foreign keys but usually I only have one of them and I use the naming convention magic that EF provides so I know what I have done here is wrong but I am confused how I am supposed to set their foreign key properties.

Guerrilla
  • 13,375
  • 31
  • 109
  • 210
  • [Possibly related](https://stackoverflow.com/q/5559043/335858). – Sergey Kalinichenko Dec 24 '17 at 16:49
  • You are still using EF naming conventions, so the model should work even w/o annotations, but annotations won't hurt. What's the actual issue you are experiencing (because I see no issue other than the lack of PK)? – Ivan Stoev Dec 24 '17 at 16:58
  • Trying to migrate I get error `Introducing FOREIGN KEY constraint 'FK_Offers_Users_ProviderId' on table 'Offers' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint or index. See previous errors.` – Guerrilla Dec 24 '17 at 17:02
  • 2
    Ah, the famous *multiple cascade paths* issue. You need to turn cascade delete off at least for one of the relationships, which requires fluent configuration (cannot be done via data annotations). Can you show the relevant part of the `User` class, in particular the collection navigation properties to `Offer` if any. – Ivan Stoev Dec 24 '17 at 17:15

0 Answers0