views:

857

answers:

2

Hi

i am facing some problems in my project. when i try to update entity it gives me different type of errors.

i read from net. these errors are because

1 - I am getting Object of entity class from method which creates DataContext locally

and in update method id does not update because here another DataContext is created locally. (even it does not throw any exception)

i found many articles related to this problem

1 - Adding timestamp column in table (does not effect in my project. i tried this)

one guy said that use SINGLE DataContext for everyone.

i did this by creating the following class

public class Factory
    {
        private static LinqDemoDbDataContext db = null;

        public static LinqDemoDbDataContext DB
        {
            get 
            {
                if (db == null)
                    db = new LinqDemoDbDataContext();

                return db;
            }
        }
    }





public static Student GetStudent(long id)
    {
        LinqDemoDbDataContext db = Factory.DB;

        //LinqDemoDbDataContext db = new LinqDemoDbDataContext();

            Student std = (from s in db.Students
                          where s.ID == id
                          select s).Single();

            return std;

    }



 public static void UpdateStudent(long studentId, string name, string address)
        {
            Student std = GetStudent(studentId);

            LinqDemoDbDataContext db = Factory.DB;

            std.Name = name;
            std.Address = address;

            db.SubmitChanges();
        }

in this case i want to update student details.

it solved my problem. but now the question is.

Is it good approach to use above technique in Web Based application???

A: 

The factory is a good approach as it gives you the opportunity to re-use an existing connection and will also create a new connection on demand when required.

James
this is fine. but in Web based application is it good approach to use static DataContext??
Mohsan
this should help you out http://blog.codeville.net/2007/11/29/linq-to-sql-the-multi-tier-story/
James
In terms of a web application I would probably just create a DataContext per request.
James
@James : but it does not concurrency conflicts:(
Mohsan
That wasn't what the question was, you asked if the approach you were using was good...however, if you want to know how to handle concurrency have a read here http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2008/07/01/10557.aspx He also does a few good video tutorials...James.
James
A DataContext should be used and disposed of in an atomic fashion. Each method should instantiate and then throw away the DataContext when it is done.
Simucal
+5  A: 

Is it good approach to use above technique in Web Based application???

No. DataContext is not thread safe. You cannot share 1 DataContext among the different threads handling different requests safely.

Also - this pattern is called Singleton, not Factory

David B
+1 - This article also describes some of the pitfalls and reasons why not to use the "global" datacontext solution: http://blog.codeville.net/2007/11/29/linq-to-sql-the-multi-tier-story/
Jon Smock