1

Below are the model classes. The database and tables were created properly. I can post some tested data to them through postman, but I get the Multiplicity constraint violated error when retrieving all the data from the table as shown in the picture below. In one-to-many relationship in my other applications, everything works as expected.

public class Quote
{
    public Quote()
    {
        Outlet = new Outlet();
    }
    public int Id { get; set; }
    public string RefNumber { get; set; }
    public string Status { get; set; }
    public string Name { get; set; }
    public string DeliveryAddress { get; set; }
    public DateTime DeliveryDate { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }
    public string Comment { get; set; }
    public DateTime Date { get; set; }
    public virtual Outlet Outlet { get; set; }
}

public class Outlet
{
    public Outlet()
    {
        Inlets = new List<Inlet>();
    }
    [ForeignKey("Quote")]
    public int Id { get; set; }
    public int OutletAngle { get; set; }
    public int OutletSize { get; set; }
    public string OutletType { get; set; }
    public virtual Quote Quote { get; set; }
    public ICollection<Inlet> Inlets { get; set; }

}

public class Inlet
{
    public int Id { get; set; }
    public int InletAngle { get; set; }
    public int InletSize { get; set; }
    public string InletType { get; set; }
}
}

Multiplicity constraint violated

Samseth
  • 81
  • 1
  • 6
  • I'd start by removing `Outlet = new Outlet();` from the constructor, see [this](https://stackoverflow.com/a/20773057/861716). – Gert Arnold Aug 29 '18 at 09:59
  • If I remove that. I will get "object reference not set to an instance of an object." when I access Outlet object (eg: quote.Outlet.OutletAngle = quoteNspec.OutletAngle;) – Samseth Aug 29 '18 at 10:07
  • Then something else is wrong. You don't solve that by creating an empty `Outlet` object. You should check the SQL query when using `Include`. – Gert Arnold Aug 29 '18 at 10:59
  • Yeah, that's yet another problem that was concealed (not fixed!) by this `new Outlet()`. It's a well-know issue with serializing. – Gert Arnold Aug 29 '18 at 12:08
  • I removed Outlet = new Outlet(); from the constructor. I got this exception in POSTMAN "ExceptionMessage": "Self referencing loop detected for property 'Quote' with type 'System.Data.Entity.DynamicProxies.Quote_30914B5D2310E06FC24EB11BFD0FEDE921E2029E86F2132A59883F4CC9A3B260'. Path '[0].Outlet'.", – Samseth Aug 29 '18 at 12:40
  • And it breaks my post API (Additional information: Object reference not set to an instance of an object.) – Samseth Aug 29 '18 at 12:42
  • See my previous response to your previous (now deleted) comment. – Gert Arnold Aug 29 '18 at 12:43
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/179026/discussion-between-samseth-and-gert-arnold). – Samseth Aug 29 '18 at 12:44
  • Look, it's *indisputable* that you should remove this line in the constructor. I can't help that you based your application on an architectural flaw. Now you should fix the issues that were concealed until today. It's beyond the scope of this question to delve into that. – Gert Arnold Aug 29 '18 at 12:48
  • Thanks, Gert! I was thinking of merging the one-to-one tables if I cannot fix this bug! Using sql query directly in EF might work but it might not be worth it. – Samseth Aug 29 '18 at 13:01

0 Answers0