Using System.Data.Linq in a Razor view

前端 未结 5 2107
名媛妹妹
名媛妹妹 2020-12-03 00:54

I may have a fundamental misunderstanding of what is going on here, but I\'m having an issue looping through a LinqToSQL class in my razor view:

Ow

相关标签:
5条回答
  • 2020-12-03 01:24

    You need to add a reference to System.Data.Linq in your project and/or in your Web.config.

    0 讨论(0)
  • 2020-12-03 01:27

    What fixed it for me was to right click the System.Data.Linq assembly reference, then hit properties. In there set Copy Local to true.

    0 讨论(0)
  • 2020-12-03 01:29

    I spent hours and hours googling and no solution worked but this is what helped...

    Remove everything inside <assemblies> element in web.config!

    <compilation targetFramework="4.7.2">
      <assemblies>
        <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Helpers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <!-- etc etc blah blah -->
      </assemblies>
    </compilation>
    

    Either add System.Core in there, or even better, remove everything and change it to:

    <compilation targetFramework="4.7.2"/>
    

    Explanation

    The System.Linq namespace is defined in System.Core, right? So we either need to add it to the <assemblies> list , or (because System.Core is always referenced in your root web.config located at "%Windir%/Microsoft.NET/v4.0.30319/Framework/whatever") remove everything from <assemblies> to prevent conflicts.

    0 讨论(0)
  • 2020-12-03 01:31

    You need to import the namespace into your view by adding @using System.Data.Linq at the top of your view. However if you want it in all your views then you need to add <add namespace="System.Data.Linq" /> to the web.config in your Views folder:

      <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.Data.Linq" />
          </namespaces>
        </pages>
      </system.web.webPages.razor>
    

    Although not relevent to your question you should really try to move this logic out of the view and into the controller, it will make things much easier to debug and means that your presentation is separated from your business logic.

    0 讨论(0)
  • 2020-12-03 01:34

    Does your Linq-to-Sql datacontext exist outside of the web project (e.g. in another class library)? If so, where you have added a reference to that project it all builds fine, but in the Razor view you are trying to directly access a type from the System.Data.Linq assembly without referencing it in the web project. Try adding the reference to the main web project and see what you get.

    0 讨论(0)
提交回复
热议问题