We have an application that generates and compiles code on the fly using CSharpCodeProvider. The code that gets compiled uses Entityframework, so we pass a reference to Entityframework.dll to the compiler. The Entityframework.dll, which we redistribute with our application, is the one built for .net 4.0. We also pass a reference to System.ComponenModel.DataAnnotations.dll to the compiler. This is a reference to the dll found in the GAC.
All this works fine when running the application on .net 4.0. However, when running on .net 4.5 we get an issue, since some attributes that were in Entityframework in .net 4.0 have been moved to System.ComponenModel.DataAnnotations in .net 4.5. So we get errors like this:
Message=Error (CS0246): The type or namespace name 'Column' could not be found (are you missing a using directive or an assembly reference?) - file: c:\Generated\DataContext\0rs2sztj.2.cs:14 Error (CS0433): The type 'System.ComponentModel.DataAnnotations.Schema.ColumnAttribute' exists in both 'c:\Users\Steven Segers\AppData\Local\Temp\Temporary ASP.NET Files\vs\69fafaea\17b01629\assembly\dl3\95fc97d4\5bdae37d_7c05d101\EntityFramework.DLL' and 'c:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.ComponentModel.DataAnnotations\v4.0_4.0.0.0__31bf3856ad364e35\System.ComponentModel.DataAnnotations.dll' - file: c:\Generated\DataContext\0rs2sztj.2.cs:14
As far as I can see there are two ways out of this:
- Pass the reference assembly of System.ComponenModel.DataAnnotations for .net 4.0 to the compiler. Problem here is that this reference assembly needs to be present on the target machine. To accommodate this we need to either require the customer to install the Windows SDK on his server or distribute the reference assemblies with our application. I find the former a rather undesirable requirement, and I'm not sure if the latter is allowed.
- Make sure we pass the Entityframework.dll that targets the runtime .net version to the compiler. I'm not sure what would be the best way to accomplish this.