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 6983659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:26:53+00:00 2026-05-27T18:26:53+00:00

I am trying to use Linq to Entity as my DAL for a new

  • 0

I am trying to use Linq to Entity as my DAL for a new project.

In my DAL I have this method to get the Job Entity

    public Job LoadJob(int id)
    {
        Job job = new Job();

        using (TNEntities context = new TNEntities())
        {
            var jobEntity = (from c in context.Jobs
                      where c.id == id
                      select c).First();

            job = (Job)jobEntity;                
        }

        return job;
    }

I use the Job entity in my program and then I would like to save it:

I have tried a few different things, but currenlt my method looks like this (it does not work)

    public void SaveJob(Job job)
    {
        using (TNEntities context = new TNEntities())
        {
            context.Jobs.AddObject(job);
            context.SaveChanges();
        }
    }

I have tried context.jobs.attach(job) which does not throw an error but it does not update my Job. I assume its because the Job is out of Context as its out of scope of the using context. But Im not sure how to re-attach it so it updates the job that I selected in my first method.

  • 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-27T18:26:54+00:00Added an answer on May 27, 2026 at 6:26 pm

    Ideally, you want to read your job from a context, make changes, then call SaveChanges on the same context you originally read it from. It might (should) be possible to attach the modified entity to a new context, and set its status to modified, but I’ve found modified child objects are mistreated with this approach.

    One of the easier approaches is to have all of these operations in a DataAccess object that has one instance of your TNEntities context, and uses it to read your job entity, and save changes.

    public class JobDao : IDisposable {
    
        TNEntities context = new TNEntities();
    
        public Job LoadJob(int id)
        {
           return this.context.Jobs.First(c => c.id == id);
        }
    
        public void Save(){
           this.context.SaveChanges();
        }
    
        public void Dispose(){
            this.Context.Dispose();
        }
    }
    

    (of course you’ll want to put a lot of that boilerplate code into a base class)

    using(JobDao dao = new JobDao()) {
       Job j = dao.LoadJob(12);
       j.JobTitle = "Software Developer";
       dao.Save();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get this sitemap clas s working. It appears to use LINQ,
I have been trying to use dynamic LINQ to Entity in my application for
Trying to use Linq to SQL for a small project I'm working on at
While trying to use LINQ in a .NET 2.0 Windows Forms project I got:
I'm trying to use linq to sql for my project (very short deadline), and
I am new to using LINQ. I am trying to use it in Silverlight
For the past days I've been trying to use Entity Framework and LINQ 2
Trying to use Linq with some Entity objects to do a crafty query here.
I am trying to use entity framework and get the date from the SQL
I'm trying to use LINQ to SQL to select a few specific columns from

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.