Entity Framework version error in my small code first project - asp.net-mvc-3

I'm writing a small test project in order to get my feet wet with EF code first. Unfortunately, when I try compiling, I get the following error:
Assembly 'Backend, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' uses
'EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
which has a higher version than referenced assembly 'EntityFramework, Version=4.1.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089' c:\Users\Kevin\Documents\Visual
Studio 2010\Projects\CFTest\Backend\bin\Debug\Backend.dll CFTest
For some reason, there's a versioning conflict, but I dunno how to fix it.
EDIT: My App.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="Data Source=.\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
</configuration>

My backend project had one version of EF while my MVC project came with another by default. Solved.

It is conflict with the version of the .NET framework your project is using, and the version of the .NET Framework that the EF dll was compiled in. If you go to the add reference window for the project, the listing should say what .NET Framework version each DLL uses. Change you project target to use that version as well.

Related

How Do I Configure my Web App to Work with Oracle 12c Release 3 (12.1.0.1.0)?

Thanks for looking
I have written an app that uses Entity Framework 6 which is supposedly supported by Oracle Data Provider 12c R3.
I have installed this version of the ODP, but I am having trouble making it work. I have tried following the configuration instructions, yet I still get this error when my code tries to access any Oracle entities:
0152: No Entity Framework provider found for the ADO.NET provider with invariant name 'Oracle.DataAccess.Client'.
NOTE: I am aware that there are many other StackOverflow questions on this subject, but most of them are older and do not take into consideration the newer version of ODP which DOES support EF 6.
Any help is appreciated.
I finally was able to to use ODP with EF6.
I did the following to make it work :-
First Installing ODAC 12c Release 3 which includes support for Entity Framework 6 Code First and Code First Migrations; NuGet, .NET Framework 4.5.2; and ODP.NET, Managed Driver XML DB. As per
http://www.oracle.com/technetwork/topics/dotnet/whatsnew/index.html
Adding two references , to my project references and they are :
Oracle.ManagedDataAccess.dll
Oracle.ManagedDataAccess.EntityFramework.dll
Installing EF6.1.1 using NuGet by running the following command in Package Manager Console( you can enter it by Tools->NuGet Package Manager -> Package Manager Console):
Install-Package EntityFramework -Version 6.1.1
And modify your web.config or web.config to use Oracle.ManagedDataAccess , by adding Provider and a valid connection string eg :
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<section name="Oracle.ManagedDataAccess.Client" type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>
<entityFramework>
<contexts>
<context type="App.Context.Default, App.Context">
<databaseInitializer type="MyProject.Context.Config.ContextInitializer, MyProject.Context" />
</context>
</contexts>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="Oracle.ManagedDataAccess.Client" type="Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=6.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings>
<add name="Default" providerName="Oracle.ManagedDataAccess.Client" connectionString="DATA SOURCE=XE;USER ID=User" />
</connectionStrings>
Rebuild your Application as x86, and start using EF6 , you can check if it works by adding a model using ADO.Net Entity Model using Code First

AjaxControlToolkit Version=4.1.60501.0 - Error: AjaxControlToolkit requires ASP.NET Ajax 4.0 scripts

I'm getting this error:
Error: AjaxControlToolkit requires ASP.NET Ajax 4.0 scripts. Ensure the correct version of the scripts are referenced. If you are using an ASP.NET ScriptManager, switch to the ToolkitScriptManager in AjaxControlToolkit.dll.
I've searched Hi and Low and can not resolve this. When I went to VS2008 I had the same issue and resolved it with changing my web.config
Now I ma running VS 2012 express. I've installed AjaxControlToolkit version 4.1.60501.0
Web.config:
<compilation debug="false" targetFramework="4.5">
<assemblies>
<add assembly="AjaxControlToolkit, Version=4.1.60501.0, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"/>
<add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
</assemblies>
and
<controls>
<add tagPrefix="cc1" namespace="AjaxControlToolkit" assembly="AjaxControlToolkit"/>
</controls>
In ASPX I have:
<cc1:ToolkitScriptManager ID="ScriptManager1" runat="server">
</cc1:ToolkitScriptManager>
And I'm getting the above error. I know the Toolkit DLL is the correct version.
I am also running VS2008 / Ajax 3.5 on the same machine, but I cleared out the Temporary ASP.Net Files folder of all other projects and still have the issue. When I run this new VS2012 project, a ROOT folder is created in the in the Temp folder and the AjaxControlToolkit.DLL in that folder is version 4.1.60501.0
What needs to change?
Thanks Much,
Jim

