简体   繁体   中英

Value cannot be null. (Parameter 'o') EF Core On Update-Database

I am using Entity Framework Core - and on updating the database after migration goes well without any problem, the output of the next command in the Package Manager Console or command prompt.

update-database

Output

Finding design-time services referenced by assembly 'E-Tickets'...
No referenced design-time services were found.
Finding IDesignTimeServices implementations in assembly 'E-Tickets'...
No design-time services were found.
System.ArgumentNullException: Value cannot be null. (Parameter 'o')
   at Microsoft.EntityFrameworkCore.Utilities.Check.NotNull[T](T value, String parameterName)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationCommandListBuilder.Append(String o)
   at Microsoft.EntityFrameworkCore.Migrations.SqlServerMigrationsSqlGenerator.Generate(AlterDatabaseOperation operation, IModel model, MigrationCommandListBuilder builder)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.<>c.<.cctor>b__83_5(MigrationsSqlGenerator g, MigrationOperation o, IModel m, MigrationCommandListBuilder b)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(MigrationOperation operation, IModel model, MigrationCommandListBuilder builder)
   at Microsoft.EntityFrameworkCore.Migrations.SqlServerMigrationsSqlGenerator.Generate(MigrationOperation operation, IModel model, MigrationCommandListBuilder builder)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(IReadOnlyList`1 operations, IModel model, MigrationsSqlGenerationOptions options)
   at Microsoft.EntityFrameworkCore.Migrations.SqlServerMigrationsSqlGenerator.Generate(IReadOnlyList`1 operations, IModel model, MigrationsSqlGenerationOptions options)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.GenerateUpSql(Migration migration, MigrationsSqlGenerationOptions options)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.<>c__DisplayClass16_2.<GetMigrationCommandLists>b__2()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Value cannot be null. (Parameter 'o')

The most important line is

Value cannot be null. (Parameter 'o')

I couldn't recognize what is 'o'.

I will provide the Startup, the Context and the tables to see all aspects of the problem. I am using the code-first approach.

ConfigureServices

public void ConfigureServices(IServiceCollection services)
{
    var connectionString = Configuration.GetConnectionString("defaultConnection");

    services.AddDbContext<eTicketDbContext>(options =>
                                                options.UseSqlServer(connectionString));

    services.AddControllersWithViews();
    services.AddRazorPages();
}

DBContext

public eTicketDbContext(DbContextOptions<eTicketDbContext> options): base(options)
{
}

public virtual DbSet<Actor> Actors { get; set; }
public virtual DbSet<Category> Categories { get; set; }
public virtual DbSet<Cinema> Cinemas { get; set; }
public virtual DbSet<Movie> Movies { get; set; }
public virtual DbSet<Producer> Producers { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
} 

Actor table

public class Actor
{
    [Key]
    public int Id { get; set; }

    [Display(Name = "Profile Picture")]
    [FileExtensions(Extensions = "JPEG,JPG,PNG", ErrorMessage = "Error Wrong Extension")]
    public string ProfilePictureURL { get; set; }

    [Display(Name = "Full Name")]
    [Required(ErrorMessage = "Full Name is required")]
    [StringLength(50, MinimumLength = 3, ErrorMessage = "Full Name must be between 3 and 50 chars")]
    public string FullName { get; set; }

    [Display(Name = "Biography")]
    [Required(ErrorMessage = "Biography is required")]
    [StringLength(400, MinimumLength = 25, ErrorMessage = "Full Name must be between 3 and 50 chars")]
    public string Bio { get; set; }

    public virtual ICollection<Movie> Movies { get; set; }
}

Category table

public class Category
{
    [Key]
    public int CategoryId { get; set; }
    public string Categ { get; set; }

    public virtual ICollection<Movie> Movies { get; set; }
}

Cinema table

public class Cinema
{ 
    [Key]
    public int Id { get; set; }

    [Display(Name = "Cinema Logo")]
    [FileExtensions(Extensions = "JPEG,JPG,PNG", ErrorMessage = "Error Wrong Extension")]
    public string Logo { get; set; }

    [Display(Name = "Cinema Name")]
    [Required(ErrorMessage = "Cinema name is required")]
    public string Name { get; set; }

    [Display(Name = "Description")]
    [Required(ErrorMessage = "Cinema description is required")]
    public string Description { get; set; }

    public virtual ICollection<Movie> Movies { get; set; }
}

Movie table

public class Movie
{
    [Key]
    public int Id { get; set; }

    [Display(Name = "Name")]
    [Required(ErrorMessage = "Name is required")]
    [StringLength(50, MinimumLength = 3, ErrorMessage = "Full Name must be between 3 and 50 chars")]
    public string Name { get; set; }

    [Display(Name = "Description")]
    [Required(ErrorMessage = "Description is required")]
    [StringLength(400, MinimumLength = 25, ErrorMessage = "Full Name must be between 25 and 400 chars")]
    public string Description { get; set; }

    [Display(Name = "Price")]
    [Required(ErrorMessage = "Price is required")]
    [Range(.1, 100)]
    public double Price { get; set; }

    [Required]
    [Display(Name = "Picture")]
    [FileExtensions(Extensions = "JPEG,JPG,PNG", ErrorMessage = "Error Wrong Extension")]
    public string ImageURL { get; set; }

    [Display(Name = "Start Date")]
    [Required(ErrorMessage = "Start Date is required")]
    [BindProperty, DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime StartDate { get; set; }

    [Display(Name = "End Date")]
    [Required(ErrorMessage = "End Date is required")]
    public DateTime EndDate { get; set; }

    [Required]
    [Display(Name = "Cinema")]
    public int CinemaId { get; set; }

    [Required]
    [Display(Name = "Producer")]
    public int ProducerId { get; set; }

    public virtual Cinema Cinema { get; set; }
    public virtual Producer Producer { get; set; }

    public virtual ICollection<Actor> Actors { get; set; }
    public virtual ICollection<Category> Categories { get; set; }
}

Producer table:

public class Producer
{
    [Key]
    public int Id { get; set; }

    [Display(Name = "Profile Picture")]
    [FileExtensions(Extensions = "JPEG,JPG,PNG", ErrorMessage = "Error Wrong Extension")]
    public string ProfilePictureURL { get; set; }

    [Display(Name = "Full Name")]
    [Required(ErrorMessage = "Full Name is required")]
    [StringLength(50, MinimumLength = 3, ErrorMessage = "Full Name must be between 3 and 50 chars")]
    public string FullName { get; set; }

    [Display(Name = "Biography")]
    [Required(ErrorMessage = "Biography is required")]
    public string Bio { get; set; }

    public virtual ICollection<Movie> Movies { get; set; }
}

I am sorry that the question got so long, but I am trying to illustrate the whole picture

只需更改 DbContext 名称即可解决问题,我不知道为什么,但它正在工作。

我遇到了同样的问题,我通过升级项目中的 EntityFramework 解决了。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM