为了账号安全,请及时绑定邮箱和手机立即绑定

使用实体框架更新表中的单行

使用实体框架更新表中的单行

C#
互换的青春 2022-10-23 15:31:46
我是在网络表单中使用 EF6 的新手。我正在尝试更新没有 ID 的表中唯一可用的行,它只是应用程序的参数配置表。我在表单视图上有这个更新方法。当我尝试加载该项目时,它给了我错误。我认为我在这里做错了,但不确定我需要做什么。我对linq一无所知。错误 11 无法将类型“System.Linq.IQueryable”隐式转换为“InventarioCiclico.xInventarioConfigs”。存在显式转换(您是否缺少演员表?) C:\Users\A0H79224\Documents\Visual Studio 2013\Projects\InventarioCiclico\InventarioCiclico\Account\Admin\ConfigurarInventario.aspx.cs 73 20 InventarioCiclico // The id parameter name should match the DataKeyNames value set on the control    public void fvInventarioConfigs_UpdateItem(xInventarioConfigs configs)    {        InventarioCiclico.xInventarioConfigs item = configs;        InventarioCiclicoContext context = new InventarioCiclicoContext();        // Load the item here, e.g. item = MyDataLayer.Find(id);        item = (from c in context.xInventarioConfigs select c).Take(1);        if (item == null)        {            // The item wasn't found            ModelState.AddModelError("", String.Format("Item with id was not found"));            return;        }        TryUpdateModel(item);        if (ModelState.IsValid)        {            context.SaveChanges();            // Save changes here, e.g. MyDataLayer.SaveChanges();        }    }
查看完整描述

2 回答

?
白猪掌柜的

TA贡献1893条经验 获得超10个赞

即使您使用Take(1)仅选择一条记录, Take也会返回IQueryable。您可以使用类似这样的快速修复:

item = (from c in context.xInventarioConfigs select c).Take(1).FirstOrDefault();

或者即使没有 Take as FirstOrDefault 也会选择一行。


查看完整回答
反对 回复 2022-10-23
?
侃侃尔雅

TA贡献1801条经验 获得超16个赞

Take 返回一个 IQueryable,它可能只包含一个项目,但仍然是一个集合。如果您使用 Take(1) 仅选择一条记录,您不妨直接选择 First(如果结果集中没有记录,请注意此处)或 FirstOrDefault

item = (from c in context.xInventarioConfigs select c).FirstOrDefault();


查看完整回答
反对 回复 2022-10-23
  • 2 回答
  • 0 关注
  • 90 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号