使用Entity的InvalidProgramException

时间:2012-04-06 02:20:35

标签: c# mysql entity-framework exception

我一直在尝试通过实体框架获取数据的InvalidProgramException。

using (var db = new Entities())
{
    var utrs = from u in db.users_to_roles
               select u; // utrs.Count() > 1 at this point. So, there IS data!

    users_to_roles utr = utrs.FirstOrDefault(); // InvalidProgramException!
    utrs.GetEnumerator(); // InvalidProgramException!
    utrs.ToList(); // InvalidProgramException!
}

表格很简单:

CREATE TABLE `users_to_roles` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `userid` bigint(20) NOT NULL,
  `roleid` bigint(20) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `fkuserid` (`userid`),
  KEY `FK22FCE45052C7D0B5` (`roleid`),
)

我对其他表没有这个问题。我错过了什么?

例外: System.InvalidProgramException - “公共语言运行时检测到无效程序。”

堆栈追踪:

 at System.Data.Entity.DynamicProxies.users_to_roles_026CF8D248576FA69379B3C621F372FAF9B6F2E4794C8490AE27BABE54609FF1.set_id(Decimal )
       at lambda_method(Closure , Shaper )
       at System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)
       at lambda_method(Closure , Shaper )
       at System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
       at System.Data.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
       at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
       at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)
       at Users.Lib.DAOs.Mysql.MysqlUserDAO.RemoveRoleFromUser(String email, String roleName) in C:\Work\Users.Lib\DAOs\Mysql\MysqlUserDAO.cs:line 242       

修改

解决方案,感谢下面的@Quintin。我的实体中的'id'列被映射到十进制而不是Int64。不知道怎么会这样,但问题解决了!