简体   繁体   English

如何在代码优先迁移中解决Configuration.cs中的错误

[英]How to solve error in Configuration.cs in code first migration

I was setting up code first migration for model changes from Package Manager Console which creates seed method in Configuration.cs . 我正在从Package Manager Console设置代码首次迁移以进行模型更改,这会在Configuration.cs创建seed方法。 i placed my code in Seed method and it shows error at context.Movies.AddorUpdate(----- 我将我的代码放在Seed方法中,它在context.Movies.AddorUpdate(-----)显示错误

it says : 它说 :

The type arguments for method 'System.Data.Entity.Migrations.DbSetMigrationsExtensions.AddOrUpdate(System.Data.Entity.IDbSet, params TEntity[])' cannot be inferred from the usage. 无法从用法推断出方法'System.Data.Entity.Migrations.DbSetMigrationsExtensions.AddOrUpdate(System.Data.Entity.IDbSet,params TEntity [])'的类型参数。 Try specifying the type arguments explicitly. 尝试显式指定类型参数。

protected override void Seed(MvcMovie.Models.MovieDbContext context)
{ 
    context.Movies.AddOrUpdate(
    i => i.Title,
        new Movie
        {
            Title = "When Harry Met Sally",
            ReleaseDate = DateTime.Parse("1989-1-11"),
            Genre = "Romantic Comedy",
            Price = 7.99M
        },

        new Movie
        {
            Title = "Ghostbusters ",
            ReleaseDate = DateTime.Parse("1984-3-13"),
            Genre = "Comedy",
            Price = 8.99M
        },

        new Movie
        {
            Title = "Ghostbusters 2",
            ReleaseDate = DateTime.Parse("1986-2-23"),
            Genre = "Comedy",
            Price = 9.99M
        }
    );
}

Movie.cs Movie.cs

namespace MvcMovie.Models
{
    public class Movie
    {
        public int ID
        {
            get;
            set;
        }
        public string Title 
        {
            get;
            set; 
        }

        [Display(Name="ReleaseDate")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
        public DateTime ReleaseDate 
        {
            get;
            set; 
        }
        public string Genre 
        { 
            get;
            set;
        }
        public decimal Price 
        {
            get;
            set;
        }

    }
   public class MovieDbContext : DbContext
   {
       public DbSet<Movie> Movies { get; set; }
   }
}

The error you are seeing is likely due to you having multiple classes called Movie . 您看到的错误可能是由于您有多个名为Movie类。 I suggest you take a look at your namespaces and using statements to tidy this up. 我建议你看看你的命名空间并using语句来整理它。 But, if you cannot change them, specify the type explicitly using the full namespace (I'm guessing which namespace to use here, you may need the "other" one!): 但是,如果您无法更改它们,请使用完整命名空间显式指定类型(我猜这里使用哪个命名空间,您可能需要“其他”命名空间!):

context.Movies.AddOrUpdate(
    i => i.Title,
    new MvcMovie.Models.Movie
      //^^^^^^^^^^^^^^^^^^^^^ Note the full namespace here
    {
        Title = "When Harry Met Sally",
        ReleaseDate = DateTime.Parse("1989-1-11"),
        Genre = "Romantic Comedy",
        Price = 7.99M
    },
    //Snip rest of code
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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