none
table names doesn't appear in the using Element. RRS feed

  • Question

  • I made 2 tables in MS SQL Server then i made 2 procedures for them to be displayed in visual studio 2017

    After i made the connection successfully in windows forms C#

    when i typed "Using" in From.cs 

    it was in that form :

    private void save_product_Click(object sender, EventArgs e)
            {
                using (OrascomEntities db = new OrascomEntities())
                {
                    db.Categories.cat_idColumn  //-- Only works in that form
    
                    Categories //-- when i type just the table name it doesn't recognize it.
                }
            }

    I'm pretty sure that it have to work like that because i do tht before.

    I don't know if the problem because of the connection or visual studio 

    I used to just type the table name inside using and it was working pretty fine


    • Edited by Kingkomand2o Wednesday, February 6, 2019 2:23 PM
    Wednesday, February 6, 2019 2:22 PM

All replies

  • db is defined as a local variable inside a using statement. Therefore it is only accessible inside the using statement. This is reasonable.

    Categories, as you specified it, must either be a member of the containing type or a type that is defined elsewhere otherwise it isn't defined. To access the "Categories" table of your entities context you'll have to create an instance of the entity and then access the table through the context. Your using statement is already doing that so in the very specific case of the code you posted you'd reference the "Categories" table as db.Categories. This is the correct behavior.

    private void save_product_Click(object sender, EventArgs e)
    {
       using (OrascomEntities db = new OrascomEntities())
       {
          db.Categories.cat_idColumn  //-- Only works in that 
    form
    
          //Not valid, Categories is a property on the context and therefore must be accessed through the context
          //Categories //-- when i type just the table name it doesn't recognize it.
    
          //Valid, can access the tables via the context - context is like a database connection - tables are only accessible for the life of the context
          db.Categories.
       }
    }

    If you need access to "Categories" outside the function you gave then you'll have to repeat the using statement logic. In the very specific case of using an ORM like EF you have to use the context to access the tables. The context cannot be left open indefinitely so each time you need access to the tables you open the context. This can be done once per request (for web apps) or once per event (in a Windows app) depending upon your needs. It is important to note that you cannot store the "table" reference somewhere else for later use because when the context goes away you lose access to the tables within it.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, February 6, 2019 2:57 PM
    Moderator
  • Did you also try Category instead of Categories?

    In order to investigate the types, select the Categories word in db.Categories and press <F12>.

    Wednesday, February 6, 2019 5:50 PM