I keep getting an InvalidProgramException trying to grab data through the entity framework.
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!
}
The table is simple:
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`),
)
I don’t have this problem with other tables. What am I missing?
Exception: System.InvalidProgramException – “Common Language Runtime detected an invalid program.”
Stack Trace:
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
Edit
Solution, thanks to @Quintin below. The ‘id’ column in my entities was mapped to a decimal instead of an Int64. Not sure how it got this way, but problem is solved!
I assume you encountered a bug in EF, please see: http://connect.microsoft.com/VisualStudio/feedback/details/620031/invalidprogramexception-using-entityframework-poco-template-table-w-decimal-primary-key#