c# - 种子方法如何使用foreach循环?

标签 c# asp.net-mvc entity-framework

 protected override void Seed(ContosoUniversity.DAL.SchoolContext context)
  {
      var students = new List<Student>
        {
            new Student { FirstMidName = "Carson",   LastName = "Alexander", 
                EnrollmentDate = DateTime.Parse("2010-09-01") },
            new Student { FirstMidName = "Meredith", LastName = "Alonso",    
                EnrollmentDate = DateTime.Parse("2012-09-01") },
            new Student { FirstMidName = "Arturo",   LastName = "Anand",     
                EnrollmentDate = DateTime.Parse("2013-09-01") },
            new Student { FirstMidName = "Gytis",    LastName = "Barzdukas", 
                EnrollmentDate = DateTime.Parse("2012-09-01") },
            new Student { FirstMidName = "Yan",      LastName = "Li",        
                EnrollmentDate = DateTime.Parse("2012-09-01") },
            new Student { FirstMidName = "Peggy",    LastName = "Justice",   
                EnrollmentDate = DateTime.Parse("2011-09-01") },
            new Student { FirstMidName = "Laura",    LastName = "Norman",    
                EnrollmentDate = DateTime.Parse("2013-09-01") },
            new Student { FirstMidName = "Nino",     LastName = "Olivetto",  
                EnrollmentDate = DateTime.Parse("2005-08-11") }
        };
     students.ForEach(s => context.Students.AddOrUpdate(p => p.LastName, s));
     context.SaveChanges();

我在我的 MVC 项目中使用迁移,但在上面代码的 Configuration.cs 文件中。使用种子方法,但我不明白 Foreach 循环的语法。所以任何人都可以建议我。

最佳答案

List.ForEach 方法类似于“foreach”循环。它使用 lambda 对每个元素执行操作。

http://msdn.microsoft.com/en-us/library/bwabdf9z(v=vs.110).aspx

http://msdn.microsoft.com/en-us/library/bb397687.aspx

关于c# - 种子方法如何使用foreach循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24624847/

相关文章:

c# - asp.net MVC路由错误404

c# - Entity Framework 中的 Linq SelectMany

c# - ASP.net MVC Entity Framework 更新多对多关系

c# - 由 javascript 填充的下拉列表在回发后被重置

c# - 在重新启动 dotnet 错误之前等待文件更改

asp.net-mvc - ASP.NET MVC 如何应用基于角色或基于身份验证的 View 呈现?

c# - 在 ASP.NET MVC 中正确完成购物车 session 状态

c# - 您要在 C# 中获取 Windows 安装 SID 吗?

c# - 动态控件 - C# - CheckBoxList Parent - Children

c# - EntityFramework 未使用默认值更新列