c# - 将 IEnumerable 保存到数据库中

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

我有模型:它的数据来 self 解析它的某个站点。

public class Race
{
   [Key]
   public int Id;
    public string Cars;
    public string Place;
    public string Pilot;
}

var compare = Doc.DocumentNode
                 .SelectNodes(path)
                 .Select(s => new Race()
                            {
                                Cars =s.SelectNodes("./div[1]//text()").Single().InnerText,
                            }
                        );
using (_context) //  how can I do this?
{
    _context.Race.Add(compare);
    _context.SaveChanges();
}

Argument 1: cannot convert from 'System.Collections.Generic.IEnumerable<Race>' to 'Race'

最佳答案

您应该使用 AddRange 而不是 Add

using (_context)     //  how can I do this?
{
    _context.Race.AddRange(compare);
    _context.SaveChanges();
}

关于c# - 将 IEnumerable 保存到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36038978/

相关文章:

c# - SSAS ADOMD 连接强制关闭

c# - ASP.NET GridView 焦点选定行

.net - 有没有办法扩展 ASP.NET Membership Provider?

asp.net-mvc - 使用Html.DropDownList禁用选择列表

c# - 通过 Visual Studio Team Services 和 Team Foundation Server 的 REST API 概述从 Microsoft vso WorkItem 访问评论

c# - 在跨平台 (Xamarin) 应用程序中使用哪个 IoC 容器?

ASP.NET Identity DbContext 混淆

c# - 将下拉列表的值传递给 Javascript 变量

c# - 如何通过 LINQ 在二维数组中进行搜索?

c# - 在当前鼠标位置缩放/拉伸(stretch)图片框?