none
头痛,EntityFramework5.0运行一段时间后出错! RRS feed

  • 问题

  • 最近发现个头痛的问题,EntityFramework5.0运行一段时间后出错!

    程序是基于silverlight5.0的BS应用,服务端用的EntityFramework5.0进行数据库访问,客户端与服务端通讯用的是wcf ria service。
    重启IIS后,一切运行正常,但是运行一段时间,有时候几天,有时候4,5个小时,就会报错,如下:
    The 'Sender' property on 'Sys_Message' could not be set to a 'String' value. You must set this property to a non-null value of type 'Int32'.

    引发这个报错的代码如下:
    Sys_Message task = objContext.Sys_Message.Where(m => m.ID == TaskID).FirstOrDefault();


    也就是说这一切都是交给EntityFramework进行处理的,所以估计是EntityFramework运行一段时间后内部出现混乱,重启IIS后就又正常。不知道根本原因是什么,本来想升级到EntityFramework6.0,但是EntityFramework6.0又不支持wcf ria service,哎,坑爹的MS!

    哪位高手帮忙分析下,不胜感激!


    • 已编辑 a222222_ 2014年9月19日 3:23
    2014年9月19日 3:17

全部回复

  • 请问你的数据库数据是动态实时变化的吗?我的意思是:每隔一定时间自动会向数据库插入数据?

    ASP.NET Forum
    Other Discussion Forums
    FreeRice Donate
    Issues to report
    Free Tech Books Search and Download

    2014年9月21日 7:18
  • 是的,站点本身会进行数据库写入操作,还有个Windows服务,也会定时从其他数据库里同步数据到站点的数据库中。难道是这个原因吗?那我肯定要写入数据到数据库啊。求解
    2014年9月22日 7:57
  • 你应该设法测试一下插入数据库数据的时候是否在某些特殊情况下为空(看错误信息说明某个字段的Int值不能为空,但是实际上为空)。

    ASP.NET Forum
    Other Discussion Forums
    FreeRice Donate
    Issues to report
    Free Tech Books Search and Download

    2014年9月22日 8:11
  • 错误信息是说Int型字段插入了String类型的值,而且为什么重启IIS后又一切正常了呢?而且发现在重启IIS之前任何用LinQ访问数据库数据的代码都会报错,读不出数据来,重启IIS后这些代码都是工作的很好

    • 已编辑 a222222_ 2014年9月22日 11:54
    2014年9月22日 11:44
  • 1、确保你的String一定可以转化为Int。

    2、出错的时候打开数据库,仔细查看究竟哪个字段出问题了。


    ASP.NET Forum
    Other Discussion Forums
    FreeRice Donate
    Issues to report
    Free Tech Books Search and Download

    2014年9月23日 7:46
  • 报错的代码

    Sys_Message task = objContext.Sys_Message.Where(m => m.ID == TaskID).FirstOrDefault();

    也就是说报错的代码是查询,而不是插入,那数据库里的数据一定是Int了,因为int型字段不可能是String类型的数值,否则数据库怎么可能存储?所以根本还是EntityFramework的问题。

    再说如果是数据的问题,那重启IIS后怎么又可以查询了呢?

    2014年9月24日 0:59
  • 经过分析,发现原因了,同样的代码entityframework居然产生不一样的SQL语句。

    代码:IQueryable<Sys_User> users = objContext.Sys_User.Include("Sys_UserLogState").Where(u => u.Number == userName && !u.Forbidden);

    正常时生成的语句:

    exec sp_executesql N'SELECT
    [Project2].[ID] AS [ID],
    [Project2].[Number] AS [Number],
    [Project2].[Name] AS [Name],
    [Project2].[Password] AS [Password],
    [Project2].[PwdChangeDate] AS [PwdChangeDate],
    [Project2].[PwdExpireDays] AS [PwdExpireDays],
    [Project2].[Forbidden] AS [Forbidden],
    [Project2].[Description] AS [Description],
    [Project2].[GroupID] AS [GroupID],
    [Project2].[DomainUser] AS [DomainUser],
    [Project2].[EmpID] AS [EmpID],
    [Project2].[CreateDate] AS [CreateDate],
    [Project2].[SignPict] AS [SignPict],
    [Project2].[OpenID] AS [OpenID],
    [Project2].[C1] AS [C1],
    [Project2].[ID1] AS [ID1],
    [Project2].[State] AS [State],
    [Project2].[UserID] AS [UserID],
    [Project2].[Motion] AS [Motion],
    [Project2].[ActiveTime] AS [ActiveTime],
    [Project2].[Token] AS [Token]
    FROM ( SELECT
     [Limit1].[ID] AS [ID],
     [Limit1].[Number] AS [Number],
     [Limit1].[Name] AS [Name],
     [Limit1].[Password] AS [Password],
     [Limit1].[PwdChangeDate] AS [PwdChangeDate],
     [Limit1].[PwdExpireDays] AS [PwdExpireDays],
     [Limit1].[Forbidden] AS [Forbidden],
     [Limit1].[Description] AS [Description],
     [Limit1].[GroupID] AS [GroupID],
     [Limit1].[DomainUser] AS [DomainUser],
     [Limit1].[EmpID] AS [EmpID],
     [Limit1].[CreateDate] AS [CreateDate],
     [Limit1].[SignPict] AS [SignPict],
     [Limit1].[OpenID] AS [OpenID],
     [Extent2].[ID] AS [ID1],
     [Extent2].[State] AS [State],
     [Extent2].[UserID] AS [UserID],
     [Extent2].[Motion] AS [Motion],
     [Extent2].[ActiveTime] AS [ActiveTime],
     [Extent2].[Token] AS [Token],
     CASE WHEN ([Extent2].[ID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
     FROM   (SELECT TOP (1)
      [Extent1].[ID] AS [ID],
      [Extent1].[Number] AS [Number],
      [Extent1].[Name] AS [Name],
      [Extent1].[Password] AS [Password],
      [Extent1].[PwdChangeDate] AS [PwdChangeDate],
      [Extent1].[PwdExpireDays] AS [PwdExpireDays],
      [Extent1].[Forbidden] AS [Forbidden],
      [Extent1].[Description] AS [Description],
      [Extent1].[GroupID] AS [GroupID],
      [Extent1].[DomainUser] AS [DomainUser],
      [Extent1].[EmpID] AS [EmpID],
      [Extent1].[CreateDate] AS [CreateDate],
      [Extent1].[SignPict] AS [SignPict],
      [Extent1].[OpenID] AS [OpenID]
      FROM [dbo].[Sys_User] AS [Extent1]
      WHERE ([Extent1].[Number] = @p__linq__0) AND ([Extent1].[Forbidden] <> cast(1 as bit)) ) AS [Limit1]
     LEFT OUTER JOIN [dbo].[Sys_UserLogState] AS [Extent2] ON [Limit1].[ID] = [Extent2].[UserID]
    )  AS [Project2]
    ORDER BY [Project2].[ID] ASC, [Project2].[C1] ASC',N'@p__linq__0 nvarchar(4000)',@p__linq__0=N'admin'

    出问题时生成的语句:

    exec sp_executesql N'SELECT
    [Project1].[ID] AS [ID],
    [Project1].[Number] AS [Number],
    [Project1].[Name] AS [Name],
    [Project1].[Password] AS [Password],
    [Project1].[PwdChangeDate] AS [PwdChangeDate],
    [Project1].[PwdExpireDays] AS [PwdExpireDays],
    [Project1].[Forbidden] AS [Forbidden],
    [Project1].[Description] AS [Description],
    [Project1].[GroupID] AS [GroupID],
    [Project1].[DomainUser] AS [DomainUser],
    [Project1].[EmpID] AS [EmpID],
    [Project1].[CreateDate] AS [CreateDate],
    [Project1].[SignPict] AS [SignPict],
    [Project1].[OpenID] AS [OpenID],
    [Project1].[C1] AS [C1],
    [Project1].[ID1] AS [ID1],
    [Project1].[State] AS [State],
    [Project1].[UserID] AS [UserID],
    [Project1].[Motion] AS [Motion],
    [Project1].[ActiveTime] AS [ActiveTime],
    [Project1].[Token] AS [Token]
    FROM ( SELECT
     [Extent1].[ID] AS [ID],
     [Extent1].[Number] AS [Number],
     [Extent1].[Name] AS [Name],
     [Extent1].[Password] AS [Password],
     [Extent1].[PwdChangeDate] AS [PwdChangeDate],
     [Extent1].[PwdExpireDays] AS [PwdExpireDays],
     [Extent1].[Forbidden] AS [Forbidden],
     [Extent1].[Description] AS [Description],
     [Extent1].[GroupID] AS [GroupID],
     [Extent1].[DomainUser] AS [DomainUser],
     [Extent1].[EmpID] AS [EmpID],
     [Extent1].[CreateDate] AS [CreateDate],
     [Extent1].[SignPict] AS [SignPict],
     [Extent1].[OpenID] AS [OpenID],
     [Extent2].[ID] AS [ID1],
     [Extent2].[State] AS [State],
     [Extent2].[UserID] AS [UserID],
     [Extent2].[Motion] AS [Motion],
     [Extent2].[ActiveTime] AS [ActiveTime],
     [Extent2].[Token] AS [Token],
     CASE WHEN ([Extent2].[ID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
     FROM  [dbo].[Sys_User] AS [Extent1]
     LEFT OUTER JOIN [dbo].[Sys_UserLogState] AS [Extent2] ON [Extent1].[ID] = [Extent2].[UserID]
     WHERE ([Extent1].[Number] = @p__linq__0) AND ([Extent1].[Forbidden] <> cast(1 as bit))
    )  AS [Project1]
    ORDER BY [Project1].[ID] ASC, [Project1].[C1] ASC',N'@p__linq__0 nvarchar(4000)',@p__linq__0=N'admin'

     明显有差异,EntityFramework的重大bug啊,搞死人了!!!!!!!!!!!!

    2014年9月25日 2:35