I am new to .NET and LINQ to SQL.
So far, I have been pulling data and populating a gridview, and everything is working fine.
However, when I built my second page, I created a new DataContext (I assumed using one per page) when I referenced a table that I used in my previous page, it gave me an “Ambiguous reference” error because I had my table in both DataContexts. After removing the table from the second, I could reference the one from my first.
So, should I have just one context for my entire application or one for each table? Maybe something in between where I put the tables that I mostly use together in the same context?
What is the best practice?
In LINQ to SQL the DataContext serves as an abstraction of the database. If I’m understanding your question correctly, you had been using two separate classes that defined unique DataContexts per page? You need only one DataContext class that can be shared in the application. If you really wanted to, you could have multiple instances of the DataContext but I can’t think of when you’d want to do that.
If you have code of how you’re accessing the DataContext and tables it would help.
See the MSDN reference on the DataContext class – DataContext for more information.