I’m writing my first application using minimal APIs in a .NET 8 application. I’m following a training exercise I found on Microsoft Learn, and I’m using a database I designed. I ran into this error:
CS0311 The type ‘Show1’ cannot be used as type parameter
‘TImplementation’ in the generic type or method
‘ServiceCollectionServiceExtensions.AddScoped<TService,
TImplementation>(IServiceCollection)’. There is no implicit reference
conversion from ‘Show1’ to ‘BlazorMediaLibrary.API.Interfaces.IShow1’
I searched this error and came up with Why does a generic type constraint result in a no implicit reference conversion error? here on SO. However, I don’t think this fits my situation. Here’s what I got for my Interface, which was generated when I scaffolded the database to create the model classes and DbContext:
public interface IShow1
{
int ShowId { get; set; }
int ShowCode { get; set; }
string EpisodeName { get; set; }
string? Synopsis { get; set; }
DateTime? DateLastViewed { get; set; }
DateTime? OriginalBroadcastDate { get; set; }
TimeOnly? PlayTime { get; set; }
double? Rating { get; set; }
TypeOfShow1 ShowCodeNavigation { get; set; }
ICollection<Entries2> Entries2s { get; set; }
ICollection<UserRating> UserRatings { get; set; }
}
and here’s the model class implementation:
public partial class Show1 : IShow1
{
public int ShowId { get; set; }
public int ShowCode { get; set; }
public string EpisodeName { get; set; } = null!;
public string? Synopsis { get; set; }
public DateTime? DateLastViewed { get; set; }
public DateTime? OriginalBroadcastDate { get; set; }
public TimeOnly? PlayTime { get; set; }
public double? Rating { get; set; }
public virtual TypeOfShow1 ShowCodeNavigation { get; set; } = null!;
public virtual ICollection<Entries2> Entries2s { get; set; } = new List<Entries2>();
public virtual ICollection<UserRating> UserRatings { get; set; } = new List<UserRating>();
}
In the Program.cs file I defined this record:
internal record Show1(int ShowId, int ShowCode, string EpisodeName, string? Synopsis, DateTime? DateLastViewed, DateTime? OriginalBoardcastDate, TimeOnly? PlayTime, float? Rating);
And here’s the line in Program.cs which is raising the error:
builder.Services.AddScoped<IShow1, Show1>();
I didn’t include ShowCodeNavigation, Entries2, or UserRatings in the record definition in Program.cs. Is that what’s causing my problem?