Dinav Ahire
Dinav Ahire

Reputation: 583

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

Upvotes: 1

Views: 1197

Answers (1)

DavidG
DavidG

Reputation: 119206

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

Upvotes: 3

Related Questions