【发布时间】:2021-07-12 18:35:09
【问题描述】:
我正在尝试通过实体框架将对象列表添加到 SQL Server 数据库,但添加时出现错误
[HttpPost]
public void Post(List<Row> rows)
{
try
{
using (DbModel dbModel = new DbModel())
{
foreach (var el in rows)
{
dbModel.Provider_Status.Add(el);
}
dbModel.SaveChanges();
}
}
catch { }
}
行类:
public class Row
{
public string FileName { get; set; }
public string FileTitle { get; set; }
public string ProviderID { get; set; }
public string ServiceID { get; set; }
public string PublishDate { get; set; }
public string ExpiryDate { get; set; }
}
数据库模型 DbModel:
public partial class Provider_Status
{
public int Id { get; set; }
public string FileName { get; set; }
public string FileTitle { get; set; }
public string ProviderID { get; set; }
public string ServiceID { get; set; }
public string PublishDate { get; set; }
public string ExpiryDate { get; set; }
}
错误信息:
CS1503 参数 1:无法从“File_Upload.Models.Row”转换为“File_Upload.Models.Provider_Status”
【问题讨论】:
-
错误是什么??你的
DbModel班级是什么样的? -
我更新了包含 DbModel 和错误消息的帖子
-
也许你需要一个从 Row 到 Provider_Status 的类型转换器,因为看起来你正试图将 Row 对象插入到 Provider_Status 集合中。
-
我将参数更改为 List
行并且它有效.. 谢谢大家 -
也看看 AddRange 而不是使用循环
标签: asp.net sql-server asp.net-mvc entity-framework asp.net-web-api