admin管理员组

文章数量:1123234

Code:

public class RepositoryContext : DbContext
{
    public RepositoryContext(DbContextOptions options)
        : base(options)
    {
    }
}

error:

The type or namespace name 'DbContextOptions' could not be found (are you missing a using directive or an assembly reference?)
Repository D:\My Work\Algos\Repository\RepositoryContext.cs

I am trying to create a DbContext class here. Is there any issue? What is missing here? Do I need some namespaces also?

I am getting this error for the first time.

Code:

public class RepositoryContext : DbContext
{
    public RepositoryContext(DbContextOptions options)
        : base(options)
    {
    }
}

error:

The type or namespace name 'DbContextOptions' could not be found (are you missing a using directive or an assembly reference?)
Repository D:\My Work\Algos\Repository\RepositoryContext.cs

I am trying to create a DbContext class here. Is there any issue? What is missing here? Do I need some namespaces also?

I am getting this error for the first time.

Share Improve this question edited 5 hours ago James Z 12.3k10 gold badges27 silver badges47 bronze badges asked 9 hours ago sampath ssampath s 151 silver badge7 bronze badges 13
  • 2 Please add code and compilation error as text. – Guru Stron Commented 8 hours ago
  • 1 shouldn't it be public RepositoryContext(DbContextOptions<RepositoryContext> options) : base(options) {} – Bryan Dellinger Commented 8 hours ago
  • 1 Can you please share the csproj file and/or using statements on top of the context file? Is there any chance that you are using EF 6 instead of EF Core? – Guru Stron Commented 8 hours ago
  • 2 are you missing a using directive have you added the correct using statement? Have you tried right clicking on the error and selecting a fix? Images don't help at all. You need to provide enough code to reproduce the problem and the full error text. Needless to say, EF Core works fine in all ASP.NET Core applications – Panagiotis Kanavos Commented 8 hours ago
  • 2 @sampaths what you just posted shows you don't have the correct usings. //using Microsoft.EntityFrameworkCore; is a comment and using System.Data.Entity; is the old EF namespace. It's not used at all in EF Core – Panagiotis Kanavos Commented 8 hours ago
 |  Show 8 more comments

1 Answer 1

Reset to default 2

You referenced EntityFramework instead of EntityFrameworkCore.

You need this package: https://www.nuget.org/packages/Microsoft.EntityFrameworkCore

And this namespace: using Microsoft.EntityFrameworkCore;

本文标签: DbContextOptions error in ASPNET Core Web API solutionStack Overflow