none
Data Context - Properties RRS feed

  • Question

  • Hi-- I'm learning LINQ to SQL, and at the moment I am experimenting with a simple data context class that declares one LINQ Table. The MS Hands-On-Lab that I am using as a guide declares the table as a public variable, and my data context works fine when I declare my table the same way. I don't like public variables in classes, so I changed the table declaration to a property. Now, when I run my app, I get a null argument exception when the LINQ query declaration is executed in my Main method.

     

    Can LINQ tables be declared as properties in a data context class, or do they have to be declared as public variables? If they can be declared as public variables, how would I have to change my code to make the declaration work? Thanks!

     

    Here is my data context class:

     

    Code Snippet

        public class LearningLinqDataContext : DataContext
        {
         #region Declarations

         // Public variables
            private Table p_TocItems;
       
         #endregion

         #region Constructor

            public LearningLinqDataContext(string connection) :
                base(connection)
            {
            }

         #endregion

            #region Properties


            public Table TocItems
            {
                get { return p_TocItems; }
                set { p_TocItems = value; }
            }

            #endregion
        }

     

     

    And here is my Main method:

     

    Code Snippet

            static void Main(string[] args)
            {
                // Create a data context
                LearningLinqDataContext dc = new LearningLinqDataContext(@"Data Source=.\sqlexpress;Initial Catalog=TempDb");
               
                // Query for TOC items
                var items =
                    from t in dc.TocItems
                    select t;

                foreach (TocItem tocItem in items)
                {
                    Console.WriteLine("ID={0}, Text={1}",
                    tocItem.ItemID.ToString(), tocItem.Text);
                }
                Console.ReadLine();
            }

     

     

    Monday, March 31, 2008 12:13 PM

Answers

  • Hi,

     

    your DataContext should be

    Code Snippet

        public class LearningLinqDataContext : DataContext
        {
         #region Constructor

            public LearningLinqDataContext(string connection) :
                base(connection)
            {
            }

         #endregion

            #region Properties


            public Table<TocItem> TocItems
            {
                get { return this.GetTable<TocItem>(); }
            }

            #endregion
        }

     

     

     

    You can even write this is your main method

    var items = from t in dc.GetTable() select  t;

     

    regards

    Philipp

    Monday, March 31, 2008 1:41 PM

All replies

  • Hi,

     

    your DataContext should be

    Code Snippet

        public class LearningLinqDataContext : DataContext
        {
         #region Constructor

            public LearningLinqDataContext(string connection) :
                base(connection)
            {
            }

         #endregion

            #region Properties


            public Table<TocItem> TocItems
            {
                get { return this.GetTable<TocItem>(); }
            }

            #endregion
        }

     

     

     

    You can even write this is your main method

    var items = from t in dc.GetTable() select  t;

     

    regards

    Philipp

    Monday, March 31, 2008 1:41 PM
  • Thank you, Phillip!

     

    Monday, March 31, 2008 2:48 PM