【发布时间】:2021-06-28 11:56:09
【问题描述】:
您好,我收到了完整的错误消息: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 迁移,我不得不添加构造函数。我添加了构造函数,但我不知道我做错了什么。 这是我的 Album.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")));
}
【问题讨论】:
-
你想要
public AlbumContext(DbContextOptions<AlbumContext> options) ...错误消息对此非常清楚。它告诉你它不能将Album转换为DbContext,因为Album不继承自DbContext,AlbumContext确实
标签: c# entity-framework .net-core dbcontext