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.
Related
i have two projects that share one database. The Entity Data Model of the database is in a separated class library (i'm using Entity Framework 5).
So, one of the projects is an ASP.NET MVC project and i want to include the Entities in my Model, so i could use them in a WebAPI controller.
I have added a reference to the .dll of the database access library and i have included the ConnectioString to the web.config, so the database is fully accessible from my MVC project. But, if try to create a new Controller like this:
Add -> Controller -> WebAPI 2 Controller with actions, using Entity Framework
Then, at the "Model Class" section, Visual Studio does not shows my Entities to include them as Model objects, is it possible to make Visual Studio search for my entities at the .dll to automatically generate the controller?
And one more question: How does Visual Studio decide which classes to show as Model Classes and which doesn't?
Did you compiled your solution before you tried to add the Models in the new Controller dialog?
I'm trying to reuse shared templates in my MVC applications. For this, I use the Razor Generator VS Extension and I set RazorGenerator as the Custom Tool for my *.cshtml files. In my solution, I've created another MVC project (as described on http://razorgenerator.codeplex.com/) and therein, under Views/Shared/EditorTemplates I've defined a String.cshtml template.
In my actual MVC app, I've added a reference to this project and in a view I try to do the following:
#Html.EditorFor(d => d.FeedName)
(FeedName being of type string). However, the String.cshtml does not get picked up.
I've tried to set the Custom Tool Namespace of String.cshtml to use the same namespace like the actual app just to see if that would help, but it didn't.
So, how can I tell the engine to pick up the editor template from another assembly?
Is it possible to use the MVC5 Scaffolding to create a new Controller with View, using EntityFramework if the Models and DbContext classes are not in the same namespace.
I have defined the models and dbcontext in 2 separate libraries (Project.Models and Project.DataAccess) and when entering the Add Controller menu the dropdowns for model and datacontext don't contain the classes I'm trying to use.
I have of course referenced them in the project.
Just make sure to compile your solution and then go to your MVC project and add reference to your model and dbcontext projects and that should enable you to see the model and dbcontext classes while creating controllers or views via scaffolding.
Sometimes MVC project some how catches the first reference. Even you build/rebuild library model, it doesn't update the MVC project. I got same problem and tried these actions:
Unloaded library project and reloaded it into solution
Deleted library model reference from MVC project and added again
Created DbContext class in Library model project.
Then it worked.
Following the sports store tutorial in Pro Asp.Net MVC book,
I have TWO projects in my Asp.Net MVC4 solution. One SportsStore.WebUI and one SportsStore.Domain , which has my Product class below
I added a Required attribute to Name in my model class
//Product.cs class
[Required]
public string Name { get; set; }
//..more properties, not important as I get the same error for all -
because I added Required to all
When I run the project I get The model backing the 'EFDbContext' context has changed since the database was created. error.
I have checked on stackoverflow and MSDN
Solution 1: Didn't Work
Suggested solution on MSDN was to run Update-Database -Force . That doesn't work , I get
"Could not load assembly 'SportsStore.WebUI'. (If you are using Code First Migrations inside Visual Studio this can happen if the startUp project for your solution does not reference the project that contains your migrations. You can either change the startUp project for your solution or use the -StartUpProjectName parameter.)"
That is strange because A- I'm NOT using code first and B-The Domain project DOES reference WebUI project
Solution 2: Didn't Work
Then I tried Enable-Migrations in the PM console and I get
No context type was found in the assembly 'SportsStore.WebUI'.
I then tried changing the startup project to SportsStore.Domain ( which has my product class) - nothing, same error
This problem is actually occurring because you didn't change the target for your invocation of the scaffolding. In the Package Manager Console, when you run any commands which target your Domain objects, the Default Project dropdown needs to be set to your Domain project. If you try to run migrations or database updates against your web project, they will fail.
Edit
Important is this line in the error
If you are using Code First Migrations inside Visual Studio this can happen if the startUp project for your solution does not reference the project that contains your migrations.
the error suggests "Could not load assembly 'SportsStore.WebUI' which means that's the project the migrations were put in, even though they should have gone in your Domain Project.
I also had this error. I had an existing asp.net mvc using EF 5.0 that was working fine.
I then created a console app with EF 5.0 that pointed to the same database. The asp.net mvc app started throwing the model backing exception.
I noticed there was a new table named "_MigrationHistory". To get the asp.net app working again, I renamed this table "_MigrationHistory_backup"
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.