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

  • SEARCH
  • Home
  • 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 6355459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:46:09+00:00 2026-05-24T22:46:09+00:00

I’ve recently started with Entity Framework on an MVC project I’m doing to get

  • 0

I’ve recently started with Entity Framework on an MVC project I’m doing to get the grips with the technologies and I’ve got a few questions : ..

My first one is this, this is my Save code for an entity – Is this the best way.. it looks a little suspect to me..

public bool SavePrank(PrankDefinition prank)
    {

        if (prank == null)
            throw new ArgumentNullException("prank");

        if (prank.ID == 0)
        {
            DataBase.Pranks.Add(prank);
            DataBase.SaveChanges();
        }
        else
        {
            DataBase.Pranks.Attach(prank);
            DataBase.Entry(prank).State = EntityState.Modified;
            DataBase.SaveChanges();
        }

        return true;

    }

Ive also got this code for getting the latest version of the entitys..

public List<PrankDefinition> GetPranks()
    {
        List<PrankDefinition> pranks = DataBase.Pranks.Where(p => p != null).ToList();

        foreach (PrankDefinition prankDef in pranks)
        {
            DataBase.Entry(prankDef).Reload();
        }

        return pranks;
    }

the reason ive had to call .reload on the entity’s is because when another client is using the project – there changes to entity’s are not reflected instantly (which is critical). My question for this – is there a better way to do this? is there something I can attach to the Where method to get the latest versions?

my context – if it helps..

    public static DataContext DataBase
    {
        get
        {
            if (HttpContext.Current != null &&            HttpContext.Current.Session["DataBase"]== null)
            {
                HttpContext.Current.Session["DataBase"] = new DataContext();
            }

            return HttpContext.Current.Session["DataBase"] as DataContext;
        }
        set
        {
            if (HttpContext.Current != null)
                HttpContext.Current.Session["DataBase"] = value;
        }
    }

any help would be fantastic!

EDIT : UPDATE TO DATA CONTEXT

Would this be a better implementation of the DataContext?

public static DataContext DataBase
    {
        get { return new DataContext(); }
    }

cheers.
ste.

  • 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-05-24T22:46:10+00:00Added an answer on May 24, 2026 at 10:46 pm

    Just a few remarks:

    • In my opinion SaveChanges finishes a unit of work and doesn’t belong to single repository methods like SavePrank. I would probably prefer a pattern like this:

      InsertOrUpdatePrank(prank); // = SavePrank without SaveChanges
      ModifyPerhapsSomeOtherEntity(otherEntity);
      SetPerhapsRelationshipBetweenPrankAndOtherEntity(prank, otherEntity);
      // ... more ...
      DataBase.SaveChanges();
      

      This way all changes are written to the database in a single transaction.

    • Where do you dispose your context? If you instantiate and store it in a session but don’t dispose the context in your code explicitely the context lives across multiple requests. This is a potential source of big trouble because your context might still contain entities from older requests when you process a new request. For example: If the prank with ID = 123 gets updated twice in two subsequent requests from the same user (in the same session) DataBase.Pranks.Attach(prank) would throw an exception because on old prank from a former requests with same ID is already attached to the context. In a web application a context should never live longer than a single request to avoid problems like this and many more.

    • If you dispose the context per request I don’t see the need anymore to “reload” entities. You have to load the entities from the database anyway because each context is a new one and empty when you enter an action. So, there is nothing to reload. You’ll get the latest version of your entites from the database anyway when you run your queries.

    • Where(p => p != null) makes no sense. To fetch all rows you can just write:

      List<PrankDefinition> pranks = DataBase.Pranks.ToList();
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

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.