How to add assembly in web.config file of mvc 4
Asked Answered
C

2

29

I have a "MVC 4" project and i want to add an assembly to web.config file but i don't know where should i put it. I try any ways but i cant find the solution.

every time i got this error:

You must add a reference to assembly 'System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

this is my web.config file:

    <?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=152368
  -->
<configuration>
  <connectionStrings>
    <add name="ParsGraphicEntities" connectionString="metadata=res://*/Entities.ParsGraphic.csdl|res://*/Entities.ParsGraphic.ssdl|res://*/Entities.ParsGraphic.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=ParsGraphic;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-UI.MVC.InternetApplication-2012317193118;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="true" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" />
    </authentication>
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <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.WebPages" />
      </namespaces>
    </pages>
    <profile defaultProvider="DefaultProfileProvider">
      <providers>
        <add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
      </providers>
    </profile>
    <membership defaultProvider="DefaultMembershipProvider">
      <providers>
        <add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
      </providers>
    </membership>
    <roleManager defaultProvider="DefaultRoleProvider">
      <providers>
        <add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
      </providers>
    </roleManager>
    <sessionState mode="InProc" customProvider="DefaultSessionProvider">
      <providers>
        <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
      </providers>
    </sessionState>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
  <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="1.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>
</configuration>

please help me.

thanks.

Crutchfield answered 26/2, 2012 at 19:9 Comment(4)
Why do u think u have to add a reference in the web.config? Does it compile in design time? If not then i would suggest adding a reference by using the Add Reference dialog. Or better, use nuget and install entity frameworkExtraterrestrial
did you check out #5021748Tarpon
@rfcdejong, I already add System.Data.Entity with Add reference dialog but i must add this assembly in web.configCrutchfield
@Rick.Anderson-at-Microsoft.com, that works fine with mvc 3 and i work with it without any problem in MVC3 but Web.config file of mvc 4 have difference with mvc 3.Crutchfield
P
49

It is quite simple. Look at your compilation section in your web.config:

<compilation debug="true" targetFramework="4.0"/>

Now open the section and put your assembly inside like this:

<compilation debug="true" targetFramework="4.0">
  <assemblies>
    <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </assemblies>
</compilation>

Libraries in this section are checked in compilation time, it has nothing to do with runtime binding.

Pitsaw answered 9/4, 2012 at 19:9 Comment(6)
To clarify: there may be more than one Web.config in your project. Add the assemblies info above to the config located at the project level, not the one in your Views folder. don't forget to remove the "/" closing tag in the first self-closing compilation tagAnaerobe
Although this solved my problem, I still don't understand why I had to do it. I already had a reference to System.Data.Entity in my project but yet I was getting runtime errors saying it cannot find the assembly. When I added it in the web.config in the assemblies section, the problem went away. I need someone to explain why I have to have both.Winfordwinfred
Adding a reference adds to your project (project file and bin folder) that reference. However you don't deploy your project file. This means that you can publish your web app with your dll in the bin folder but your web app doesn't know that it is there when it compiles all your app on the server side on the first request. As I said that section is checked in compilation time (because your server can recompile the code). That is why you have to include that information in your web config, because that web.config is the file that is deployed on your server.Kelley
(second chunk) Web.config files can be quite complex and they can be defined in different ways to get to the same result. Sometimes some dependencies have their own sections and sometimes they don't, so when an assembly that you need (for any reason) is not contemplated in any way in your web.config you might have to add it yourself. There are many ways to skin a cat.Kelley
@AlfonsoMuñoz This makes sense except where is the publicKeyToken coming from?Justle
@Ben Hoffman, there is always an answer for everything :) #17365849Kelley
R
4

I just experienced the same issue with MVC 4 in VS Express 2012 for Web, but, while Alfonso's answer was much more helpful than anything else I found on the Web, it unfortunately did not clear up the error in my project. After doing some research on the Internet (to no avail) and then just poking around Visual Studio, I found the following solution.

In addition to adding EntityFramework to my project's references and adding the tag to my Web.config, I also had to take the following steps to satisfy the compiler:

  1. Right-click the References folder > Add Reference...
  2. Expand Assemblies on the left side of the window and select Framework.
  3. Scroll to and select System.Data.Entity in the list of assemblies.
  4. Make sure the box next to System.Data.Entity is checked, then click 'OK'.
  5. Rebuild the project.
Rainer answered 17/8, 2013 at 5:53 Comment(1)
Nice job. This was the special sauce I was missing.Risinger

© 2022 - 2024 — McMap. All rights reserved.