Unable to add migration on dotnet - asp.net-core

Hello everyone I am trying to add migration on my project by following code
add-migration InitialCreate
I am the facing issue while adding migration. The error says
Method not found:
'Microsoft.EntityFrameworkCore.Metadata.Builders.IndexBuilder
Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder`1.HasIndex
What's going on here. How to solve this????
Packages installed on my project

There might problen of version mismatching. Please update your Openiddict to 2.0.1 like
<PackageReference Include="OpenIddict" Version="2.0.1" />
<PackageReference Include="OpenIddict.EntityFrameworkCore" Version="2.0.1" />
<PackageReference Include="OpenIddict.Mvc" Version="2.0.1" />

Related

ASP.NET Core Controllers Fail to Register with Authorization Package

My asp.net core 3.1 project throws an exception if I include this package (context: I’m trying to create a custom Authorization policy):
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="5.0.3" />
This is the exception and method where it is thrown in startup.cs:
services.AddControllers();
System.ArgumentNullException: 'Value cannot be null. (Parameter 'configure')'
This is the complete list of packages. Is there some conflict that I'm not aware of? The package was suggested by VS when I created an AuthorizationHandler class.
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.0.1" />
<PackageReference Include="Azure.Identity" Version="1.2.3" />
<PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.1.0" />
<PackageReference Include="CorrelationId" Version="3.0.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.15.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="3.1.7" />
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="5.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.9" />
<PackageReference Include="Microsoft.AspNetCore.OData" Version="7.5.2" />
<PackageReference Include="Microsoft.Identity.Web" Version="1.2.0" />
<PackageReference Include="NSwag.AspNetCore" Version="13.8.2" />
Oops, looks like the answer was already available with a little more effort on my part. I resolved this by manually downgrading the package version to 3.1.12 as described here. Looks like VS gave me the .net version of the package instead of the .net core version.
I was using an authorization handler form a .NET Standard 2 library where v.6.0.x was installed. Downgrading was not an option for me.
Exchanging services.AddControllers() with services.AddMvcCore() in my Core 3.1 project solved the problem for me.

Azure DevOps build pipline constantly giving version conflict on every package

I have a simple .NET Core 2.2 website along with Microsoft.Identity.Web added as a .NET Core assembly. The following .csproj files make up my solution:
Main project:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<WebProject_DirectoryAccessLevelKey>1</WebProject_DirectoryAccessLevelKey>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.Graph" Version="1.14.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.7.2" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Microsoft.Identity.Web\Microsoft.Identity.Web.csproj" />
</ItemGroup>
<ItemGroup>
<WCFMetadata Include="Connected Services" />
</ItemGroup>
</Project>
Microsoft.Identity.Web project:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="2.2.0" />
<PackageReference Include="Microsoft.Identity.Client" Version="3.0.6-preview" />
</ItemGroup>
</Project>
NOTE: I can build the solution locally without issue and have never had an issue with it building or running -ever.
I created a new Azure DevOps pipeline and pointed it at my Github repo. During the NuGetCommand process it fails telling me:
[error]The nuget command failed with exit code(1) and error(NU1607: Version conflict detected for Microsoft.AspNetCore.Antiforgery. Reference the package directly from the project to resolve this issue.
Based on this I thought I needed to install that package directly so I went to NuGet package manager, searched for the package, and installed it. Then I get a different error:
[error]The nuget command failed with exit code(1) and error(NU1607: Version conflict detected for Microsoft.AspNetCore.Authentication.Cookies. Reference the package directly from the project to resolve this issue.
Tried installing that package and I get another error:
[error]The nuget command failed with exit code(1) and error(NU1607: Version conflict detected for Microsoft.AspNetCore.Authorization.Policy. Reference the package directly from the project to resolve this issue.
..and so on and so on...
So installing these erroneous packages is not likely the right way forward so I also tried:
Changing my main .csproj file to use <PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.0" /> but that just gave more of the same above errors (different ones) and according to Nate here we shouldn't be including explicit versions.
At one point after installing all these packages it seemed to be hung on the <PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="2.2.0" /> reference so following guidance from a colleague I changed that version to 2.1.0 but again, no resolution; just more red herrings.
I've been fighting with this for four days now and I'm completely lost on how to solve it. Any guidance would be appreciated.
Update:
Initially I had set up my DevOps build pipeline by following the 'wizard' steps within DevOps itself. I chose my repo and it selected my master branch automatically including setting up a yaml file. I ran the build as the last step in the wizard and that's where my problems started.
I thought I would take a different approach in troubleshooting by setting up my Azure resource group first, then my app service, dependent PaaS services, and finally configure the 'deployment options' section. I followed the steps there to choose Azure DevOps as my build/deploy pipeline, selected my organization, repo, and clicked finish. It set up the build and deploy without issue.
I'll leave this post here for suggestions and hopefully have a definitive answer to why it didn't work. I feel like the errors with my first approach were red herrings all together though...

How to resolve the Testserver problem in XUnit Test?

I am using xunit for testing my project. I was added all dependencies but i am getting following error.
The type or namespace name 'TestServer' could not be found (are you missing a using directive or an assembly reference?)
My test3.Tests.csproj dependencies
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>
How can i resolve the problem.Any one try to help me thank you..
Try to install Microsoft.AspNetCore.Mvc.Testing.
Infrastructure components, such as the test web host and in-memory test server (TestServer), are provided or managed by the Microsoft.AspNetCore.Mvc.Testing package. Use of this package streamlines test creation and execution.
Test app prerequisites

Netcore 2.1.1 release leads to app failing to run

It seems there is suddenly something out of sync with asp.net core packages.
It looks like Microsoft.AspNetCore.App 2.1.1 was released 16 hours ago on nuget.org but only 158 downloads, and apparently I'm one of the downloaders.
My .csproj file is like this:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\cloudscribe.Web.Navigation\cloudscribe.Web.Navigation.csproj" />
<ProjectReference Include="..\cloudscribe.Web.SiteMap\cloudscribe.Web.SiteMap.csproj" />
<ProjectReference Include="..\cloudscribe.Web.SiteMap.FromNavigation\cloudscribe.Web.SiteMap.FromNavigation.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.*" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.*" />
</ItemGroup>
</Project>
It builds but when I try to run it I get:
It was not possible to find any compatible framework version
The specified framework 'Microsoft.AspNetCore.App', version '2.1.1' was not found.
- Check application dependencies and target a framework version installed at:
C:\Program Files\dotnet\
- Installing .NET Core prerequisites might help resolve this problem:
http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
- The .NET Core framework and SDK can be installed from:
https://aka.ms/dotnet-download
- The following versions are installed:
2.1.0 at [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
If I change my version for Microsoft.AspNetCore.App to 2.1.0 and do dotnet restore I get this:
C:_c\cloudscribe.Web.Navigation\src\NavigationDemo.Web\NavigationDemo.Web.csproj : error NU1107: Version conflict detected for Microsoft.AspNetCore.Antiforgery. Reference the package directly from the project to resolve this issue.
C:_c\cloudscribe.Web.Navigation\src\NavigationDemo.Web\NavigationDemo.Web.csproj : error NU1107: NavigationDemo.Web -> cloudscribe.Web.Navigation -> Microsoft.AspNetCore.Mvc.Razor 2.1.1 -> Microsoft.AspNetCore.Mvc.ViewFeatures 2.1.1 -> Microsoft.AspNetCore.Antiforgery (>= 2.1.1)
C:_c\cloudscribe.Web.Navigation\src\NavigationDemo.Web\NavigationDemo.Web.csproj : error NU1107: NavigationDemo.Web -> Microsoft.AspNetCore.App 2.1.0 -> Microsoft.AspNetCore.Antiforgery (= 2.1.0).
Restore failed in 3.54 sec for C:_c\cloudscribe.Web.Navigation\src\NavigationDemo.Web\NavigationDemo.Web.csproj.
I suppose that is partly due to the other projects in my solution with dependencies like this:
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.*" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="2.1.*" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="2.1.*" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.1.*" />
<PackageReference Include="Microsoft.Extensions.Options" Version="2.1.*" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.*" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.1.*" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor" Version="2.1.*" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.1.*" />
There does not seem to be an updated sdk for 2.1.1 but the availability of Microsoft.AspNetCore.App 2.1.1 on GitHub seems to be breaking things for me.
The 2.1.1 (preview) SDK is available on their GitHub.
You can download directly here:
https://dotnetfeed.blob.core.windows.net/orchestrated-release-2-1/20180605-09/final/assets/Sdk/2.1.301-preview-008906/dotnet-sdk-2.1.301-win-x64.exe
See:
https://github.com/dotnet/versions/tree/7a833dddfddc27f2074b755b94234a25b9757637/build-info/dotnet/product/cli/release/2.1
We are still waiting on the official SDK...
Edit:
If you are having trouble building, add the following to your .csproj
<PropertyGroup>
<TargetLatestAspNetCoreRuntimePatch>true</TargetLatestAspNetCoreRuntimePatch>
</PropertyGroup>
Update:
Official 2.1.1 SDK now released: https://www.microsoft.com/net/download/thank-you/dotnet-sdk-2.1.301-windows-x64-installer
Your problem is this line of your csproj:
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.*" />
The * is saying to pick the latest 2.1 version of that NuGet package. For many packages, this would be perfectly fine. However, v2.1.1 of that package requires a matching v2.1.1 SDK to also be installed. As of right now, it is not available (it's currently blocked).
However, if you read the Migrate from ASP.NET Core 2.0 to 2.1 docs, you will see this:
Replace the version specified "Microsoft.AspNetCore.All" package reference with the versionless "Microsoft.AspNetCore.App" package reference.
The version is now inferred by the version of the SDK you are targeting. This means your csproj should now contain this:
<PackageReference Include="Microsoft.AspNetCore.App" />
In addition to other suggestions, I needed to up the Microsoft.NetCore.App to 2.1.1. Since I couldn't do it via the UI, adding the following to the .csproj worked.
<PackageReference Include="Microsoft.NetCore.App" Version="2.1.1" />
I had the same issue. Another option is, in NuGet Package Manager, rollback Microsoft.AspNetCore.All, Microsoft.AspNetCore.Rewrite, and Microsoft.NETCore.App to versions 2.1.0.
The wildcard version number package references is weird to me. That doesn't happen by default, so either you or one of your team members did that. I don't know if that's the source of at least some of your issues or not, but I'd start by changing them to full version references. NuGet packages are not committed with your project, so having wildcards can lead to strange build issues, particularly in automated builds, when you're developing against one version but a different version is restored later. If you want to upgrade a package, that should be an explicit choice, so you can then verify that all your code still functions as it should post-upgrade.
Also worth noting, since 2.1, code generation has been rolled in, so you should not be explicitly referencing any CodeGen packages.
As far as solving the current issue goes. You may be having some issues with your package cache. In Visual Studio, go to Tools > NuGet Package Manager > Package Management Settings, and then click the "Clear All NuGet Cache(s)" button.
That should prompt a redownload of all your projects NuGet packages, and may be enough to solve the issue. If not, I'd recommend cleaning all your projects and then going into the directory for each and manually deleting both the bin and obj directories. Then, rebuild your solution.
For bugged SDK 2.1.301 version use:
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.0" />
and force all other packages 2.1.1 -> 2.1.0.
Installing 2.1.301 (https://github.com/dotnet/core/blob/master/release-notes/download-archives/2.1.1-download.md) fixed this issue for me.

Can't Add Migration in Visual Studio Code for EntityFrameworkCore

I'm trying to build a DotNetCore WebApi in Visual Studio Code with EntityFrameworkCore.
I've been reading a number of tutorials and I always get stuck at the point where it says:
Run Add-Migration InitialCreate
I get the following error:
The term 'add-migration' is not recognized as the name of a cmdlet, function, script file, or operable program
Other tutorials say I should run this command
dotnet ef migrations add InitialCreate
Which gives me the error:
No executable found matching command "dotnet-ef"
These are the references in my csproj:
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3"/>
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2"/>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="1.1.0"/>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.2"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.1"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.1.0-preview4-final"/>
How can I add a migration?
To be able to use the command line CLI to manage migrations, you need to have the tools as part of your project. Make sure you have something like this in your project:
<ItemGroup>
<DotNetCliToolReference
Include="Microsoft.EntityFrameworkCore.Tools.Dotnet"
Version="1.0.1" />
</ItemGroup>
Next from the command line, make sure you are in the same directory as the csproj file and run this:
dotnet ef migrations add InitialCreate
to make the "dotnet-ef" command available in Visual Studio Code,Install the dotnet ef tool by running the preceding command.
dotnet tool install --global dotnet-ef
Adding a migration:
dotnet ef migrations add MigrationName
try executing
dotnet ef database update