类型'namespace'不能用作泛型类型或方法'DbContextOptions<TContext>'中的类型参数'TContext'



你好,我收到了完整的错误消息:The type 'Album.Api.Models.Album' cannot be used as type parameter 'TContext' in the generic type or method 'DbContextOptions<TContext>'. There is no implicit reference conversion from 'Album.Api.Models.Album' to 'Microsoft.EntityFrameworkCore.DbContext'. [Album.Api]

我试图进行dotnet-ef迁移,但我不得不添加构造函数。我添加了构造函数,但我不知道我做错了什么。这是我的相册.cs模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Npgsql.EntityFrameworkCore.PostgreSQL;
namespace Album.Api.Models
{

public class AlbumContext : DbContext
{
public AlbumContext(DbContextOptions<Album> options)
: base(options)
{
}
public DbSet<Album> Albums {get; set;}

}





public class Album
{
public long Id {get; set;}
public string Artist {get; set;}
public string Name {get; set;}
public string ImageUrl {get; set;}
}
}

如果有必要,这是我的startup.cs配置:

public void ConfigureServices(IServiceCollection services)
{

services.AddDbContext<AlbumContext>(options =>
options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));
}

Album类是您的实体。DbContextOptions类用于您的上下文,在本例中为AlbumContext。所以你需要更改这一行:

public AlbumContext(DbContextOptions<Album> options)

到此:

public AlbumContext(DbContextOptions<AlbumContext> options)

对于迁移,您可能还需要实现IDesignTimeDbContextFactory。看见https://learn.microsoft.com/en-us/ef/core/cli/dbcontext-creation?tabs=dotnet-核心cli

一些调试提示。。。编译器错误会告诉你是哪一行导致了错误,这样你就可以缩小它的范围。如果您使用的是VisualStudio,您可以单击一个标识符,然后按F12以获取有关它的更多信息。默认情况下,文档注释是折叠的,但对于包含它们的任何包(包括大多数MS包(,您可以展开它们以获取有关该标识符的基本信息。

相关内容

最新更新