![](/img/trans.png)
[英]Entity Framework Code First: Configuration.cs seed or custom initializer
[英]How to solve error in Configuration.cs in code first migration
我正在從Package Manager Console
設置代碼首次遷移以進行模型更改,這會在Configuration.cs
創建seed
方法。 我將我的代碼放在Seed
方法中,它在context.Movies.AddorUpdate(-----)顯示錯誤
它說 :
無法從用法推斷出方法'System.Data.Entity.Migrations.DbSetMigrationsExtensions.AddOrUpdate(System.Data.Entity.IDbSet,params TEntity [])'的類型參數。 嘗試顯式指定類型參數。
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; }
}
}
您看到的錯誤可能是由於您有多個名為Movie
類。 我建議你看看你的命名空間並using
語句來整理它。 但是,如果您無法更改它們,請使用完整命名空間顯式指定類型(我猜這里使用哪個命名空間,您可能需要“其他”命名空間!):
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.