C# Entity Framework 4 Common Language Runtime dete

2019-02-17 01:24发布

问题:

How do you debug/fix a "Common Language Runtime detected an invalid program" error? What exactly does it mean anyway?

I have a C# MVC 2 web app that can deployed to two websites that reside on the same IIS 7.5 webserver (x64). One is the live site (deployed using Release configuration), the second is the beta site (deployed using a new Beta configuration created just for this project).

The two websites are:

Default Website/my_app
Beta/my_app

On the beta site when selecting a paged list of purchase orders, it throws the "detected an invalid program" exception. The exact same code when run on the live site works perfectly. Why would it do this?

Edit: I installed Visual Studio on the server and found the actual line that was causing the problem and the stack trace:

var list = ObjectContext.ObjectSet.AsQueryable();
int totalRecords = list.Count();
var paged = list.Skip((page > 0 ? page - 1 : 0) * rows).Take(rows);

And this is the exception message with stack trace:

{System.InvalidProgramException: Common Language Runtime detected an invalid program.
   at System.Data.Entity.DynamicProxies.PurchaseOrderListVie_96479BFE9FA60F4C53137C56C1A1B2A11D90FF5AFFDC20383CC68E0A750792E3.set_Total(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.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at MyApp.Controllers.PurchaseOrderController.GetPurchaseOrderList(Int32 page, Int32 rows, String sidx, String sord) in C:\src\MyApp.2010\MyApp.UI\Controllers\PurchaseOrder\List.cs:line 11}

This new info shows exactly where the problem is, but not what the problem is. Hopefully someone who knows Entity framework very well can shed light on this:

System.Data.Entity.DynamicProxies.PurchaseOrderListVie_96479B_etc.set_Total(Decimal )

Is the line where the error occurs. Now I ran the query in sql management studio and the result was not null, and Total was not null either. So why did it have a problem calling set_Total()?

This is how the POCO defines the Total field (generated by a T4 template):

[Decimal] [Required] [DisplayName("Total")]
public virtual decimal Total
{
    get;set;
}

The main difference between the live and beta sites is the build configuration. But both of the configurations have every single project set to "Any CPU".

All our development machines and servers are 64 bit. Could there be some difference between the IIS configuration of the websites that is causing this?

I've tried running PEVerify - but it just says "All Classes and Methods Verified." How can PEVerify help with this type of problem?

BTW I can see that there are around 15 questions with "Common Language Runtime detected an invalid program problem" in the title. My question is not a duplicate and has several unique features that are different from the other questions that have a similar title (and only one of those 15 is about Entity Framework too - the rest are about Reflection or TFS)

回答1:

I ran into this issue today when I deployed a web application implementing linq to entity 4 to our QA environment. The issue turned out to be an IIS setting on the Windows 2008 R2 server. Under Advanced Settings for the application pool, the setting for Enable 32-bit Applications was set to False. I set it to True and now my application works just as it did on the development server which happened to be a Windows 2003 server. I hope this helps.



回答2:

For me this happened when I added a view to my entity model. By default the designer sets all the columns Entity Key property to true. When I set it to false for all decimal/numeric columns the error goes away. Tas



回答3:

Have you tried to deploy the beta config on another machine? Are your apppools setup to be the same (i.e., classic vs integrated, same .net version)? Have you tried cleaning the solution and redeploying to a new location? Have you tried deploying beta build to release location? (be sure to delete all files before publishing; im curious if theres a leftover dynamically loaded dependency that might be causing problems)

UPDATE:

Excellent wrt more information. On line 3, you are defining the variable page while also using a previously defined variable named page. How does that compile? Try commenting out that code or at least try it without the skip.

NOTE: I think that the Count() followed by the Take() might be executing the query twice.

NOTE2: I've only used the EntityFramework v4 Database First development, but I don't remember programming directly against the ObjectSet. Usually it's your entity class (e.g., MyContext.Orders)... Maybe there is something going on with programming against that object and setting a Decimal value. Are there any properties on the model for that property that make it non-standard?



回答4:

there is a major bug!!, i think it is while having decimal field column property in Entity framework,

i have faced this nasty error msg, i compared getting data from 2 separate tables(same tables),one with an integer column ID and the other with decimal column ID,

for the table with the integer column, no problem same code ran perfectly, for the table with a decimal column, this error message pop out i have made checks on sql server iis and the environment and i even wanted to format the pc, its very weird and the error msg is not clear.

hope this approach helps any facing such an error in the future.