我知道更新一个模型在EF我必须得到模型然后更改它然后dbContext.SaveChanges()但有时在我的Asp.net mvc项目,我想更新我的表中的字段,我不知道它的ID并且必须使用where子句获取它.但我不希望连接两次到数据库,因为在ADO.net我可以写:
UPDATE MyTable SET Field3 = "NewValue" WHERE Active = 1
Run Code Online (Sandbox Code Playgroud)
现在我想写一个linq到sql for EF就像那样工作.存在任何方式吗?谢谢
嗨我在c#中创建一个winform应用程序.
我使用EF5来处理数据库.
并且为了将数据绑定到我的datagridviews,我从BindingSource创建了一个组件,该组件具有运行此事件的Bind()方法:
private object bindingSourceTbl1_DataSourceBinding(object sender, EventArgs e)
{
using (SampleDbEntities dbo = new SampleDbEntities())
{
return(from x in dbo.Tbl1
where x.Id == (int)comboBoxPerson.SelectedValue
select x).Take(1000).ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
因为我的数据库有很多大数据,我获取部分数据.我用搜索获得匹配记录.为此,我创建了一个SearchPanel组件,用于创建文本框,用于过滤网格中的每个列.
现在我想将一个表达式树发送到我的事件作为参数加入where子句,如下所示:
private object bindingSourceTbl1_DataSourceBinding(object sender, EventArgs e,Expression whereClause)
{
using (SampleDbEntities dbo = new SampleDbEntities())
{
return(from x in dbo.Tbl1
where x.Id == (int)comboBoxPerson.SelectedValue && whereClause
select x).Take(1000).ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
但我的表达式树构建器方法存在于我的组件代码中,我不能访问我的项目中的DbContext,只是我的组件中有fieldNames,我也想为所有表编写一个方法.
这意味着我无法回归
表达式<Func <AnyDbSet,bool >>
我不知道怎么办?
谢谢