enable-migrations error : project failed to build - asp.net-mvc

Using ASP.NET MVC4 with EF4.3
Running Enable-Migrations from PM Console
System.Management.Automation.RuntimeException: The project 'MyProj'
failed to build. The project 'MyProj' failed to build.
The project builds (control-shift-B)
Is there any way to get more detail on this error to diagnose?

This can also happen if you are building a 64-bit application. Switch to "Any CPU", at least temporarily while building the migrations, and the problem may go away. This is just one thing that can cause that error, though.

Executing "Enable-Migrations" from the package manager console requires a reference to a specific DbContext. For example
PM> Enable-Migrations -ContextTypeName myMvcApp.Web.Infrastructure.ContextDb
You do not need to install a 3rd party assembly or any other assembly other than the EntityFramework 4.3+ (the command above is for EF 5.0. This is not a well documented but essential feature of the EF.

Turns out I had run this earlier, on a project where there was no dbContext. This created a migrations Configuration class that said "fill in the name of the class", which was failing to build.
This was causing one of my projects to silently fail to compile.
Removing the Configuration class solved my problem.

Let's be clear. This error is a confusing one because the message suggests that the project won't build whereas it clearly does compile in Visual Studio.
It happened to me after refactoring namespaces for ApplicationDbContext and other security classes to match the folder structure from
namespace {projectname}.Models
to
namespace {projectname}.Models.Account
Reverting this namespace refactoring fixed the issue.
Sometimes, rarely, ReSharper is not your friend.

In my case I had errors in my project. I have cleared all other errors in my project.
Then it works well.

In my case there was a message in the Visual Studio output window that made much more sense.
PosmasterDBContext' does not contain a definition for 'Products' and
no extension method 'Products' accepting a first argument of type
'PosmasterDBContext' could be found (are you missing a using directive
or an assembly reference?)
I knew right away that somewhere in my code, I changed Products to Product
Once I reverted it to Products, the error disappeared.

Related

Visual Studio Code: using directive missing or invalid, except it isn't

I started messing with the new Visual Studio Code today and I am running into a small "problem"...of sorts.
I have added reference to Npgsql in the project.json of a Web API project and run 'dnu restore' which also updated the project.lock.json to include the proper references. However the code editor highlights an error for every reference to an Npgsql object, giving me the "are you missing a using directive or reference" message for each one, including the 'using Npgsql;'
The really odd part is that the same tooltip that tells me it doesn't recognize the type..also tells me exactly what it is and does. Intellisense still works and if I execute 'dnx: web'...the whole thing runs just fine with no errors.
I am assuming its just a bug in the new editor but who knows, maybe I've not configured something correctly? Anybody else see this?
You might see this when having two frameworks configured in project.json. By default you will have dnx451 and dnxcore50. It might be that the dependency is only available for one of the frameworks but not for the other. Check the error message VSCode gives you - it will include in []-brackets the framework for which the error is valid.

MVC5 with VB.NET: "BC30451: 'ViewData' is not declared." when switching to Debug configuration

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.

Cannot get both Authentication and Entity Framework working in startup.cs

I am using the most up-to-date code examples and snippets I can find, and I understand that things are changing and moving quickly.
When I use the out-of-the-box Visual Studio "vNext ASP.NET Web Application" template, then authentication and database services work just fine.
However, I think that the template is no longer up-to-date and I would like to use the latest assemblies and methodologies.
If I try to use "Microsoft.AspNet.Security.OAuth" as mentioned here:
https://stackoverflow.com/a/26532837/390480
then I cannot get both Authentication and Entity Framework with SQL Server to work.
What happens is that some methods or properties are no longer available.
Is there any example right now with authentication and EF 7 with SQL working?
Optimally I would like to use UseOAuthAuthorizationServer but that no longer seems to be an option so UseCookieAuthentication is fine.
Thank you.
UPDATE 1:
I have tried the two sample applications "Music Store" and "Identiy Sample".
They would not build/run until I ran the file that comes with them named build.cmd.
So I ran this file but forgot that the last time I ran it it corrupted my system and it corrupted my system again.
Now Visual Studio keeps showing a message box saying:
The project system has encountered an error.
An item with the caption "" already exists in the tree
with the same sort order as the item being added.
Also after running build.cmd I now get hundreds of errors such as:
Error 1 The type or namespace name 'AspNet' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
I don't know what fixed it last time. Trying to fix again now.
UPDATE 2:
I remember how I repaired my system now. I recorded the steps here and will try it again now. It's about a 1 hour process.
http://forums.asp.net/p/2013828/5795198.aspx?Re+Can+t+creat+new+project
first get the latest KRE
with Command prompt (run as admin)
#powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/aspnet/Home/dev/kvminstall.ps1'))"
right click on your project and pick newer version KRE !
http://i.stack.imgur.com/dxGth.png
update all your project.json packages to RC1
i.stack.imgur.com/YYAIe.png
and last part on ASP.Net 5 starter Web Template has migration files under Migration folder
and from beta1 to RC there are some changes happened on method names (like GenerateValuesOnAdd is now GenerateValueOnAdd) need to be fixed
and you are ready to go
**** I prefer to start ASP.NET 5 Empty project and select and test everything Step by Step you can also follow that way too ***
(Sorry I want to do a better job on this but stackoverflow doesn't let me put links or images with my reps)

Cannot add a MVC controller VS2012 gives "object reference not set to an instance of an object"

I have searched extensively and cannot find another person on the planet with this problem...
The symptoms are:In Visual Studio 2012, on a MVC 4.0 Project that was upgraded from VS2010 and is configured for dual version editing from VS2012 and VS2010. When I right-click on the Controllers Folder and select Add->Controller... from the context menu, I get an "Object reference not set to an instance of an object" error from Visual Studio.
The existing code in the project runs fine, and if I use the Add->New Item option instead, and pick the Controller from the dialog box, then I can add a blank controller. I just cannot add a typed controller using the method above.
If I however create a new MVC4 Project, then I can add typed controllers again using the same menu, so the problem does not exist in Visual Studio's installation, it has to exist in the Project File. I have also checked the references between my project and a new MVC Project and all the new project references are included in my project.Has anyone experienced something similar?How do I fix this?
The issue seems to have been caused by an incomplete install/registration of the MvcScaffolder package.
The following process resolved my issue for me.
Start Visual Studio and open the project. Open the Package Manager Console. (This was the "trick" or missing piece of information that led to the solution...)
Here I saw the following error caused by the PowerShell script used for registration:
New-Object : Could not load file or assembly 'System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
At <ProjectPath...>\packages\MvcScaffolding.1.0.9\tools\registerWithMvcTooling.ps1:163 char:27
+ $newProvider = New-Object <<<< $powerShellScaffolderProviderType($mvcScaffoldingProvider)
+ CategoryInfo : NotSpecified: (:) [New-Object], FileNotFoundException
+ FullyQualifiedErrorId : System.IO.FileNotFoundException, Microsoft.PowerShell.Commands.NewObjectCommand
When I checked the registered scaffolders from the Package Manager Console I got:
PM> get-defaultscaffolder
DefaultName ScaffolderName
Action MvcScaffolding.Action
Controller MvcScaffolding.Controller
CustomScaffolder T4Scaffolding.CustomScaffolder
CustomTemplate T4Scaffolding.CustomTemplate
DbContext T4Scaffolding.EFDbContext
Mailer Mailer.Razor
Repository T4Scaffolding.EFRepository
UnitTest MvcScaffolding.ActionUnitTest
View MvcScaffolding.RazorView
Views MvcScaffolding.Views
So clearly the scaffolders are configured to use the MvcScaffolding package, but the package had a registration error.
A quick search for the error provided this link http://mvcscaffolding.codeplex.com/discussions/437692 which refers to https://visioautomation.codeplex.com/discussions/397034 that suggests installing PowerShell 3, which is actually part of the Windows Management Framework 3.0, available from http://www.microsoft.com/en-us/download/details.aspx?id=34595. I picked the Windows6.1-KB2506143-x64.msu file when prompted because I’m running Win 7-64-bit.
Restart the PC after installation, re-open Visual Studio and the project and look at the Package Manager Console. If the error is gone, then so is the controller problem.
I had this problem on VS2012 on Win8, We where working on a project that we started developing on VS2010 and i wasn't able to create new controllers with the CTRL+M, CTRL+C shortcut.
Opening any project and re-installing MvcScaffolder and its dependencies under Manage Nuget package have fixed this for me.
Didn't have to restart VS2012 at all.
Maybe your existing project have wrong ProjectTypeGuids element in .csproj file?
Right one for MVC4 is {E3E379DF-F4C6-4180-9B81-6769533ABE47}.
Try to compare what project type do you have in new project and in your existing project.
The preferred answer of installing the Windows Management Framework 3.0 did not work. The download "Windows6.1-KB2506143-x86.msu" did not work on my Windows 7 32 bit system. It failed with the error "The update is not applicable to your computer".
Reinstalling MVCscaffolding with Manage Nuget Packages did not work.
My ProjectType GUID was correct.
Microsoft Web Developer Tools was already installed.
Deleting and recreating the solution didn't work.
It was necessary to repair the Visual Studio 2012 installation from the install disk to fix this issue.
Installing "Microsoft Web Developer Tools" fixed this for me. Related:
How do I open a csproj with ProjectTypeGuids 349c5851-65df-11da-9384-00065b846f21?
{E3E379DF-F4C6-4180-9B81-6769533ABE47} - MVC4 Project
{fae04ec0-301f-11d3-bf4b-00c04f79efbc} - CSharp Project
{349c5851-65df-11da-9384-00065b846f21} - Web Application Project (this is what you need web dev tools for)
The update Windows6.1-KB2506143-x64.msu worked for me. The problem started after a full SpyWare software scan. I think that it removes more things than the usual. So, back to normal life.
I had the same problem, im using database first, and it was because i have changed the .edmx contex.tt to generate the contex.cs with the connection name set to a custom method,
: base(sqlHelper.getMainConnectionString())
every time, before i change/update the .edmx, i need to change the connection name to it's creation defaults:
: base("name=YourWebconfigConnectionstringName")
rebuild the project and it works again
Answer:
Check your connectionstring string and provider parameters at the webconfig, then delete and recreate the .edmx this will force the generator to rebuild all the database objects, metadata included OR delete all tables in the designer, change the contex.cs to the default creation name, rebuild the project, add the tables, rebuild the project, it shoul work again.
This problem manifested on a target MVC5 project using VS2017.
Like the original op, a new project was attempted to perform an Add Controller with EF scaffolding against ApplicationUser. The error did initially manifest until a single user was registered forcing the database to be created. After that, add scaffolding worked without incident. I think this makes sense because the entity didn't really yet exist before the first registration, although I will stand corrected if someone more knowledgeable than myself has a more accurate explanation. But because Add Controller worked with a new project, just as the original op determined, it didn't seem to be a VS2017 installation problem. This pointed to something wrong with the taret project.
At this point in the target project an Undo All in Team Explorer was executed to get the project back to a previously working point. While this might be accomplished by getting a specific version, for this target project all that was required was an Undo All. A word of caution: in the case of this specific project, before undoing, the Package Manager was used to rollback the database to the starting point that would match with the Undo All. If one performs a get specific version, be sure to check if the database needs to get rolled back to the proper configuration. After resetting all the project changes, the object reference not set to an instance of an object error was no longer present during the Add Controller operation, fixing the error within the target project.
I post this response somewhat reluctantly because I am unable to duplicate the error, and I am unsure of what actually caused the problem in the first place, but this is a particularly nasty error if one is in the middle of a project and Add Controller starts failing. I figured if someone has this problem and rolling back the current change set might fix it for them, like me they will be thankful. If I later determine the pattern of activity that caused the problem to crop up in the first place, I'll surely add more to this post.
I'm running VS 2019 Enterprise, ASP.NET Core 3.1.3.
I was trying to add MVC controller with views using Entity Framework (EF) after scaffolding in database first approach.
I run on this issue because one of the tables in my ER model was missing primary key.
That table was connected to table for which I tried to generate MVC controller through another table that has primary key so I didn't noticed initially that error.

Could not load file or assembly 'EntityFramework (1)'

Error is as follows:
C:\Users\[username]\Google Drive\VS2010\Projects\[project name]\[project name]\Views\Show\EditOrder.cshtml: ASP.NET runtime error: Could not load file or assembly 'EntityFramework (1)' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference.
Hey all,
Got a strange problem here. seemingly randomly my project completely borked itself. A little bit of background: It's a ASP.Net MVC3 (.Net Framework 4) project that I mostly authored in Visual Web Developer 2010 Express. I say mostly because you'll note in the verbose error above I've stored it on my Google Drive. I'm syncing this project back and forth to my new development laptop, with an install of Visual Studio 2010 Professional. This probably isn't a great idea, but I wanted a simple method of working on it here on my desktop (at the office), at home (on my dev laptop), and on the road (again, dev laptop.)
Any ideas on what might have happened, and how I can fix this?
Bit more of background, now that I'm remembering this ... I had issues publishing it to our local IIS 6 server. I realized I had to add the _bin_deployableAssemblies to the package for it to work. At some point, I had deleted them from the project and re-added them. It was probably around that time, or at least within a few hours, that this issue started happening.
I can has halp?
Your MVC 3 project has a reference to one version of Entity Framework, but actually finds a different version when attempting to resolve the reference to Entity Framework on the computer where you get this error.
The simplest solution is probably to update to the current version of Entity Framework on your current computer, where you get the error.
If you open your project's .csproj file in a regular text editor (e.g. Notepad) you should see a line similar to this one:
<Reference Include="EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\EntityFramework.4.3.1\lib\net40\EntityFramework.dll</HintPath>
</Reference>
That will tell you the version your application is expecting to find. If you see
<SpecificVersion>False</SpecificVersion>
then the expected version or a later version can be used.
Another way to resolve this kind of loading issue is using the Fusion Log Viewer, which will show you binding attempts that fail and what the loader tried (make sure you run it as Administrator or it will silently just do nothing).
Bloody strange ..
Looking in my /[Project Name]/bin folder I found 4 strange files:
EntityFramework (1).dll
EntityFramework (1).xml
[Project Name] (1).dll
[Project Name] (1).pdb
In any case, after deleting them guys and doing a Rebuild, all is well!
(Anyone have any ideas as to how or why this happened? Might be a Google Drive issue, now that I'm thinking about it ...
It was a Google Drive issue for me. If you remove all files ending with (1) in your Project/bin folder it should resolve the problem.

Resources