【问题标题】:Insert Multiple Rows to Database LINQ向数据库 LINQ 中插入多行
【发布时间】:2017-09-08 21:09:28
【问题描述】:

我有一个 foreach 循环,它遍历 List<NewItem> 类型的列表,创建 NewItem 的新实例,设置其属性,然后 Add()s 将项目插入到上下文中,以在执行 @ 时插入987654324@:

foreach (var newItem in newItems)
{
    NewItem item = new NewItem 
    {
        User = newItem.User,
        Itemno = newItem.Itemno
    };

    db.NewItem.Add(item);

}

try
{
    db.SaveChanges();
}
catch (Exception e)
{
    Console.WriteLine(e);
}

我收到错误:

无法更新 EntitySet 'NewItem',因为它有一个 DefiningQuery 并且元素中不存在任何元素来支持 当前操作。

为什么Add() 方法实际上没有向我的数据库上下文添加任何内容?

【问题讨论】:

    标签: c# entity-framework linq entity-framework-5


    【解决方案1】:

    如果目标数据库表没有主键,则添加一个。

    【讨论】:

      猜你喜欢
      • 2016-02-17
      • 2015-05-14
      • 1970-01-01
      • 2014-02-10
      • 2016-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多