Using System.Data.Linq in a Razor view - linq-to-sql

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:
<h3>Owners</h3>
#foreach (var ThisOwner in Prop.PropertyOwnerships.Where(p=p.bIsOwner.Value==true))
{
<div class="ODEditEntry">
...
I'm getting the following error:
Compiler Error Message: CS0012: The type 'System.Data.Linq.EntitySet`1' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
I tried putting #using System.Data.Linq at the top of the cshtml file but it is telling me that Linq doesn't exist in the System.Data namespace. This is obviously not true and, yes, I do have system.data.linq as a reference in my project.
Any Ideas here? Is a import needed? Can I just not do Linq style stuff in my razor views? That would seem....odd?

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.

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.

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.

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

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.

Related

How can I add the "Microsoft.Reporting.RdlBuildProvider" Build Provider Extension to my website project?

I am having no end of problems in getting a website to display (I was given a zip file of the files). Based on this, it is a problem with my Web.config file not being readable.
As it is right there in the project, I don't understand why VS is having a problem reading it. Perhaps it is something in the file's contents that is throwing a spanner or monkey wrench into the works?
The only thing there that really sticks out to me (I don't understand most of it) is this:
<compilation debug="true">
<buildProviders>
<add extension=".rdlc" type="Microsoft.Reporting.RdlBuildProvider,
Microsoft.ReportViewer.Common, Version=8.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</buildProviders>
...and the reason why that stands out is because the "Reporting.RdlBuildProvider" part is displayed in red, as if it is unrecognized (nothing else in the files is thus flagged).
It is true that there is a relation to SSRS in this project, but I don't think a direct one, so I tried commenting out that part of Web.config, but it made no difference - I still get the "500.19" error when trying to run the site.
Nevertheless, I would like to get the red out, so how can I add the "Microsoft.Reporting.RdlBuildProvider" Build Provider Extension to my website project?
Try this:
<compilation debug="true">
<buildProviders>
<add extension=".rdlc" type="Microsoft.Reporting.RdlBuildProvider, Microsoft.ReportViewer.WebForms, Version=8.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
</buildProviders>
<assemblies>
<add assembly="Microsoft.ReportViewer.Common, Version=8.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
<add assembly="Microsoft.ReportViewer.WebForms, Version=8.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
</assemblies>
</compilation>

Does the <domainService> Tag still work for Silverlight 5 and RIA Services?

I am trying to add a JSON endpoint to a WCF RIA Service.
I have read many tutorials on this and I have also watched videos on silverlight.net.
Everywhere I find something on this topic, I am told to use the domainService Tag in web.config.
One code example looks like this:
<system.serviceModel>
<domainServices>
<endpoints>
<add name="JSON" type="Microsoft.ServiceModel.DomainServices.Hosting.JsonEndpointFactory, Microsoft.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</endpoints>
</domainServices>
<system.serviceModel>
This is not working and there are two issues:
In Visual Studio 2010 the domainService Tag is underlined blue, with the hint, that this is an unexpected tag.
In IIS the domainService Tag is producing an unknown element error.
Among others, I have referenced the following DLLs in my web project:
-Microsoft.ServiceModel.DomainServices.Hosting
-Microsoft.ServiceModel.DomainServices.Tools
-Microsoft.ServiceModel.DomainServices.LinqToSql
-System.ServiceModel.DomainServices.EntityFramework
-System.ServiceModel.DomainServices.Hosting
-System.ServiceModel.DomainServices.Hosting.OData
-System.ServiceModel.DomainServices.Server
I also have the WCF RIA Services V1.0 SP2 installed.
I really don't know, how to resolve it. I need help. I have googled and searched excessively.
Thank you.
This case is solved. Please read my comments to Jeff's answer. You may find some additional information on this problem.
The JSON endpoint is part of the WCF RIA Services Toolkit, not the product itself. You can get the Toolkit MSI here:
http://www.microsoft.com/en-us/download/details.aspx?id=26939
However, I recommend pulling the JSON endpoint in through NuGet instead, using the RIAServices.Endpoints package:
http://nuget.org/packages/RIAServices.Endpoints
Here's some more information about the NuGet packages available:
http://jeffhandley.com/archive/2012/12/10/RIA-Services-NuGet-Package-Updates-ndash-Including-Support-for-EntityFramework.aspx
For me the correct way to resolve the issue was adding the following to web.config
<configuration>
<configSections>
<sectionGroup name="system.serviceModel">
<section name="domainServices" type="System.ServiceModel.DomainServices.Hosting.DomainServicesSection, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" allowDefinition="MachineToApplication" requirePermission="false" />
</sectionGroup>
</configSections> ...

Setting breakpoints in a Razor file in Visual Studio 2012

In Visual Studio 2012, I am trying to set breakpoints in my razor (.cshtml) file, but every time I try, it says the breakpoint could not be ser, or else I get the following error message:
The following breakpoint could not be set:
...
The Common Language Runtime was unable to set the breakpoint.
I found that in my web.config, I had the following line:
<compilation
debug="true" targetFramework="4.0"
assemblyPostProcessorType="Microsoft.VisualStudio.Enterprise.Common.AspPerformanceInstrumenter, Microsoft.VisualStudio.Enterprise.AspNetHelper, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
The assemblyPostProcessorType attribute was causing the problem. I'm assuming it does something to the dynamically-generated razor assemblies that breaks debugging.
So I simply removed that attribute to get:
<compilation debug="true" targetFramework="4.0" />

Why can't I use Html helper methods with strongly-typed views in ASP.NET MVC 2?

I created an ASP.NET MVC 2 project with a model. I then created a view and selected for it to be strongly-typed for the model that was created. I also did the same thing with a partial view. In either case for some reason I am getting the error: "Cannot resolve symbol 'Html' whenever I try to use the Html helper methods that ASP.NET MVC 2 provides for creating form elements for the model. I have Visual Studio 2010 and ASP.NET MVC 2 installed. Is this something that has been seen before? If so, is there a solution that will resolve this?
My model looks like:
public namespace MyNamespace { public class MyModel { public string MyProperty { get; set; } } }
The first line of my regular view is:
<%# Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MyNamespace.MyModel>" %>
The first line of my partial view is:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyNamespace.MyModel>" %>
I think I found a fix for your problem (I'm not sure if it's temporary though)
When you create a new asp mvc project it contains 2 web.config files. One on the project root and one on the Views directory.
I thought that the one on the Views wasn't required so I excluded it. The next day resharper stop recognizing the Html helper methods. I added it again and reload the solution and all errors were gone.
Hope this helps
EDIT
this link would probably help with this problem
I get this error all the time, I don't quite know what causes it. It must have something to do with Visual Studio failing to parse the Page / UserControl directive properly and subsequently not loading the correct type defined in the inherits attribute.
It doesn't really affect the application other than losing intellisense and you also get a lot of errors in the visual studio output when you have the aspx view file open and build your solution (your solution should still build no worries).
I can get the error to go away every now and again by messing with the Page / UserControl directive, which forces Visual Studio to re-parse it. E.g. put a random space anywhere inside the inherits attribute and then delete it.
HTHs,
Charles
Ps. I too am using resharper, but I'm not sure if it's to blame.
Yea, this has happened a couple times to me as well. If the application loads correctly and the view loads the objects I suspect it might be your ReSharper Build. Try updating your version of Resharper and see if it recognizes your models.
I've had the same issue, also removed the web.config in a moment of ignorance...
Merging the below piece of the web.config to your web.config in the root of the web application will do the trick also. It's works again like it should and still you have only one web.config file.
<system.web>
<pages
validateRequest="false"
pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
</namespaces>
<controls>
<add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
</pages>
</system.web>
This error happened to me because I moved by mistake the web.config that was inside the "Views" folder.
This file contains the include of System.Web.Mvc. Hope this helps.
Greetings

Possible to add shortcut names to razor namespace imports in MVC 3 web.config

Similar to using Shortcut = System.Whatever; in the cshtml I have so far been out of luck doing something similar in the webconfig
<pages>
<namespaces>
<add namespace="System.Web.Routing" as="Routing" />
</namespaces>
</pages>
Something like that...Is it possible at all?
No, it is not possible in Razor because there is no mechanism in C# that would allow you to redefine a namespace.