实体框架问题在会话中存储数据 [英] Entity Framework problem storing data in session

查看:89
本文介绍了实体框架问题在会话中存储数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用实体框架问题来获取会话中的数据。

I am using entity framework problem in getting data in session.

var res = from a in ef.Logins where a.username == txtuser.Text && a.password == txtpass.Text select a.login_id;
Session["rcode"]=res;

if(Session["rcode"]!=null)
{
Abc obj=(Abc)Session["rcode"];
lable.text=obj.loginid;/// con not show data in lable
}



错误信息是:


Error message is:

System.Data.Objects.ObjectQuery`1[System.String]

推荐答案

尝试像

Try something like
var res = from a in ef.Logins where a.username == txtuser.Text && a.password == txtpass.Text select a.login_id;
string loginID= res.SingleOrDefault();

//期待a.login_id是字符串数据类型。



然后赋值相应的会话和访问。



希望这可以帮助你

//expecting a.login_id is string Datatype.

Then assign the value to Session and access accordingly.

Hope this helps you


你试过这个

会话[rcode] = res.ToString();
have you tried this
Session["rcode"]=res.ToString();


看看我们可以用不同的方式修改它

如果您需要登录ID,那么按数据类型选择a.login_id
See we can modify it in different ways
IF you need login id then by the data type "select a.login_id"
<pre lang="cs">
int res =Convert.ToInt32( from a in ef.Logins where a.username == txtuser.Text && a.password == txtpass.Text select a.login_id);
Session["rcode"]=res;

if(Session[&quot;rcode&quot;]!=null)
{ 
lable.text=Session["rcode"].ToString(); 
}</pre>





然后如果您需要在会话中保留整个登录课程

以下代码



Then If you need to keep whole login class in Session
Code below

Login res = (from a in ef.Logins where a.username == txtuser.Text && a.password == txtpass.Text select a).FirstOrDefault();
Session["rcode"]=res;
 
if(Session["rcode"]!=null)
{
Login obj=(Login )Session["rcode"];
lable.text=obj.loginid;/// con not show data in lable
}


这篇关于实体框架问题在会话中存储数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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