I have setup an MVC3 EF4 project with Model and the Repository broken off in to separate assembly projects. The basic validation for a required property etc. works fine, but if i need to do any remote validation say to check if a user is already in a group, etc. The remote validation does not recognize the controller within the Model project.
[Remote("IsUID_Available", "Validation")]
When I try to add a reference to the main project inside the Model project it says it would cause a circular dependency and doesn't allow it to be added.
Do I need to move my Models out the separate assembly and into the main project or is there another way to do remote validation with the Models being in a separate assembly.
Also what is the best practice here. I've read several articles that say putting the models in a separate assembly is the best practice but if you can't use half the validation functionality of MVC what is the point. I've also noticed most of the Microsoft MVC samples show the models just in the main project and not broken off into an assembly.
Turns out this wasn't an issue after all. Remote validation can be used as specified in the code above with the model in a separate assembly.
It was ReSharper that was giving an error in visual studio that the controller was unknown and marking it with the red underline but when actually compiled and tested the remote validation works.
Related
I'm new to programming so I might not be using some of the correct terminology. I'm running into an issue with InteliSense when calling a C# class from another project within the same solution. It's not suggesting a using statement and is instead trying to get me to create a new class inside of the current project which is not what I want. I am having to go in and add a refernce to the project and then add the using statement in order to get access to the class.
I looked at some of the documentation online and nothing has helped so far. InteliSense appears to configured correctly to suggest using statements. It provide suggestions just fine. I've been able to create lists and then use it to add the proper using statement along with some other things. Just doesn't want to work with anything inside the solution. I've been following a couple of different tutorials including, .net core 2.1 and 3.1, inside MVC and Razor page projects along with a couple just straight C# console apps. It doesn't work in any of them when I start adding multiple projects to the solution and try using classes from outside the current project.
I am having to go in and add a refernce to the project and then add the using statement in order to get access to the class.
That is the correct behavior. In order for ProjectB to use classes defined in ProjectA, you must first add a reference to ProjectA. Just having the projects in the same solution is not sufficient.
The purpose of having multiple projects in the same solution is simply for grouping related code. The projects may or may not actually depend on each other. For example, a web application may have a separate projects for the actual web UI (the pages themselves), a data access layer, unit tests, maybe some class libraries for shared code used by multiple projects, and maybe even console applications (or some other project type) for performing backend administrative tasks. In this scenario, the web UI and console applications may have references to the data access layer project and/or the class libraries. The unit test project will have a reference to the web UI project, and so on. The dependencies are one-way - you may not have circular references (the unit test project has the web UI project as a dependency, but not the other way).
I am creating a .NET Core web app using framework 4.6.2. I built my first view, testing throughout, until I added my view model and the page now errors out whenever I navigate to it. The errors are shown below. The ViewModel has all of the referenced properties on it. I don't even have a project.json given it's a brand new project on VS17.
This will happen if the properties you're using on your view model are not marked public. In my case, I had them marked internal and so was getting this error.
I have a Visual Studio 2015 solution with about 20 projects in it. Each of the projects references 2 class libraries that use Entity Framework 6. In these class libraries we have similar named objects that reside in differing namespaces. After adding a new Azure Web Job project I now get the exception below (just for the Web Job project):
An unhandled exception of type 'System.NotSupportedException' occurred in EntityFramework.dll
Additional information: The type 'AAA.BBB.Entity' and the type 'AAA.BBB.Entities.Entity' both have the same simple name of 'Entity' and so cannot be used in the same model. All types in a given model must have unique simple names. Use 'NotMappedAttribute' or call Ignore in the Code First fluent API to explicitly exclude a property or type from the model.
Several other projects work and don't throw this exception - a REST API, Websockets API, Test Project, and std web app. I've compared project references, properties, packages, etc and can't figure out what the difference is. The only difference I can see is that the Web Job is a console application and the others are web apps and a test project.
This is a known limitation of EF - https://entityframework.codeplex.com/workitem/483. Unfortunately, due to how EF6 is architected it would be extremely difficult to fix. Note that if only one of the clashing classes is used in the model, then you should be fine if you are using Code First – but it will fail if you have an EDMX model. Trying to use both types in the same model will always fail, regardless of whether you are using code first or EDMX.
I've read in other posts that you can put the business logic in a VS2013 MVC project "anywhere", which I take to mean "possibly outside the Controllers folder".
However, when I create an App_Code folder in my project, and put business logic classes in it, (with the build property set to Compile, not Content) I get a compilation error, on the following, and the code editor intellisense won't recognize the the .Caching in the following:
using System.Runtime.Caching;
If I move the class back into a subfolder of Controllers, no problem, the .Caching appears in intellisense and no compilation error occurs.
Any explanations of why this might be so, and how I might adjust my project to allow business logic classes to operate correctly outside the Controllers folder, would be appreciated.
The App_Code folder contains code that is compiled at runtime.
See the following reference: http://msdn.microsoft.com/en-us/library/t990ks23.aspx
If you have business logic, it's probably best to put it into an external (from the webiste) library to increase reuse and enable better compartmentalization.
Add a business layer class library project to your solution, and then add reference to this business layer class library project to your MVC project.
First off, there's no such ting as App_Code folder for MVC.
Why?
Well, the App_Code concept is meant to work with Website Projects, however, MVC are Web Application Projects which is a framework or implementation of a pattern to work closer to the HTTP protocol and web requests and separate concerns into presentation (views), request handlers (controllers) and models. So, don't expect it to work with App_Code
Suggestion
Create/Add a Class Library project in the same VS Solution where you can keep all your business logic and then reference this project in your MVC app
I'm trying to get Intellisense working for razor views in a non-ASP.NET project and would like to understand the relationship between VisualStudio's Razor editor and BuildProviders.
For background, I'm writing a framework on top of Manos (mono web server) that uses Razor for its view engine. I've got that part working perfectly, but Intellisense in VS doesn't work giving a range of errors from unknown types to unregistered build providers, depending on where the output DLL's of the project are placed.
My project is a .NET Class Library, with .cshtml files (build action none). The base razor view class is defined in a separate assembly (outside the project) which could be registered in the GAC, but currently isn't.
I've already read these articles:
http://www.west-wind.com/weblog/posts/2011/Jan/12/IntelliSense-for-Razor-Hosting-in-nonWeb-Applications
Need razor view engine auto-complete to work in a class library?
http://blogs.msdn.com/b/webdevtools/archive/2011/01/20/how-to-get-razor-intellisense-for-model-in-a-class-library-project.aspx
.NET - Razor outside MVC application - Problems with removing #inherits and providing #model
Sounds like I need to write my own BuildProvider, but can't find any documentation explaining the relationship between a build provider and razor intellisense.
Razor intellisense is flaky at best currently. However, if you're using VS SP1, its slightly better. The web.config workaround (as pointed in your third link) works for me in a class library as long as the extension is cshtml (haven't tried vbhtml so can't say for sure).
Also take a look here: http://razorpad.codeplex.com/
Similar to LinqPad, this will allow you to test your razor code ahead of time.
The Razor editor is pretty heavily tied in to the ASP.Net runtime, in fact it actually runs ASP.Net in the background in order to collect the necessary run-time information.
My only suggestion for getting true-fidelity IntelliSense is a bit of a super-hack. Rather than a Class Library, you could make your application a Web Application Project. A WAP is actually just a class library which VS can host a website from. if you clean out ALL the extra stuff (Global.asax, web.config, etc.) you may get exactly what you're looking for. It's a workaround, and a bit of a stretch at that but give it a shot, it may just work :)