Installed MVC 4 Beta, now my MVC 3 projects throw an error when I rebuild all

I just installed MVC 4 Beta, and now my MVC 3 projects (that I haven't converted yet to 4) are throwing an error when I rebuild all:
Error 18 [A]System.Web.WebPages.Razor.Configuration.HostSection cannot be cast to [B]System.Web.WebPages.Razor.Configuration.HostSection. Type A originates from 'System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Razor\v4.0_1.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'. Type B originates from 'System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Razor\v4.0_2.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'.
Has anyone come across this yet? Thanks!
I've spent the entire day trying to fix this, and after an afternoon systematically ripping EVERYTHING out of my project except one basic view and one basic controller it was still throwing this error. So I started deleting other stuff and found there was a rogue Web.Config file in my Views folder that had MVC 3 stuff in it.
Search your project for other Web.Config files!!!
I had the same issue, and was able to solve it as follows:
Remove the assembly System.Web.WebPages.Razor from the project references.
Press Save All on the solution
Add the assembly again, make sure you select version 1.0.0.0. Select True for Specific Version in the reference properties.
I got an solution for this:
You need to add this to your mvc 3 web.config:
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.WebPages" />
<!-- Your namespace here -->
</namespaces>
</pages>
</system.web.webPages.razor>
And also copy the binfiles from
C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies
C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies
to your project binfolder
If you need deploy your mvc 4 project on a server that you dont have installed mvc 4 on you need to the same but change version from 1.0.0.0 to 2.0.0.0 in the section part.
Good luck!
The steps to fix this issue
First find all web.config files in whole project
In all web.config file, find the given 'cannot be cast' error code and change version 2.0.0.0 in configSections of all web.config files
System.Web.WebPages.Razor.Configuration.HostSection
again if the error comes then find that error code and change the version 2.0.0.0 in all web.config files. repeat the process until fix the version related issues.
When you install MVC4, your mvc3 projects will point to system.web.webpages ver 2.0..
For you MVC3 projects, Remove the reference to system.web.webpages ver 2.0.. and add a reference back to system.web.webpages ver 1.0..
Recompile and redeploy and it should work again...
Not sure why it does that, but it's still a beta install
Just in case someone else runs into this same problem. It looks like there are a few different things that can cause this particular error. This is the best description of what is causing the error that I've found. In my specific case, I added a reference to a library via NuGet, and it looks as if a specific reference to System.Web.WebPages was added to my project. I removed this reference, rebuilt, and everything was good to go.
Good luck to others with similar problems!
Make sure that the references to System.Web.WebPages is 2.0.0.0 in root/Web.config and root/Views/Web.config
If every thing is pointing to version 2 then verify if you have the directory "_bin_deployableAssemblies".
If you have it, then right click on your project and choose the option "Add Deployable Dependencies..." then check MVC and Razor options to update your DLLs.
Might help someone:
Added correct version of DLL conlfic between razor 1.0 and 2.0
Modified the web.config file where it says 3.0 to 4.0 and 1.0 to 2.0 only for MVC and razor
Another web.config hidden under view that also need to be modified.
Above three steps helped to resolve the issue.
The problem it's that you may have another web.config in the Views directory that have recerences pointing to the old asemblies. When the project loads, It tries to load both versions and then fails.
Try to also correct the versions on the "View's" web.config.
For the oncoming, like me.
That looks like you may have to update some of the assembly data in your web.config.
It looks Like We need to change the version number, if we upgrade to mvc4 from mvc3.
I just changed the webconfig, which is in Views Folder.... Need to change the version number
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
</system.web.webPages.razor>
<pages
validateRequest="false"
pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=**4**.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=**4**.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=**4**.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<controls>
<add assembly="System.Web.Mvc, Version=**4**.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
</pages>
</system.web>

Migrating my MVC 3 application to MVC 4

I really do not know what to do, I'm following this article that shows how to migrate my MVC 3 application manually .
I followed all the steps but when running my application the following error occurs:
Server Error in '/' Application.
Could not load file or assembly 'System.Web.WebPages.Razor,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or
one of its dependencies. The located assembly's manifest definition
does not match the assembly reference. (Exception from HRESULT:
0x80131040)
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.
Exception Details: System.IO.FileLoadException: Could not load file or
assembly 'System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The
located assembly's manifest definition does not match the assembly
reference. (Exception from HRESULT: 0x80131040)
Source Error:
An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location of
the exception can be identified using the exception stack trace below.
Assembly Load Trace: The following information can be helpful to
determine why the assembly 'System.Web.WebPages.Razor,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
could not be loaded.
WRN: Assembly binding logging is turned OFF. To enable assembly bind
failure logging, set the registry value
[HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1. Note: There
is some performance penalty associated with assembly bind failure
logging. To turn this feature off, remove the registry value
[HKLM\Software\Microsoft\Fusion!EnableLog].
Stack Trace:
[FileLoadException: Could not load file or assembly
'System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The
located assembly's manifest definition does not match the assembly
reference. (Exception from HRESULT: 0x80131040)]
Microsoft.Web.Samples.PreApplicationStartCode.Start() +0
[InvalidOperationException: The pre-application start initialization
method Start on type Microsoft.Web.Samples.PreApplicationStartCode
threw an exception with the following error message: Could not load
file or assembly 'System.Web.WebPages.Razor, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its
dependencies. The located assembly's manifest definition does not
match the assembly reference. (Exception from HRESULT: 0x80131040).]
System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1
methods) +423
System.Web.Compilation.BuildManager.CallPreStartInitMethods() +306
System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager
appManager, IApplicationHost appHost, IConfigMapPathFactory
configMapPathFactory, HostingEnvironmentParameters hostingParameters,
PolicyLevel policyLevel, Exception appDomainCreationException) +677
[HttpException (0x80004005): The pre-application start initialization
method Start on type Microsoft.Web.Samples.PreApplicationStartCode
threw an exception with the following error message: Could not load
file or assembly 'System.Web.WebPages.Razor, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its
dependencies. The located assembly's manifest definition does not
match the assembly reference. (Exception from HRESULT: 0x80131040).]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9079340
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +97
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest
wr, HttpContext context) +256
Version Information: Microsoft .NET Framework Version:4.0.30319;
ASP.NET Version:4.0.30319.237
I looked through the code, but I see no reference to System.Web.WebPages.Razor in version 1.0 version 2.0 only.
Use NuGet and add the package Microsoft.AspNet.Mvc (Microsoft Asp.Net Mvc 4). This should upgrade your references. See:
https://nuget.org/packages/Microsoft.AspNet.Mvc
You will need to upgrade the assembly references in your web.config.
Remove from the root web.config:
<assemblies>
<add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
Add to the root web.config:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Views/Web.Config:
Change Razor from 1.0.0.0 to 2.0.0.0:
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages validateRequest="false"
pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<controls>
<add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
</pages>
Did you forget to update the references in Views\Web.Config? The references in it is used to build the view pages.
I assumed you have MVC3 Intranet web application, when upgrade from MVC3 to MVC4 using NuGet, your application now references System.Web.WebPages.Razor Version 2.0.0.0, NOT System.Web.WebPages.Razor Version 1.0.0.0 anymore. Here is my way of resolving the reference.
There are two places to fix:
In the root web.config,
<compilation debug="true" targetFramework="4.0">
<assemblies>
. . .
<add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
. . .
</assemblies>
</compilation>
change Version=1.0.0.0 to Version=2.0.0.0
So your new reference will look like this:
<compilation debug="true" targetFramework="4.0">
<assemblies>
. . .
<add assembly="System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
. . .
</assemblies>
</compilation>
In the Views folder, there is web.config file. Inspecting this file you will see it references Version 1. Replace the whole web.config file in Views folder with a new web.config file. Make a backup of the old web.config file, just in case. To get a brand new web.config for the Views folder, you create a new MVC4 application for intranet. Go to the Views folder. Copy the Web.Config file from the Views folder of the new application to the Views folder of the broken application.
Hope it helps!
Reinstalling the AspNetSprite packages as suggested above does not solve the problem for me. But adding theses lines on the runtime\assemblyBinding section of the root web.config file works :
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages.Razor" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
Have you globally searched for System.Web.WebPages.Razor, Version=1.0.0.0 in your entire solution? Do you still reference System.Web.WebPages.Razor (Version 1.0.0.0)? Do you reference System.Web.WebPages.Razor Version 2.0.0.0?
[Update] I finally ran into the same issue. It seems like one of the NuGet packages I installed and uninstalled didn't entirely remove the reference to System.Web.Razor it used. Cleaning the solution didn't help because the DLL wasn't used directly by the web application. I finally got it to work by manually deleting the entire bin directory ...
Check the web.config inside your views folder.
UPDATE:
This bit looks suspcious.
Microsoft.Web.Samples.PreApplicationStartCode
Are you referencing any sample application libraries, like MVC futures or anything like that?
My application used a third-party package, the AspNetSprites-MvcAndRazorHelper. Reinstalled this package and the error of the reference was corrected.
This is another Nuget package that u can use to upgrade mvc 3 to mvc 4
https://nuget.org/packages/UpgradeMvc3ToMvc4
this is very simple, when you update to MVC 4.0.0.0 ,but the webconfig in the solution still points to MVC 3.0.0.0.0.
so just replace MVC version 3.0.0.0.0. by 4.0.0.0.0
Do the same with other assemblies, to check the version of assembly , check properties of the respective assembly in references in your project.

