我无法访问对象 [英] Im not able to access an object
问题描述
这是我的Configuration.cs代码
命名空间 DataAccess
{
公共 类连接
{
公共 静态 Social_NetwokingEntities GetContext()
{
Social_NetwokingEntities fdc = 新 Social_NetwokingEntities(ConfigurationManager.ConnectionStrings [" ].ToString());
返回 fdc;
}
}
}
使用该文件的另一个文件的代码是:
私有连接conn;
公共 AccountRepository()
{
conn = 新 Connection();
}
公共帐户GetAccountByID( int AccountID)
{
帐户account = 空;
使用(Social_NetwokingEntities dc = conn.GetContext())
{
account =(来自 dc中的>帐户选择 a).First();
}
返回帐户;
}
当我使用(Social_NetworkingEntities dc = conn.GetContext())键入
实例,请使用一种类型对其进行限定.
我不明白是什么问题..
请帮帮我.
谢谢.解决方案
我知道这不是您问题的答案,但希望您知道您的Linq语句可以大大改善:
account = dc.Account.FirstOrDefault(i => i.AccountID == AccountID);
不确定您真正的问题是什么.
原因是在Class
中
公共 class 连接
{
公共 静态 Social_NetwokingEntities GetContext()
{
Social_NetwokingEntities fdc = 新 Social_NetwokingEntities(ConfigurationManager.ConnectionStrings [" ].ToString());
返回 fdc;
}
}
GetContext
被声明为static
方法,因此可以通过Type Connection
而不是通过类Connection
的instance
进行访问.因此,将显示以上错误消息.
此外,我认为下面的代码可能足以代替问题中提到的第二个代码块
公共帐户GetAccountByID( int AccountID)
{
使用(Social_NetwokingEntities dc = Connection.GetContext())
{
返回 dc.Accounts.FirstOrDefault(a = > a.AccountID == AccountID);
}
}
Hi,
This is my code for Configuration.cs
namespace DataAccess
{
public class Connection
{
public static Social_NetwokingEntities GetContext()
{
Social_NetwokingEntities fdc = new Social_NetwokingEntities(ConfigurationManager.ConnectionStrings["FisharooConnectionString"].ToString());
return fdc;
}
}
}
and the code for another file using this file is :
private Connection conn;
public AccountRepository()
{
conn=new Connection();
}
public Accounts GetAccountByID(int AccountID)
{
Accounts account = null;
using (Social_NetwokingEntities dc = conn.GetContext())
{
account = (from a in dc.Accounts where a.AccountID == AccountID select a).First();
}
return account;
}
when i type
using(Social_NetworkingEntities dc = conn.GetContext())
i get an error "cannot be accessed by an instance, qualify it with a type.
i dont understand what is the problem..
please help me.
Thank you.
解决方案 I know this is not the answer to your question, but wanted you to know that your Linq statement could be much improved:
account = dc.Account.FirstOrDefault(i => i.AccountID == AccountID);
Not sure what your real problem is though.
The reason is that in the Class
public class Connection
{
public static Social_NetwokingEntities GetContext()
{
Social_NetwokingEntities fdc = new Social_NetwokingEntities(ConfigurationManager.ConnectionStrings["FisharooConnectionString"].ToString());
return fdc;
}
}
the GetContext
is declared as a static
method, so it can be accessed through the Type Connection
and not through the instance
of the class Connection
. Hence, the above error message is displayed.
Further, I think the following code may suffice instead of the second code block mentioned in the question
public Accounts GetAccountByID(int AccountID)
{
using (Social_NetwokingEntities dc = Connection.GetContext())
{
return dc.Accounts.FirstOrDefault(a => a.AccountID == AccountID);
}
}
这篇关于我无法访问对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!