4

I am developing an ASP.NET MVC application (Microsoft .NET Framework Version 3.5 SP1 using Entity Framework1.0). I have about 30 tables in my MySQL database. So far I have created 10 Model classes. I have an instance of the dataentity in all of my models.

Sample Model Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MyProj.Models
{
    public class SSDModel
private ddms_dataEntities2 db = new ddms_dataEntities2();

public ssd searchbyId(string id)
        {
            return db.ssd.FirstOrDefault(d => d.ssd_id_text == id);
        }

 public void add(ssd item)
        {
            db.AddTossd(item);
        }
}

When I try to access the method searchbyId() from another model class by instantiating an object of SSDModel, I was getting an exception - the objectcontext was not shared.

I am trying to find out the best way to share the object context between the model classes. I did go through SO and other sites for finding a solution. I understand the one of the best approaches would be to have one object context per HttpRequest. However, everything I found on the net is about entity-framework 4.0. I don't want to migrate the application to another version.

Please suggest a good document/blog/sample application that I can refer to. This is my first MVC application and any help would be greatly appreciated.

Thanks

user1819118
  • 71
  • 1
  • 1
  • 7

1 Answers1

2

You can pass the Context to the constructor when the SSDModel object created,

public class SSDModel
       private ddms_dataEntities2 db;

       public SSDModel(ddms_dataEntities2  context){
          db=context;
       }

       public ssd searchbyId(string id)
        {
            return db.ssd.FirstOrDefault(d => d.ssd_id_text == id);
        }

        public void add(ssd item)
        {
            db.AddTossd(item);
        }
}

And when you are going to initialize sevaral model classes just create one context and pass that for all constructors.

var ddms_dataEntities2 db = new ddms_dataEntities2();
SSDModel ssd=new SSDModel(db);
OtherModel other=OtherModel(db);

One easy way to keep the context per request is describerd here
Or else you can use IOC containers.

Community
  • 1
  • 1
Jayantha Lal Sirisena
  • 21,216
  • 11
  • 71
  • 92
  • Thank you so much for your response. I am able to pass the object context by creating a constructor as you had suggested. I want to know how this object context is disposed. In a multi user scenario is it a good approach to have each of the controller functions inside a using(ddms_dataEntities2 db = new ddms_dataEntities2()) block and call db.saveChanges() and db.dispose() in every controller function. I can instantiate the model classes inside the using() block as required. Please let me know. – user1819118 Nov 13 '12 at 22:35
  • Yes, you can do like that way. After the using block the context will be automatically disposed. And the recommended lifetime of the context is to have one per request. In multiplier environment it will work. – Jayantha Lal Sirisena Nov 14 '12 at 03:09
  • Thanks for your response. Here is what I have done to solve the problem: 1] Create an instance of the of the object context in my controller. 2] Create a constructor in the controller and instantiate the model classes by passing the context as suggested by Jayantha. I am not getting the error regarding sharing of object context. I am able to proceed. Thanks again Jayantha. Since I dont have enough points, I am not able to upvote. – user1819118 Nov 16 '12 at 19:39