0

I have two models with a navigation property in one to the other. Model looks like this: I am calling the two models in the same view. SuiteCategoryModels as the default and SuitesModels as a partial view.

public class SuiteCategoryModels
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int SuiteCatID { get; set; }
    [Required]
    [DataType(DataType.Text)]
    [Display(Name = "Category")]
    [StringLength(50, ErrorMessage = "The {0} must be at least 6 characters long.", MinimumLength = 6)]
    public string CatName { get; set; }

    [Required]
    [DataType(DataType.Text)]
    [Display(Name = "Category Description")]
    [StringLength(500, ErrorMessage = "The {0} must be at least 6 characters long.", MinimumLength = 6)]
    public string CatDesc { get; set; }

    public virtual ICollection<SuitesModels> SuitesModels { get; set; }
}

and the second model like this:

      public class SuitesModels
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int SuiteID { get; set; }

    [Required]
    [DataType(DataType.Text)]
    [Display(Name = "Suite Name")]
    [StringLength(50, ErrorMessage = "The {0} must be at least 6 characters long.", MinimumLength = 6)]
    public string SuiteName { get; set; }

    [Required]
    [DataType(DataType.Text)]
    [Display(Name = "Description")]
    [StringLength(50, ErrorMessage = "The {0} must be at least 6 characters long.", MinimumLength = 6)]
    public string SuiteDesc { get; set; }

    [DataType(DataType.ImageUrl)]
    [Display(Name = "Picture")]
    public string SuitePix { get; set; }

    [Required]
    [Display(Name = "Rate")]
    public int SuiteRate { get; set; }

    [Required]
    [Display(Name = "Persons Per Suite")]
    public int PersonPerSuite { get; set; }

    [Required]
    [Display(Name = "Status")]
    public bool SuiteStatus { get; set; }

    public int SuiteCatID { get; set; }

    public virtual SuiteCategoryModels SuiteCategory { get; set; }
}

I created a controller action to view like this:

    public ActionResult Index()
    {
        var m = db.SuiteCategoryModels.ToList();
        //ViewBag.BB = 
        //Dispose(false);
        return View(m);
    }

and the other like this:

 public ActionResult SuitesIndex(int id = 0)
    {
        var mm = db.Suites.Select(r => r.SuiteCategory)
                  .Where(r => r.SuiteCatID == id);
        //SuitesModels suitesmodels = db.Suites.Find(id);
        if (mm == null)
        {
            return HttpNotFound();
        }
        return PartialView("SuitesIndex", mm.ToList());
    }

But I have an error page stating: There is already an open DataReader associated with this Command which must be closed first..

  1. What are the reasons of this error?
  2. What do I do to correct this?
MikeSW
  • 16,140
  • 3
  • 39
  • 53
Peter
  • 793
  • 3
  • 8
  • 31
  • Does db refer to the same instance in both cases, such as a class member? If not, it should. – snort Jan 14 '14 at 18:51
  • 1
    Duplicate? http://stackoverflow.com/questions/6062192/there-is-already-an-open-datareader-associated-with-this-command-which-must-be-c – Jeff Jan 14 '14 at 18:56
  • 1
    See this question: http://stackoverflow.com/questions/7927990/entity-framework-there-is-already-an-open-datareader-associated-with-this-comma – snort Jan 14 '14 at 18:57
  • in which line of code you get this error? – Lamloumi Afif Jan 14 '14 at 18:57
  • Where are you disposing of your context? From the snippets you are providing it doesn't look like you are, which is not a good idea. Any time you are dealing with connections to a database you really should be closing them. – Dismissile Jan 14 '14 at 19:02

1 Answers1

0

You need to add MultipleActiveResultSets=true; to your connection string.

DarthVader
  • 52,984
  • 76
  • 209
  • 300