0

我不断收到 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。不知道它是怎么做到的,但问题已经解决了!

4

1 回答 1

1

我假设您在 EF 中遇到了错误,请参阅:http ://connect.microsoft.com/VisualStudio/feedback/details/620031/invalidprogramexception-using-entityframework-poco-template-table-w-decimal-primary-key#

于 2012-04-06T02:57:51.113 回答