What is the syntax for defining a SqlDataConnection Type Provider when using a connection string name from an app.config file. I have a solution with two projects in it. The main project is an an mvc 5 project and the second is an F# project. In the f# project I have defined an app.config file with a connection string to a database and defined my SqlDataConnection as this:
SqlDataConnection<ConnectionStringName="dbname", ConfigFile="app.config">
Everything seems to be wired up correctly when writing my f# code, as I am able to get intellisense on the the database tables, but once I run/debug the site I get an error that implies that the data provider is looking for the connection string in the main project. What am I missing here?
Examples that I've seen all specify a single string to the type provider.
Try:
SqlDataConnection<"ConnectionStringName=dbname;ConfigFile=app.config">
Essentially, this is the same syntax that you'd use in your app.config file.
So it seems that once you run/debug your site, the data provider attempts to look for the connection string in the Web.config of the main project. So I renamed the app.config in my F# project to Web.config, and made sure to have the same connection string listed in the Web.config file of the main project as well. That way I appear to have access to the schema while coding, and when the project runs it picks up the connection string from the main project. This is a working solution for me, but if anyone can shed any more light on this, please do so. The new syntax is:
SqlDataConnection<ConnectionStringName="dbname", ConfigFile="Web.config">
Related
I have a MVC5 project that is currently set on the "Release Configuration" and it works 100%. However, as soon as I switch the project configuration from Release to Debug, then everything goes wrong... even if I switch it back to Release mode, everything is still broken. Only way I can get the project working again is to restore from a backup.
Here are what is happening.
Firstly, when running the project, I get the following error:
BC30451: 'ViewData' is not declared. It may be inaccessible due to its
protection level.
If I open any view in the project with Visual Studio 2013, I can see that all sorts of things are marked as errors like ViewData, Html, Url, etc.
When referring to #Html or #ViewData in the view, it normally refers to the .Html and .ViewData properties of the view's base class (WebViewPage). However, if I start typing "#Html." in any of the views, I can see in the autocomplete that it is referring to the System.Web.Webpages.Html namespace instead of the WebViewPage.Html property. It is as-if the view isn't inheriting from the System.Web.Mvc.WebViewPage class.
Any guidance as to where I can start looking to get this fixed or why this is happening?
Edit:
So since nobody responded, I went through the long way. I created a brand new MVC5 project, added all the packages via Nuget and then simply copied all my files over from the old project to the new one and now it works.
Does anybody have any idea what the heck could be causing this? I don't want to go through all this trouble again in the future if the project again suddenly decides to stop working.
Ok, I think I've found part of the cause here. Thing is, since the original code is valid (it compiles correctly and intellisense picks it up) and the code used to work and then all of a sudden, one day after compiling, it just stops working.
Anyway, in the view, when specifying the ModelType, if you don't use the full name, this error can occur or occurs eventually.
For example, using:
#ModelType Models.SomeNamespace.SomeClass
will cause the error (even though the Root Namespace for the project is "MyProject") and it can be fixed by simply specifying the full namespace and class name.
#ModelType MyProject.Models.SomeNamespace.SomeClass
It's possible that a debug assembly is locked. Can close Visual Studio, search and delete for all bin folders in the solution directory. Then open and rebuild.
The other options is to go into the MVC project properties and compare the two build configurations. Are you targeting a different .NET framework between Release and Debug? 32 bit vs 64 bit? etc?
Looks like some MVC assemblies are corrupted.
Have you checked your referenced assemblies for some errors?
Try also cleaning all the .NET internal cache. More in this stackoverflow topic: Could not load file or assembly ... The parameter is incorrect
In my case imported ViewModel class name was incorrect, correcting it fixed the issue.
I have two projects: Mvc3TestSvcRef & Mvc4TestSvcRef.
Mvc3TestSvcRef is from the ASP.NET MVC 3 template for an intranet application.
Mvc4TestSvcRef is from the ASP.NET MVC 4 template for an intranet application.
I'm trying to add a service reference.
In Mvc3TestSvcRef, I right-click the project (or the References folder) and choose Add Service Reference. I point to the URL, click Go. When the reference is resolved, I enter a namespace and click OK. As expected, I see the section added to config with the bindings and client tags completed.
I can import: "using Mvc3TestSvcRef.MySvcRef;"
And write code like:
using (var cl = new MyServiceClient())
{
cl.DoStuff();
}
In Mvc4TestSvcRef, I follow the same steps, but there is no system.servicemodel added to config. Additionally the import: "using Mvc4TestSvcRef.MySvcRef;" cannot be resolved.
I've tried this for MVC 4 from both Visual Studio 2010 and Visual Studio 2012.
Was there a major change to the process for adding service references in ASP.NET MVC 4 project type, or am I missing something or have corrupt install?
There was no code in Reference.cs, just comments:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.17929
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
I copied the reference.cs from the project that worked and modified the namespace, then added the section from the working project into the MVC 4 project and was still having an issue.
I tried to build and I got several warnings and an error. Failed to generate code for the service reference 'MySvcRef'. Please check other error and warning messages for details.
That led me to this article: Service Reference Error: Failed to generate code for the service reference
So I unchecked the Reuse types in all referenced assemblies from the Advanced section.
This seems to have generated a good service reference. Although, I should point out that if you have something in say System, like System.TimeSpan for example, that is used as a DataMember in one of your DataContracts, the reference will now have TimeSpan in the reference namespace, not from it's origin. So, the client would see any System.Timespan properties as ReferenceNameSpace.Timespan, which may throw off comparisons and such. The better answer here is to include specific assemblies from the reference and don't check the box for System.Web.Http, as pointed out in the comments below
I don't know if it is too late, but here is the solution "When you add the reference, on advanced setting remove the reuse types checkbox."
I have an Entity Framework Project and a repository class in a separate project from my MVC3 web application. I have established a reference in my MVC project to the Entity Framework data project so i can instantiate an instance of the repository and call the methods thereof. However I get the error:
The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
I've run into this before and I believe the solution is to include the connection string from the entity framework app.config file in the MVC web.config file.
This doesn't rest well with me. It feels like there should be another way that would make projects less tightly coupled together. I'm I dreaming or is there a better practice that would allow me just to make call to the referenced dll and be done with it?
Thanks
The app.config file that is included in the DLL of your Entity Framework project contains a Connection String that is used by the EDMX designer to find the target database when running an 'Update Model from Database' command.
When deploying your application, the only configuration file that is known is the web.config. The app.config file from your EF dll is not used in production.
So in your web.config you include the connection string that is used when you are running your MVC application. When using transformations you can also specify different connection strings for different deployment scenarios (test and production for example).
So it's not like you are introducing some sort of coupling. You are just using the configuration methods that .NET offers you.
There are ways, hard-coding the connection string in your repository and using it when you create the context comes to mind, but you most certainly don't want to use them. The right way to handle it is through the configuration file. You really don't want to have it use the configuration file from the DLL, since that would give you less control over what connection string you're using. This would make it harder, rather than easier, to have different connection strings for integration testing, staging, and production. While it's possible to combine the approach (fixed connection string that can be overridden by a configuration setting), having used both my preference is for a completely configuration driven approach. I like the single convention and the one-time step of updating the Web.Config (and any transforms) with the correct configuration setting seems little cost to pay for the simple convention of using the configuration always.
I don't understand how putting a connection string in the MVC project's config file makes it "tightly coupled". The config files themselves are a source for loose coupling. You can always change connection strings using config transforms, meaning you can switch the connection string just by choosing a different solution configuration.
I've been in the process of attempting to deploy an ASP.NET 4.0 (webform) project on a server that is running an ASP.NET MVC 1.0 application in it's web-root. I was slowly making progress, but hit a wall, so I decided to recompile the MVC app against version 2.0 of the MVC framework and 4.0 of the .NET framework. I used a few tools to do so.
The project appeared to successfully convert, but now it fails to compile. Specifically, it appears that there is a problem with System.Web.HttpContextBase.
For example, if I have the following code:
context.HttpContext.Response.Clear();
I get the following error:
'System.Web.HttpContextBase' does not contain a definition for 'Response' and no extension method 'Response' accepting a first argument of type 'System.Web.HttpContextBase' could be found (are you missing a using directive or an assembly reference?)
I've checked the object explorer just to make sure: HttpContextBase does have a property called Response. This thing is driving me crazy.
I have another big problem, which I believe to be related. Anytime the Request object is queried:
Request["old"];
It says:
Cannot apply indexing with [] to an expression of type 'System.Web.HttpRequestBase'
As far as I can tell, there isn't any reason for this either.
I'm at my wits end. I've checked the breaking changes document for MVC 2.0 but didn't see anything related to this. The references all look good to me; the version numbers all check out.
Further note on the server issue: I do not have any access to the deployment web server except ftp. I cannot go in and mess around with the server configuration. That's why this may sound a little odd to you.
I'm glad that worked for you, here it is again as an answer for everyone else
It's possible your IDE and Runtime are disagreeing over which library
version to use. Try creating a new project and moving your source over
to it and compiling again.
I want to compile App_GlobalResources/Strings.resx into my assembly (and eventually use satellite assemblies for Strings.es.resx, Strings.fr.resx, etc.) but the following error occurs once the app is published:
"Could not load file or assembly 'App_GlobalResources' or one of its dependencies. The system cannot find the file specified."
Steps to Reproduce:
Create a new ASP.NET MVC project.
Add an App_GlobalResources folder and a Strings.resx file.
Set the file's build action to 'Embedded Resource'
Add a string to Strings.resx and use it in HomeController.Index(), e.g. ViewData["Message"] = Strings.MyTest
F5 to debug, works fine.
Publish to IIS and (since Strings.resx is excluded from publishing) you will get the above error in the published site.
Can ASP.NET MVC projects use string resources compiled into the dll and if so, what am I doing wrong?
Update: Here is what I see in Reflector:
So why is the ResourceManager not finding them? Isn't it supposed to fallback to the main assembly by default?
Here's a good starting point http://odetocode.com/Blogs/scott/archive/2009/07/16/resource-files-and-asp-net-mvc-projects.aspx
I setup a project just like you mentioned and got precisely the same error when I deployed to IIS6. Changing Custom Tool to PublicResXFileCodeGenerator and Custom Tool Namespace to Resources per Scott's suggestions cleared it right up.
I'd also follows Scott's lead with respect to eliminating App_GlobalResources but I got it to work just fine. That might be a matter of personal preference.
Probably there is one more thing one should know about the PublicResXFileCodeGenerator - it does not work with files that has more then one dot in the file name (see http://social.msdn.microsoft.com/Forums/ru-RU/9412f049-acd8-4a77-b73a-f3810df18376/naming-resources-files-issue-aspnet-mvc-3?forum=dlr). So the codebehind for the Strings.es.resx, Strings.fr.resx, etc. remains empty. The easy solution is to rename the files.