【发布时间】:2012-04-18 16:53:54
【问题描述】:
在一个项目中,我有一个包含以下列的数据库表
我希望能够从该表中删除所有可以传入的具有匹配的 SharingAgencyId 和 ReceivingAgencyId 值的行。
到目前为止我所尝试的:
public static ICollection<SecurityDataShare> UpdateSharedEntites(long conAgency, long recAgency)
{
ICollection<SecurityDataShare> agShares = null;
try
{
using (var context = new ProjSecurityEntities(string.Empty))
{
agShares = (from a in context.SecurityDataShares
.Where(c => c.ReceivingAgencyId == recAgency && c.SharingAgencyId == conAgency)
select a);
}
}
catch (Exception ex)
{
//ToDo
throw;
}
}
我的想法是检索 id 与传入参数匹配的记录,然后使用 foreach 循环遍历 (agShare) 并删除每一行,然后保存我的更改。在当前的实现中,我似乎无法访问任何 Delete 方法。
看看上面的例子,我很感激有关如何使用 dbContext 删除表中包含值 43 和 39 的行的任何建议。
干杯
【问题讨论】:
-
context.SecurityDataShares上是否有任何 Delete 或 Remove 方法? -
SecurityDataShares 上有一个 Remove 方法,但这看起来好像删除了实体表而不是实体本身 示例:context.SecurityDatShares.Remove();
标签: linq entity-framework