Reputation: 63
Hello I'm getting this full error message: 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]
I tried to do dotnet ef migrations and I had to add the constructor. I added the constructor but I don't know what I'm doing wrong. This is my Album.cs model:
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;}
}
}
And this is my startup.cs configuring if it is necessary:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AlbumContext>(options =>
options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));
}
Upvotes: 0
Views: 1275
Reputation: 4443
The Album
class is your entity. The DbContextOptions
class is for your context, which in this case is AlbumContext
. So you need to change this line:
public AlbumContext(DbContextOptions<Album> options)
to this:
public AlbumContext(DbContextOptions<AlbumContext> options)
For migrations, you may also need to implement IDesignTimeDbContextFactory
. See https://learn.microsoft.com/en-us/ef/core/cli/dbcontext-creation?tabs=dotnet-core-cli
Some debugging tips... The compiler error will tell you which line was causing the error so that you can narrow it down. If you are using VisualStudio, you can click on an identifier and press F12 to get more information about it. The documentation comments are collapsed by default, but for any packages that include them (including most MS packages) you can expand them to get basic information about the identifier.
Upvotes: 1