I'm trying to learn how to use Entity Framework Core migrations, but I get the following error:
Unable to create a 'DbContext' of type ''. The exception 'No suitable constructor was found for entity type 'Movie'. The following constructors had parameters that could not be bound to properties of the entity type:
Cannot bind 'negativePoints', 'positivePoints', 'comments' in 'Movie(int negativePoints, int positivePoints, List comments, string title, MediaType type)'
Note that only mapped properties can be bound to constructor parameters. Navigations to related entities, including references to owned types, cannot be bound.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
From what I understood and researched, it is not possible to perform the Add-Migration
procedure with abstract classes, could you please confirm this or provide me with a tip?
I tested the procedure with a simple class without using Media and I had no problems.
This is my DbContext
class:
public class AppDbContext : DbContext
{
public IConfiguration _config { get; set; }
public AppDbContext(IConfiguration config)
{
_config = config;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(_config.GetConnectionString("DatabaseConnection"));
}
public DbSet<Movie> Movie { get; set; }
//public DbSet<Game> Game { get; set; }
//public DbSet<User> User { get; set; }
}
My Media
class:
public abstract class Media
{
public Guid Id { get; set; }
public int? NegativePoints { get; set; }
public int? PositivePoints { get; set; }
public List<Comment>? Comments { get; set; }
public string Title { get; set; }
public MediaType Type { get; set; }
public Media(int negativePoints, int positivePoints, List<Comment> comments, string title, MediaType type)
{
Id = new Guid();
NegativePoints = negativePoints;
PositivePoints = positivePoints;
Comments = comments;
Title = title;
Type = Type;
}
}
My Movie
class:
public class Movie : Media
{
public string? Director { get; set; }
public string? Genre { get; set; }
public DateTime? Release { get; set; }
public string? AgeRating { get; set; }
public string? Synopsis { get; set; }
public List<string>? Actors { get; set; }
public TimeOnly? Duration { get; set; }
public string? Language { get; set; }
public string? ContryOrigin { get; set; }
public string? ProductionStudio { get; set; }
public string? Trailer { get; set; }
public Movie(int negativePoints, int positivePoints, List<Comment> comments, string title, MediaType type) : base(negativePoints, positivePoints, comments, title, type)
{
Type = MediaType.Movie;
Director = null;
Genre = null;
Release = null;
AgeRating = null;
Synopsis = null;
Actors = new List<string>();
Duration = null;
Language = null;
ContryOrigin = null;
ProductionStudio = null;
Trailer = null;
}
}
If you have any additional suggestions I'll be happy to hear from you.
I'm using .NET 8.0
You need a constructor with no parameters. Your abstract class will also need a constructor with no parameters.
public class Movie : Media
{
public string? Director { get; set; }
public string? Genre { get; set; }
public DateTime? Release { get; set; }
public string? AgeRating { get; set; }
public string? Synopsis { get; set; }
public List<string>? Actors { get; set; }
public TimeOnly? Duration { get; set; }
public string? Language { get; set; }
public string? ContryOrigin { get; set; }
public string? ProductionStudio { get; set; }
public string? Trailer { get; set; }
/*** ADD THIS HERE ***/
public Movie(){
}
public Movie(int negativePoints, int positivePoints, List<Comment> comments, string title, MediaType type) : base(negativePoints, positivePoints, comments, title, type)
{
Type = MediaType.Movie;
Director = null;
Genre = null;
Release = null;
AgeRating = null;
Synopsis = null;
Actors = new List<string>();
Duration = null;
Language = null;
ContryOrigin = null;
ProductionStudio = null;
Trailer = null;
}
}
public abstract class Media
{
public Guid Id { get; set; }
public int? NegativePoints { get; set; }
public int? PositivePoints { get; set; }
public List<Comment>? Comments { get; set; }
public string Title { get; set; }
public MediaType Type { get; set; }
/*** ADD THIS HERE ***/
public Media(){
}
public Media(int negativePoints, int positivePoints, List<Comment> comments, string title, MediaType type)
{
Id = new Guid();
NegativePoints = negativePoints;
PositivePoints = positivePoints;
Comments = comments;
Title = title;
Type = Type;
}
}