0

I have created a (dummy) database view, called; vContact, and a stored procedure returning the same types as the view (the view would be too complex if I should do what I need). When I invoke the stored procedure I get a ContactView object with the CompanyId filled but the Company object shows (when watched in Visual Studio):

A relationship multiplicity constraint violation occurred: An EntityReference can have no more than one related object, but the query returned more than one related object. This is a non-recoverable error.

I'm using a database-first approch... I think that I have to do some fluent configuration? I think that it's something about I'm using an "encapsulating" view class so the reversed objects are not of equal type ?

Here are my objects:

[Table("vContact")]
public class ContactView : Contact
{
    // …
}

[Table("Contact")]
public class Contact
{
    [Key]
    public Guid Id { get; set; }

    // …

    [Required]
    public int CompanyId { get; set; }
    public virtual Company Company { get; set; }

    public virtual ICollection<Activity> Activities { get; set; }
}

[Table("Company")]
public class Company
{
    [Key]
    public int Id { get; set; }

    // …

    public virtual ICollection<Contact> Contacts { get; set; }

    public virtual ICollection<Activity> Activities { get; set; }
}

[Table("Activity")]
public class Activity
{
    [Key]
    public Guid Id { get; set; }

    // …

    [Required]
    public int CompanyId { get; set; }
    public virtual Company Company { get; set; }
}

My goal is to get a ContactView object, with the filles Contacts and Activities, but the Company only have to contain the "basics" - not all the contacts and activities that refers to the company.

But the properties have to be there on the company object when querying the company for activities and activities using the vCompany view by another stored procedure.

PS. I got equivalent views and stored procedures for Contact and Activities

Kim Rasmussen
  • 453
  • 1
  • 8
  • 21
  • What's the SQL for your contact view look like? I'll bet it is returning multiple rows per Contact. Further, but perhaps off topic, is that EF obviates the need to create views in many cases. Have you considered scrapping `vContact`? I would. – Yuck Apr 23 '15 at 20:23
  • That might be the case...the view was only for EF to get the types right - but the actual call was a Stored Procedure. But EF does (and requires) atlot of thing in the dark... I have now removed all views and stored procedures and am trying to achieve the same using multiple LINQ expressions. – Kim Rasmussen Apr 25 '15 at 09:29

0 Answers0