ViewBag.Title error

Working against the current RC2 - the template that is generated Razor views includes:
#{
ViewBag.Title = "Details1";
Layout = "/Views/Shared/_Public.cshtml";
}
With a red squiggly under ViewBag.Title and this compiler error:'
Error 4 One or more types required to compile a dynamic expression cannot be found. Are you missing references to Microsoft.CSharp.dll and System.Core.dll? c:\Visual Studio 2010\Projects\myProj\Views\Webinar\Details1.cshtml 6 2 TTSTrain.Webinars.WebEntry
But the project builds and functions correctly. Is the error indicative of other problems that should be addressed?
I got the same problem after I removed the targetFramework attribute from the <compilation> element in the Web.config file.
Once I restored it to
<compilation debug="true" targetFramework="4.0">
Everything worked fine again!
I solved it in the following way:
First i noticed using gacutil (Global Assembly Cache Utility) that it contained two references to System.Core, one to version 4.0 and one to version 3.5. Apparently inside the razor views, even if in the project i had the correct reference to version 4.0, it was still using version 3.5 and that's why i was getting the error about the dynamic types.
To check if that's your case open as administrator Visual Studio Command Prompt and execute:
gacutil -l System.Core
To remove the reference to the old version of System.Core i did the following steps:
- cd %systemroot%\assembly\
From here you may have more that one "gac" directory, so you will have to search within each to find your component. For me, it was within the "gac_MSIL" directory.
- cd gac_msil
- cd System.Core
- cd <assembly version number>__<public key token>
- erase *.* Say "y" to are you sure.
- cd ..
- rd <assembly version number>__<public key token>
- cd ..
- rd System.Core
After that I opened my solution again in visual studio and the error was gone, it references properly to System.Core 4.0 and I was not getting the dynamic errors anymore :)
I hope it will help you as well,
Best, N.
Similar to #Kaiser's answer, I experienced this problem as a result of having multiple System.Core assemblies in the GAC.
I chose not to delete the 3.5 assembly, however. Instead, in the Views web.config, I modified the configuration/system.web/compilation node as follows:
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</assemblies>
</compilation>
The important line is the last assembly node: it tells the Razor compiler which GAC assembly version to use.
Once I did this, all was well in my Razor views.
I do not have this problem when running VS 2012 as administrator.
Otherwise, what worked for me:
in root web config have added as recommended reference to correct assembly as child of compilation node`
<system.web>
<compilation debug="true" targetFramework="4.5">
<assemblies>
<add assembly="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</assemblies>
</compilation>
</system.web>
set copy local = true properties for System.Core and Microsoft.CSharp`
Do you have a reference to Microsoft.CSharp and System.Core?
MVC Views (usually) get compiled dynamically when you access your site, not when you compile the application in VS. I imagine you will see issues when running the site. Just add the two references and you should be fine.
By using Peters answer i managed to solve the issue with Html.EditorFor(m => m.xxx) underline errors in the chtml files.
Althought the ViewBar error persisted.
So i changed the web.config like this
<compilation debug="true" targetFramework="4.5.1">
<assemblies>
<add assembly="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
<add assembly="Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</assemblies>
</compilation>
Notice the Microsoft.Csharp line.
The above did the trick and now the chtml editor is clear from the red underlines.
Thanks Peter
I had the exact same problem. By default, when you create an MVC3 app it sticks a web.debug.config and a web.release.config in the solution. When I got rid of those two items, the ViewBag issue resolved itself. It may have something to do with what Peter was saying above but I didn't test that.
That is the time when other fields in ViewBag is read. So if you are passing them from controller.
ViewBag.yourobjectDto = yourObjectDto;
make sure this line is not blocked through if condition or something.
Try
Page.Title = "Details1";
It might work.

Resources