第一个查询的实体框架查询结果合并了! [英] entity framework query result of the first query getting merged !

查看:75
本文介绍了第一个查询的实体框架查询结果合并了!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好 


我正在从linq2sql转向EF4。在我的第一个方法,我从linq2sql转换为EF4  我正面临一个问题


似乎查询的结果被合并到另一个查询的查询结果中。


请注意,这是一个wcf方法,我正在检查客户端的结果。


这里是完整的功能代码。


 


  public  LoginResponse LoginAgent( int  accountID, string  loginID, string 密码, int  console)
{
尝试
{
V3CDataBaseDataContext context = new V3CDataBaseDataContext();
DataLoadOptions dlo = new DataLoadOptions();
dlo.LoadWith< Agent>(x => x.V3CAccount);
dlo.LoadWith< Agent>(x => x.AgentRoles);
context.LoadOptions = dlo;
DateTime serverTime = DateTime.UtcNow;
代理商代理= null ;
V3CAccount account = null ;
agent =( from ag in context.Agents
其中 ag.V3CAccount_ID == accountID&&
ag.Login_ID == loginID&&
ag.Login_Password == password
选择 ag).SingleOrDefault();
if (agent == null
{
agent = (来自 ag context.Agents
其中 ag.Login_ID == loginID&&
ag.Login_Password == password
&& ag.V3CAccount.IsMasterAccount
选择 ag).SingleOrDefault();
}

if (agent == null
throw new CustomException( new ThreeArguementAnonymousType (){P1 = accountID.ToString(),P2 = loginID,P3 = password},ExceptionTypes.InvalidCredentialsException);

if (console == ConsoleTypes.Admin)
{
if (agent.AgentRoles.Where(x => x.RoleName == " Admin" )。Count()< 1)
throw new CustomException( null ,ExceptionTypes.AgentNotInRoleFoundException);
}

account = context.V3CAccounts.Where(x => x.ID == accountID).SingleOrDefault();
if (account == null
抛出 new CustomException( null ,ExceptionTypes.InvalidAccountIDException);

if (account.ExpiryDate< DateTime.UtcNow)
throw new CustomException( null ,ExceptionTypes.AccountExpiredException);

// if(console == ConsoleTypes.Operator&& agent.LoginStatus == OnlineStatus.Online)
< span style ="color:Green"> //抛出新的CustomException(null,ExceptionTypes.AgentAlreadyLoggedInException);

string forwordIps = HttpContext.Current.Request.Headers ["X-Forwarded-For"];
string l_ReqestIPAddress = "" ;
RemoteEndpointMessageProperty clientprop = null ;

if (! string .IsNullOrEmpty(forwordIps))
{
string [] ipRange = forwordIps.Split(',');
l_ReqestIPAddress = ipRange [0];
}
else
{
var msgprops = OperationContext.Current.IncomingMessageProperties;
clientprop = msgprops [RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
l_ReqestIPAddress = clientprop.Address;
}

if (console == ConsoleTypes.Operator)
{
agent.LoginStatus = OnlineStatus 。线上;
agent.LastLoginedTime = serverTime;
agent.LoginedFromIPAddress = l_ReqestIPAddress;

列表<代理> loginedAgents = context.Agents.Where(x => x.V3CAccount_ID == accountID&& x.LoginStatus == OnlineStatus.Online).ToList();
if (loginedAgents.Count()> = agent.V3CAccount.MaxLoginedUsersAllowed&& agent.LoginStatus!= OnlineStatus.Online)
throw new CustomException( null ,ExceptionTypes。 MaxAgentLoggedInException);
if (!(loginedAgents.Where(x => x.Login_ID == loginID).Count()> 0))
{
List< LiveSession> currentLiveSessions = context.LiveSessions.Where(x => x.V3CAccount_ID == accountID)。ToList();
foreach var ls in currentLiveSessions)
{
context.LiveSessionMessages.InsertOnSubmit( new LiveSessionMessage(){Created_By = Actor.Service,Created_On = DateTime.UtcNow,LiveSessionID = ls.VisitorSessionID,Type = LiveSessionMessageType.updateVisitorButtonState,Sent = false });
}
}

}

string slidingExpirationPlainKey = agent.V3CAccount_ID.ToString( )+ " _"
+ agent.ID + " _" + DateTime.UtcNow.Ticks.ToString();
Cipher cypher = Cipher.Instance;
string slidingExpirationEncyptedKey = cypher.Encrypt(slidingExpirationPlainKey);
LoginResponse lr = new LoginResponse();
lr.agent = agent;
lr.key = slidingExpirationEncyptedKey;

//为每个活动会话插入updateVisitorButtonState消息,如果这是第一个运营商
List< LiveSession> aliveSessions = null ;


int currentLoginedAgentsCount = 0;
currentLoginedAgentsCount = OperatorMethods.GetOnLineOperatorsCount(context,accountID);
if (currentLoginedAgentsCount< 1)
{

aliveSessions =(来自 ls in context.LiveSessions
其中 ls.V3CAccount_ID == accountID && ls.State!= SessionState.OutOfSite
select ls).ToList();
foreach var ls in aliveSessions)
{
LiveSessionMessage updateVisitorButtonStateMsg = new LiveSessionMessage()
{
Created_By = agent.ID,
Created_On = DateTime.UtcNow,
LiveSessionID = ls.VisitorSessionID,
Sent = false
Type = LiveSessionMessageType.updateVisitorButtonState
};
context.LiveSessionMessages.InsertOnSubmit(updateVisitorButtonStateMsg);
}
}

context.SubmitChanges();
return lr;
}
catch (例外e)
{
CustomExceptionDetails temp = MiscMethods.HandleException(e, new 列表< object >( new object [] { new ThreeArguementAnonymousType(){P1 = accountID.ToString(),P2 = loginID,P3 =密码}} ));
throw new FaultException< CustomExceptionDetails>(temp);
}
}

解决方案


欢迎使用EF论坛!


在快速阅读您的帖子后,我认为示例代码基于LINQ to SQL而不是EF。 那么请您告诉我们问题在哪里? 在LINQ to SQL代码或EF代码? 


此外,您能否在某些控制台应用程序中进行一些测试?  WCF环境可能会使问题变得更加复杂。  如果我们正在开发一个控制台应用程序,我相信解决问题会更容易。


周末愉快!


谢谢


hello 

i am swtiching from linq2sql to EF4 . on my first method which i converted from linq2sql to EF4  i am facing a problem

it seems that the results of a query are getting merged into the query results of the another query.

plz note that , this is a wcf method and i am checking the results on the client end.

here is the full function code .

 

public LoginResponse LoginAgent(int accountID, string loginID, string password, int console)
 {
 try
 {
 V3CDataBaseDataContext context = new V3CDataBaseDataContext();
 DataLoadOptions dlo = new DataLoadOptions();
 dlo.LoadWith<Agent>(x => x.V3CAccount);
 dlo.LoadWith<Agent>(x => x.AgentRoles);
 context.LoadOptions = dlo;
 DateTime serverTime = DateTime.UtcNow;
 Agent agent = null;
 V3CAccount account = null;
 agent = (from ag in context.Agents
 where ag.V3CAccount_ID == accountID &&
 ag.Login_ID == loginID &&
 ag.Login_Password == password
 select ag).SingleOrDefault();
 if (agent == null)
 {
 agent = (from ag in context.Agents 
 where ag.Login_ID == loginID &&
 ag.Login_Password == password
 && ag.V3CAccount.IsMasterAccount
 select ag).SingleOrDefault();
 }

 if (agent == null)
 throw new CustomException(new ThreeArguementAnonymousType() { P1 = accountID.ToString(), P2 = loginID, P3 = password }, ExceptionTypes.InvalidCredentialsException);

 if (console == ConsoleTypes.Admin)
 {
 if (agent.AgentRoles.Where(x=>x.RoleName=="Admin").Count() < 1 )
 throw new CustomException(null, ExceptionTypes.AgentNotInRoleFoundException);
 }

 account = context.V3CAccounts.Where(x => x.ID == accountID).SingleOrDefault();
 if (account == null)
 throw new CustomException(null, ExceptionTypes.InvalidAccountIDException);
 
 if ( account.ExpiryDate < DateTime.UtcNow)
 throw new CustomException(null, ExceptionTypes.AccountExpiredException);

 //if (console == ConsoleTypes.Operator && agent.LoginStatus == OnlineStatus.Online)
 // throw new CustomException(null, ExceptionTypes.AgentAlreadyLoggedInException);

 string forwordIps = HttpContext.Current.Request.Headers["X-Forwarded-For"];
 string l_ReqestIPAddress = "";
 RemoteEndpointMessageProperty clientprop = null;
 
 if (!string.IsNullOrEmpty(forwordIps))
 {
 string[] ipRange = forwordIps.Split(',');
 l_ReqestIPAddress = ipRange[0];
 }
 else
 {
 var msgprops = OperationContext.Current.IncomingMessageProperties;
 clientprop = msgprops[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
 l_ReqestIPAddress = clientprop.Address;
 }

 if (console == ConsoleTypes.Operator)
 {
 agent.LoginStatus = OnlineStatus.Online;
 agent.LastLoginedTime = serverTime;
 agent.LoginedFromIPAddress = l_ReqestIPAddress;

 List<Agent> loginedAgents = context.Agents.Where(x =>x.V3CAccount_ID == accountID && x.LoginStatus == OnlineStatus.Online).ToList();
 if (loginedAgents.Count() >= agent.V3CAccount.MaxLoginedUsersAllowed && agent.LoginStatus!= OnlineStatus.Online)
 throw new CustomException(null, ExceptionTypes.MaxAgentLoggedInException);
 if (!(loginedAgents.Where(x => x.Login_ID == loginID).Count() > 0))
 {
 List<LiveSession> currentLiveSessions = context.LiveSessions.Where(x => x.V3CAccount_ID == accountID).ToList();
 foreach (var ls in currentLiveSessions)
 {
 context.LiveSessionMessages.InsertOnSubmit(new LiveSessionMessage() { Created_By = Actor.Service, Created_On = DateTime.UtcNow, LiveSessionID = ls.VisitorSessionID, Type = LiveSessionMessageType.updateVisitorButtonState, Sent = false });
 }
 }
 
 }

 string slidingExpirationPlainKey = agent.V3CAccount_ID.ToString() + "_"
 + agent.ID + "_" + DateTime.UtcNow.Ticks.ToString();
 Cipher cypher = Cipher.Instance;
 string slidingExpirationEncyptedKey = cypher.Encrypt(slidingExpirationPlainKey);
 LoginResponse lr = new LoginResponse();
 lr.agent = agent;
 lr.key = slidingExpirationEncyptedKey;

 // insert updateVisitorButtonState message for each active session if this was the first loged in operator
 List<LiveSession> aliveSessions = null;


 int currentLoginedAgentsCount = 0;
 currentLoginedAgentsCount = OperatorMethods.GetOnLineOperatorsCount(context, accountID);
 if (currentLoginedAgentsCount < 1)
 {

 aliveSessions = (from ls in context.LiveSessions
 where ls.V3CAccount_ID == accountID && ls.State != SessionState.OutOfSite
 select ls).ToList();
 foreach (var ls in aliveSessions)
 {
 LiveSessionMessage updateVisitorButtonStateMsg = new LiveSessionMessage()
 {
 Created_By = agent.ID,
 Created_On = DateTime.UtcNow,
 LiveSessionID = ls.VisitorSessionID,
 Sent = false,
 Type = LiveSessionMessageType.updateVisitorButtonState
 };
 context.LiveSessionMessages.InsertOnSubmit(updateVisitorButtonStateMsg);
 }
 }

 context.SubmitChanges();
 return lr;
 }
 catch (Exception e)
 {
 CustomExceptionDetails temp = MiscMethods.HandleException(e, new List<object>(new object[] { new ThreeArguementAnonymousType() { P1 = accountID.ToString(), P2 = loginID, P3 = password} }));
 throw new FaultException<CustomExceptionDetails>(temp);
 }
 }

解决方案

Hi,

Welcome to EF forum!

After a quick read of your post, I think the sample codes are based on LINQ to SQL instead of EF.  So could you please let us know where is the issue?  At the LINQ to SQL codes or at the EF codes? 

Besides, could you please do some test in some console app?  The WCF enviornment may get the issue be more complicated.   If we are working on a single console app, I believe it would be easier to troubleshoot the problem.

Have a nice weekend!

Thanks


这篇关于第一个查询的实体框架查询结果合并了!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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