The text of this article is not in this database — only its details are. The old site it was published on is gone, but the Internet Archive kept a copy: Generic Data Access using LINQ to SQL and C#
18 Comments
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.

Kuppurasu NagarajPosted Apr 24, 2016, 12:52 PM
Nice Sharing..
vijay bPosted Sep 10, 2015, 5:21 AM
Please help me if anyone has solution.
vijay bPosted Sep 10, 2015, 5:20 AM
i am getting following error while updating or deleting record.Error : "An entity can only be attached as modified without original state if it declares a version number or does not have an update check policy."
vijay bPosted Sep 10, 2015, 5:18 AM
Hello Scott, Thanks for nice example.
thai nhan nguyenPosted Oct 23, 2013, 3:20 AM
i can't attach sql in UPDATE. Can every one help me
Serguei DavidenkoPosted Apr 26, 2012, 5:03 AM
Interesting! Could you explain, is it true: Repository "contains static members, so it is difficult to unit test code that calls its members"? Could you recommend the method for unit testing?
diego urabayenPosted Oct 9, 2011, 8:03 PM
I have a InvalidOperationException in the Update method. When I using relational tables (with EntityRef or EntitySet) the method make an error in the Attach operation. Do you know what's happend? thanks.
v tPosted Apr 5, 2009, 1:47 PM
Hello again, Scott. I am using your code with DevExpress XtraGrid, and the SelectAll method wouldn't work with it. I am getting "...datacontext accessed after dispose...". (This probably happens when XtraGrid tries to get data for child views) The only way I found to fix this is by putting: context.DeferredLoadingEnabled = false; immediately after using(..context..) inside SelectAll. That way, all the related data is loaded during SelectAll, which I don’t like, but at least XtraGrid isn’t trying to reuse the disposed DataContext. Second issue was with Remove method - "An entity can only be attached as modified if it declares a version number or does not have an update check policy". I fixed this by replacing: table.Attach((T)newObj, true); with table.Attach((T)newObj, item); I haven't tested this much, but it seems to work for now. Thanks again for the code, it is still very helpful :)
Yasine LAKHDARIPosted Apr 3, 2009, 6:31 PM
Hello, first of all thanks for your interesting article about generic DLINQ. but i didn't found in your article Generic Queries i mean a method that has some parameters concerning the Colomn in the target table and its value and it return a set of objects within generic class of course, i think that 's very interresting and useful for code reuseability. Best Regards Thanks
v tPosted Mar 4, 2009, 12:52 PM
Exactly what I needed, thank you very much.
samar mouradPosted Nov 21, 2008, 8:22 AM
First,I wanna thank you about this wonderful example...I have downloaded this sample to test it.I've tried to insert an item with a customer id "ZOLA " and it worked perfectly but after that,when I try to delete it ,I was prompted by the following error: "object reference not set to an instance of an object" Do you have any ideas to resolve this issue? Thank you again.
Sanjay PatileditedPosted Oct 3, 2008, 2:59 AMEdited Oct 3, 2008, 3:03 AM
Hi Scott, This generic approach for writing DAL component is really helpful & it's cut down lots of Data Layer Code. Actually I am trying to implement the same concepts using ADO .NET Entity Framework (released in VS 2008 SP1) rather that LINQ but I stuck in some places that I couldn't find out the Entity class at run time since There is no GetTable() method in EntityContext class. So could you pl help me out in implementing the above soln using ADO .NET Entity Framework Sanjay
J GPosted Sep 8, 2008, 10:56 AM
Scott Thank you very much for this article - I am just now learning LINQ and this is the kind of nice, OO-based use I have been looking for. Many of the quick-and-dirty articles I have seen use way too much database-related code on the client side. This generic DAL approach is more like what I've grown accustomed to in the ADO.Net world. Thanks again for the contribution. Well written and easy to understand your examples.