Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8825837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:58:35+00:00 2026-06-14T06:58:35+00:00

I am developing an ASP.NET MVC application (Microsoft .NET Framework Version 3.5 SP1 using

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:58:37+00:00Added an answer on June 14, 2026 at 6:58 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a ASP.NET MVC 3 application, i am using entity framework code
I am developing an ASP.Net MVC 3 application using Entity Framework 4.1. For a
I am developing an ASP.Net MVC 3 application using Entity Framework 4.1. For one
I am developing an ASP.Net MVC 3 Web application using Entity Framework 4.1. I
I am developing an ASP.NET MVC 3 application using C# and Razor. I have
I have just started developing a full-web application by using the ASP .NET MVC
I have been developing an asp.net mvc application where i need to make large
I am developing an application using ASP.NET MVC 1 and VS2008. When I deploy
I am developping a web application by using the ASP .NET MVC 3 framework.
I have an Asp.Net MVC Web Application that I am developing. I have TeamCity

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.