c# - 无法在 EF asp.net 核心中使用一个插入查询添加多行
问题描述
这是我知道的重复问题,很抱歉,但没有任何效果。我需要使用 foreach 语句将许多行添加到一个表中。
这是我的代码:
foreach (var ques in questions)
{
await _db.Question.AddAsync(ques);
await _db.SaveChangesAsync();
}
为每次插入添加新对象
foreach (var ques in questions)
{
Question temp = new Question();
temp.QuestionText = ques.QuestionText;
temp.TypeId = ques.TypeId;
await _db.Question.AddAsync(temp);
await _db.SaveChangesAsync();
}
添加范围
List<Question> result = new List<Question>();
foreach (var ques in questions)
{
Question temp = new Question();
temp.QuestionText = ques.QuestionText;
temp.TypeId = ques.TypeId;
result.Add(temp);
}
_db.Question.AddRange(result);
_db.SaveChanges();
我也尝试不使用 Async 并尝试手动添加 id,但没有任何效果。ID 肯定是 AutoIncremental。
编辑:问题类:
public partial class Question
{
public int IdQuestions { get; set; }
public string QuestionText { get; set; }
public int? TypeId { get; set; }
public int? QuestionairId { get; set; }
public virtual Questionnaire Questionair { get; set; }
public virtual Questiontype Type { get; set; }
}
json:
[
{
"QuestionText": "How you doin'??",
"TypeId": 1,
"QuestionairId": 1
},
{
"QuestionText": "Whaaaaaaaat??",
"TypeId": 1,
"QuestionairId": 1
}
]
解决方案
推荐阅读
- shuffle - 随机改组 Torch 张量
- api - 从 NVP 移植到 REST API
- python - 将excel文件中的所有excel表导入pandas
- html - 如何将项目符号添加到角度材料列表?
- linux - Git - 在 Linux 上使用 Linux 风格的行尾,在 Windows 上使用 Windows 风格的行尾
- objective-c - 无法构建 Objective-C 模块“Alamofire”Xcode 10.3
- java - 具有通用约束的 JSON Jackson
- ssl - 修复我的 Apache 配置以使用letsencrypt
- python - TypeError:一元操作数类型错误-:'tuple'
- php - 即使文件是图像,laravel 图像验证也始终有效