My app broke with the 3.0 release of .NET core with reference errors for IdentityDbContext. I'm looking through documentation for Identity on core 3.0 but it implies that IdentityDbContext should be there. It's the only error I'm getting with a couple DbContext errors.
I have a pretty simple API, no MVC views, just a data server that gives back JSON objects. It's based on Identity so it has the users and roles and claims. And it's starting to take advantage of that. My main DbContext extends IdentityDbContext<ApplicationUser> but after switching target platform to 3.0 after the upgrade, it says it doesn't exist and gives me compile errors. Has anyone run into this? Am I missing something? The migration and breaking changes pages don't seem to have anything addressing my issue.
DbContext looks like this:
using System;
using Microsoft.AspNetCore.Identity;
//using Microsoft.AspNetCore.Identity.EntityFrameworkCore; <- this no longer works either
using Microsoft.EntityFrameworkCore; //<- this I had to download as a package
using App.Constants;
using App.Models.Identity;
namespace App.Models
{
public class AppContext : IdentityDbContext<ApplicationUser> //<- error is right here
{
... my models
}
}
In ASP.NET Core 3.0, Entity Framework Core and Identity related packages have been removed from the Microsoft.AspNetCore.App metapackage. So you have to add those packages separately.
Add the following PackageReferences to your project's .csproj file as follows:
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0" />
</ItemGroup>
Now it will work!
For more details: Assemblies removed from the ASP.NET Core shared framework
if you have an error like "error CS0246: The type or namespace name 'IdentityDbContext' could not be found"
you can install bellow package for asp.net web api 3 project
dotnet add package Microsoft.AspNetCore.Identity.EntityFrameworkCore --version 5.0.0-preview.3.20215.14
in dotnet cli.its work for me.
Install-Package Microsoft.AspNetCore.Identity.EntityFrameworkCore -Version 5.0.0-preview.3.20215.14
this for package manager
Related
I m working with N-tier architecture with .netcore 2.2
see how my project structure:
MainProject Version: how I m create the project(ASP.NET Core Web Application->Next->create->Empty(select the DropDown ASP.NET Core 2.2)
business logic project Version: how I m create the project(class library .net core)
data access project Version: how I m create the project(class library .net core)
model project Version: how I m create the project(class library .net core)
Error is:
Severity Code Description Project File Line Suppression State
Error NU1107 Version conflict detected for Microsoft.EntityFrameworkCore. Install/reference Microsoft.EntityFrameworkCore 3.1.2 directly to the project to resolve this issue.
projectname-> projectname.BusinessLogic -> projectname.DataAccess -> Microsoft.EntityFrameworkCore (>= 3.1.2)
projectname-> Microsoft.AspNetCore.App 2.2.0 -> Microsoft.EntityFrameworkCore (>= 2.2.0 && < 2.3.0). projectname
when I click on an error then this file display:
projectname.csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DMEBase.BusinessLogic\DMEBase.BusinessLogic.csproj" />
<ProjectReference Include="..\DMEBase.Model\DMEBase.Model.csproj" />
</ItemGroup>
</Project>
Only 1 error is generated in my project
how to solve this issue? help
NuGet Packages ScreenShot:
Write Click On Solution then I m going to Managenugetpackages->Consolidate->No Packages Found:
Downgrade entity framework core version as compatible core framework.
I am starting to lean asp.net core 3 and I have an issue at present. I am trying to share my db context like I used to through a .net core dll but I have an error.
I have the following declared in my class.
// This method gets called by the runtime. Use this method to add services to e container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<DbContext_Model>(options =>
options.UseSqlServer(Configuration.GetConnectionString("AppliedContext")));
}
The db context which I have in my core dll project solution.
public class DbContext_Model: DbContext
{
public DbContext_Model(DbContextOptions<DbContext_Model> options)
: base(options)
{ }
public DbSet<Customer> Customer { get; set; }
public DbSet<Policys> Policys { get; set; }
}
I have declared my connection string below with password masked
"ConnectionStrings": {
"AppliedContext": "Server=DESKTOP\MSSQLSERVER2017;Database=Systems;User
Id=sa;Password=xxxxx;MultipleActiveResultSets=true"
But When I do Add-Migration InitalCreate I get this error.
Unable to create an object of type 'DbContext_Model'. For the
different patterns supported at design time, see
https://go.microsoft.com/fwlink/?linkid=851728
Is sharing of the context no longer allowed between .net core libarys and a web site ?
Edit 2
Please see screen shot which shows I am selecting the correct project.
Edit 3
That seemed to cause an another error the suggestion below. So I added the nugets in for the json tooling but still get this error.
dotnet ef migrations add InitialCreate System.MissingMethodException:
Method not found: 'System.Text.Json.JsonDocument
System.Text.Json.JsonDocument.Parse(System.IO.Stream,
System.Text.Json.JsonReaderOptions)'. at
Microsoft.EntityFrameworkCore.Tools.RootCommand.Execute() at
Microsoft.EntityFrameworkCore.Tools.Commands.CommandBase.<>c__DisplayClass0_0.b__0()
at
Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute(String[]
args) at Microsoft.EntityFrameworkCore.Tools.Program.Main(String[]
args) Method not found: 'System.Text.Json.JsonDocument
System.Text.Json.JsonDocument.Parse(System.IO.Stream,
System.Text.Json.JsonReaderOptions)'. PM> dotnet ef migrations add
InitialCreate
Try something like this:
options.UseSqlServer(
Configuration.GetConnectionString("AppliedContext"),
options => options.MigrationsAssembly(<DB_PROJECT>)
Then you should be able to run this from within the folder that has the DbContext project:
dotnet ef migrations add <NAME> --startup-project <STARTUP_PROJECT>
From the screenshot in your Edit3, your need to update EF Core related package to version 3.0 . In Manage NutGet Packages , check Include prerelease next to the search box and update them to latest version like below :
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.0.0-preview8.19405.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.0.0-preview8.19405.11">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0-preview8.19405.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0-preview8.19405.11">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
Note :When adding migration , set your web application as the Startup project and set the class library as the default project where you want Migration folder generates.
Reference :https://dotnetthoughts.net/using-ef-core-in-a-separate-class-library/
The screenshot of your Edit3 seems to indicate that your EF tool version is outdated. You can check by executing:
dotnet ef --version
on the command line. If it's not preview8, then install it:
dotnet tool update dotnet-ef --version 3.0.0-preview8.19405.11
(optionally use the --global switch).
Beware: EF preview8 has an additional issue when used with HasData. It should be fixed in preview9 (which at the time of this writing is not out yet).
I am standing up a NUnit framework in VSCode but am running into an issue when setting up PageFactory within my page ctor. I have added DotNetSeleniumExtras.PageObjects to my .csproj but am unable to call PageFactory.InitElements(driver,this) even with the using SeleniumExtras.PageObjects in place.
I have seen the articles/videos regarding PageObjects being removed from .Net Core but I thought (perhaps naively) that DotNetSeleniumExtras would solve this.
Has anyone gotten this working since PageObjects were pulled out into their own project?
Here are the steps I've followed in getting setup:
1) Install VSCode
2) Create New Folder
3) Open Folder in VSCode
4) Install .Net Core
5) Install Extensions:
C#
.Net Core Test Explorer
Show Test Results (TDD Support for NUnit .Net Core)
6) Add NUnit Template:
dotnet new -i NUnit3.DotNetNew.Template
7) Create a project:
dotnet new nunit
8) Add Packages:
dotnet add package Selenium.Webdriver
dotnet add package Selenium.Support
dotnet add package DotNetSeleniumExtras.PageObjects
dotnet add package NUnit
dotnet add package NUnit3TestAdapter
dotnet add package Microsoft.NET.Test.Sdk
.csproj Contents
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DotNetSeleniumExtras.PageObjects" Version="3.11.0" />
<PackageReference Include="nunit" Version="3.10.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.6.1" />
<PackageReference Include="Selenium.Support" Version="3.11.0" />
<PackageReference Include="Selenium.Webdriver" Version="3.11.0" />
</ItemGroup>
</Project>
Page.cs
using NUnit.Framework;
using System;
using SeleniumExtras.PageObjects;
using OpenQA.Selenium;
namespace IS.Pages
{
class Landing
{
public Landing()
{
PageFactory.InitElements(DriverUtil.driver,this);
}
// Start Search Button
[FindsBy(How = How.CssSelector, Using = "body > main > header > div.background-wrapper > div > div > div:nth-child(1) > div > a")]
public IWebElement AdvancedSearch {get; set;}
Sigh,
I didn't look at the Dependancies for DotNetSeleniumExtras. Not supported (yet) by .Net Core
I can create a new dotnetcore app that has authentication/identity using the command line:
dotnet new mvc --auth Individual
How can i include entity framework in the project also?
TL;DR
You already have it in your project
Long form answer
After creating your application, it should have Entity Framework as a dependency. I'm assuming that you're running the .NET Core 2.0 SDK.
Here's the output from my machine
$ dotnet new mvc --auth Individual --name testForStackOverflow
The template "ASP.NET Core Web App (Model-View-Controller)" was created successfully.
This template contains technologies from parties other than Microsoft, see https://aka.ms/template-3pn for details.
Processing post-creation actions...
Running 'dotnet restore' on testForStackOverflow/testForStackOverflow.csproj...
Restoring packages for testForStackOverflow/testForStackOverflow.csproj...
Restore completed in 40.17 ms for testForStackOverflow/testForStackOverflow.csproj.
Restore completed in 40.17 ms for testForStackOverflow/testForStackOverflow.csproj.
Restore completed in 25.25 ms for testForStackOverflow/testForStackOverflow.csproj.
Generating MSBuild file testForStackOverflow/obj/testForStackOverflow.csproj.nuget.g.props.
Generating MSBuild file testForStackOverflow/obj/testForStackOverflow.csproj.nuget.g.targets.
Restore completed in 2.84 sec for testForStackOverflow/testForStackOverflow.csproj.
Restore succeeded.
I then look a look at the csproj which was generated:
$ cd testForStackOverflow/
~/testForStackOverflow$ ls
app.db Data Startup.cs
appsettings.Development.json Extensions testForStackOverflow.csproj
appsettings.json Models Views
bower.json obj wwwroot
bundleconfig.json Program.cs
Controllers Services
~/testForStackOverflow$ cat testForStackOverflow.csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<UserSecretsId>aspnet-testForStackOverflow-AD382505-1A70-4A75-8059-1E0E3897A088</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<None Update="app.db" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.0" />
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
</ItemGroup>
</Project>
The important line of the csproj is here:
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
This is a reference to the ASP.NET Core metapackage. This package contains all of the common ASP.NET Core NuGet packages, including Entity Framework Core - as per this screen shot from NuGet (captured a few moments ago)
This means that part of the package restore operation included restoring EF Core into your project.
I would say that you should take a look at this documentation for EF Core - the link should take you directly to the section labelled "The Model". You don't need the stuff in the preceding section (labelled "Get Entity Framework Core") as you already have it.
Of course, if you're using version 1.x of the .NET Core SDK, then it's a slightly different story.
I tried to setup a .net core console application which uses EF core 2.0.0 and Redis.Core 1.0.3 (both are the current latest version).
Unfortunately if you try to put both into one application it won't even start. Even if you try to set a breakpoint right at the beginning or start debugging with Step into or Step Over doesn't help. All you get is this:
I think there happens some kind of type load exception or something similar. So far to reproduce the problem is quite easy with this project:
Project File (MyProject.csproj)
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Redis.Core" Version="1.0.3" />
</ItemGroup>
</Project>
Application (Program.cs)
using Microsoft.Extensions.Caching.Redis;
using Microsoft.Extensions.Options;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
var cache = new RedisCache(Options.Create(new RedisCacheOptions()));
}
}
}
Anybody an idea or solution?
The problem is that
Microsoft.Extensions.Caching.Redis.RedisCache 1.0.3 depends on Microsoft.Extensions.Caching.Abstractions 1.0.3
Microsoft.EntityFrameworkCore 2.0.0 depends on Microsoft.Extensions.Caching.Abstractions 2.0.0
If you look at the Output Window, you will get:
TypeLoadException: Method GetAsync in type
Microsoft.Extensions.Caching.Redis.RedisCache from assembly
'Microsoft.Extensions.Caching.Redis, Version=1.0.3.0, Culture=neutral,
PublicKeyToken=adb9793829ddae60' does not have an implementation.
So: either wait until RedisCache gets updated to 2.0.0 or use EntityFrameworkCore < 2.0.0