首先,我为我的英语道歉。
我正在尝试学习如何使用 EntityFrameworkCore 迁移,但出现以下错误:
!无法创建类型为“”的“DbContext”。异常“找不到实体类型“电影”的合适构造函数。以下构造函数的参数无法绑定到实体类型的属性: 无法在“Movie(int negativePoints, int PositivePoints, List comments, string title, MediaType type)”中绑定“negativePoints”、“positivePoints”、“comments” 请注意,只有映射的属性可以绑定到构造函数参数。无法绑定对相关实体的导航,包括对拥有类型的引用。尝试创建实例时抛出。有关设计时支持的不同模式,请参阅 https://go.microsoft.com/fwlink/?linkid=851728
根据我的理解和研究,不可能使用抽象类执行“添加迁移”过程,您能否确认这一点或为我提供提示?
我用一个简单的类测试了该过程,没有使用媒体,没有任何问题。
我的 dbcontext 类:
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; }
}
我的媒体课:
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;
}
}
我的电影课:
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;
}
}
由于我是初学者,如果您有任何其他建议,我将很高兴收到您的来信。
使用.net 8.0
您需要一个不带参数的构造函数。
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;
}
}