表名不会出现在using元素中。 [英] table names doesn't appear in the using Element.

查看:77
本文介绍了表名不会出现在using元素中。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MS SQL Server中创建了2个表,然后我在Visual Studio 2017中显示了2个程序

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

在Windows窗体中成功建立连接C#

After i made the connection successfully in windows forms C#

当我输入"使用"时在From.cs 

when i typed "Using" in From.cs 

它是以这种形式:

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.

我不知道是否因为连接或Visual Studio而出现问题 

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

推荐答案

db被定义为using语句中的局部变量。因此,它只能在using语句中访问。这是合理的。

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

如您所指定的那样,类别必须是包含类型的成员或在别处定义的类型,否则它未定义。要访问"类别",在您的实体上下文表中,您必须创建实体
的实例,然后通过上下文访问该表。你的using语句已经这样做,所以在你发布的代码的特定情况下,你引用了"类别"。表作为db.Categories。这是正确的行为。

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.
   }
}

如果您需要访问"类别",在你给出的函数之外,你将不得不重复使用using语句逻辑。在使用像EF这样的ORM的特定情况下,您必须使用上下文来访问表。上下文不能无限期地打开
,因此每次需要访问表时都会打开上下文。根据您的需要,每个请求(对于Web应用程序)或每个事件(在Windows应用程序中)可以执行一次。请务必注意,您无法存储"表格"。
引用其他地方供以后使用,因为当上下文消失时你将无法访问其中的表。

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.


这篇关于表名不会出现在using元素中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