Decimal as Primary Key works in Dev (Win7/64bit) but not in Production (Win2008R2/64bit) Common Language Runtime detected an invalid program

StackOverflow https://stackoverflow.com/questions/12408014

Question

My Windows Service is a .NET application. The service has a dependency on my data access which uses EF 4.3 Code First. I am getting the following error when my service runs and attempts to access data.

Error occured in FullPurgeAndReplace(): System.InvalidProgramException: Common Language Runtime detected an invalid program. at System.Data.Entity.DynamicProxies.MOMInventoryItem_3ED5D5176D2C03867C62DD8E4381A882350CFD9CD931F3CD551623A6EF5C4D8E.set_Id(Decimal ) at lambda_method(Closure , Shaper ) at System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func2 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.Shaper1.SimpleEnumerator.MoveNext() at System.Collections.Generic.List1..ctor(IEnumerable1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable
1 source) ... more removed

On the SAME MACHINE I have a web application that depends on the same data access project and runs without issue. For that website in IIS I DO have Enable 32-bit applications checked for the respective Application Pool.

I have researched the problem and found that it MAY be related to the fact that the entity in the error (MOMInventoryItem) has a decimal primary key. I have no choice since I'm integrating with an existing system. However, that was supposedly a known issue with EF 4.0 from over a year ago and I would expect it to be resolved by now.

Here is some code from my Entity:

[Table("STOCK")]
public class MOMInventoryItem
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.None), Column("STOCK_ID")]
    public virtual decimal Id { get; set; }

Again, this works fine via an MVC app hosted in IIS but fails as a Windows Service, both on the same Windows 2008 R2 server. It also works on my DEV machine (Win7/VS11). What is my problem and how might I either resolve it permanently or work around it?

As always help is very much appreciated and reciprocated when possible.

Was it helpful?

Solution

Try set the startup project to target 32bits, that should prevent the apparent 64-bit issue. And explain why it runs fine with MVC.

OTHER TIPS

NOTE: The following is a workaround that worked for me BEFORE I had a better answer. The better answer was to compile the Windows Service project to target x86 instead of Any CPU. This still doesn't answer why Windows Server 2008 R2 is different then Win 7 but that's different question for a different day.

--

I found a workaround to my problem. I changed my Key to an int (since that's really what is being stored in the DB anyway, albeit technically a decimal column) and I explicitly provide the TypeName = "Decimal" in the Column attribute.

[Table("STOCK")]
public class MOMInventoryItem
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity), Column("STOCK_ID", TypeName = "Decimal")]
    public virtual int Id { get; set; }

In my case I never write to this table -- though I may need to in the future. I'm not 100% certain how this will impact writing rows but since the column is marked as DatabaseGenerated I'm assuming it won't be a problem.

I do think it's possible that if I target x86 instead of Any CPU then it might resolve it and I'll try that next as suggested by @leppie

Nonetheless, I am still curious to know why this would present this type of error -- Common Language Runtime detected an invalid program.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top