Entity Framework Core 添加-带有抽象类的迁移

问题描述 投票:0回答:2

首先,我为我的英语道歉。我正在尝试学习如何使用 Entity Framework Core 迁移,但出现以下错误:

无法创建类型为“”的“DbContext”。异常“找不到实体类型“电影”的合适构造函数。以下构造函数的参数无法绑定到实体类型的属性:

无法在“Movie(int NegativePoints, int PositivePoints, List comments, string title, MediaType type)”中绑定“negativePoints”、“positivePoints”、“comments”

请注意,只有映射属性才能绑定到构造函数参数。无法绑定对相关实体的导航,包括对拥有类型的引用。尝试创建实例时抛出。有关设计时支持的不同模式,请参阅 https://go.microsoft.com/fwlink/?linkid=851728

根据我的理解和研究,不可能使用抽象类执行

Add-Migration
过程,您能否确认这一点或为我提供提示?

我用一个简单的类测试了该过程,没有使用媒体,没有任何问题。

这是我的

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; }
}

我的

Media
班级:

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;
    }
}

我的

Movie
班级:

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

sql-server entity-framework-core asp.net-core-8
2个回答
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;
    }
}

0
投票

我发现您的代码存在一些问题。您可以使用抽象类添加迁移。

在你的媒体课上

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(); <-- try changing this to 
        Id = Guid.NewGuid();
        NegativePoints = negativePoints;
        PositivePoints = positivePoints;
        Comments = comments;
        Title = title;
        //Type = Type; <-- Change this to 
        Type=type //you are assigning the value to itself instead of the parameter value
    }

}

在 Movie 构造函数中,您不再需要分配类型,因为您是在基类 Media 中分配值。

public Movie(int negativePoints, int positivePoints, List<Comment> comments, string title, MediaType type) : base(negativePoints, positivePoints, comments, title, type)
    {
        //Type = MediaType.Movie; <-- not needed assigned in base class (Media)
        Director = null;
        Genre = null;
        Release = null;
        AgeRating = null;
        Synopsis = null;
        Actors = new List<string>();
        Duration = null;
        Language = null;
        ContryOrigin = null;
        ProductionStudio = null;
        Trailer = null;
    }
© www.soinside.com 2019 - 2024. All rights reserved.