简体   繁体   中英

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 . i placed my code in Seed method and it shows error at 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. 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

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 . I suggest you take a look at your namespaces and using statements to tidy this up. 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
);

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