英文:
Deleting records from look up table not working in .NET Core 6
问题
我最近升级了我的项目到.NET Core 6,现在无法从我的查找表中删除记录。我有一个名为Risk
的对象,它包含一个Users
的集合。从风险对象中删除用户不再起作用。有没有什么想法我做错了?
我的查找表名为RiskItemUser
,它有两列,RiskItemId
和UserId
。
代码:
var postSavedRisk = _riskService.Queryable().Include(c => c.AssignedTo).Where(w => w.Id == riskitem.Id).FirstOrDefault();
List<User> usersToRemove = postSavedRisk.AssignedTo.Where(c => userNamesToRemove.Contains(c.UserName)).ToList();
using (var db = new ApplicationDbContext())
{
var postSavedAssginedTo = db.RiskItemUser
.Where(w => w.RiskItemId == riskitem.Id)
.ToList();
foreach (var userToRemove in usersToRemove)
{
foreach (var riskAssignedTo in postSavedAssginedTo)
{
if (userToRemove.Id == riskAssignedTo.UserId)
db.RiskItemUser.Remove(riskAssignedTo);
await db.SaveChangesAsync().ConfigureAwait(false);
}
}
}
英文:
I recently upgraded my project to .NET Core 6 and now removing records from my look up tables is not working. I have a Risk
object that has a collection of Users
. Removing users from the risk object no longer works. Any ideas what I'm doing wrong?
My lookup table is called RiskItemUser
, and it has two columns, RiskItemId
and UserId
.
Code:
var postSavedRisk = _riskService.Queryable().Include(c => c.AssignedTo).Where(w => w.Id == riskitem.Id).FirstOrDefault();
List<User> usersToRemove = postSavedRisk.AssignedTo.Where(c => userNamesToRemove.Contains(c.UserName)).ToList();
using (var db = new ApplicationDbContext())
{
var postSavedAssginedTo = db.RiskItemUser
.Where(w => w.RiskItemId == riskitem.Id)
.ToList();
foreach (var userToRemove in usersToRemove)
{
foreach (var riskAssignedTo in postSavedAssginedTo)
{
if(userToRemove.Id == riskAssignedTo.UserId)
db.RiskItemUser.Remove(riskAssignedTo);
await db.SaveChangesAsync().ConfigureAwait(false);
}
}
}
答案1
得分: 2
以下是翻译好的部分:
The code, as you show it, looks like it should work, although some parts are hidden. Therefore, it's hard to tell how to make it work. But there's room for simplification, which should result in working code.
你展示的代码看起来应该可以工作,尽管有些部分是隐藏的。因此,很难确定如何使它工作。但有简化的空间,这应该可以产生有效的代码。
You want to remove users whose names are specified by userNamesToRemove
from a risk that's specified by riskitem.Id
. Assuming that there's a navigation property RiskItemUser.User
, removing these data could be done by essentially one line of code:
你想从由riskitem.Id
指定的风险中删除由userNamesToRemove
指定的用户名的用户。假设有一个导航属性RiskItemUser.User
,删除这些数据可以通过基本上一行代码来完成:
db.RiskItemUser.RemoveRange(
db.RiskItemUser.Where(ru => ru.RiskItemId == riskitem.Id
&& userNamesToRemove.Contains(ru.User.Name)));
await db.SaveChangesAsync().ConfigureAwait(false);
你标记了EFC 6,但截止到EFC 7.0,已经支持批量删除(和更新)函数,允许单语句删除多个数据库记录:
db.RiskItemUser
.Where(db.RiskItemUser.Where(ru => ru.RiskItemId == riskitem.Id
&& userNamesToRemove.Contains(ru.User.Name)))
.ExecuteDelete();
这将执行一条删除语句,而以前的方法将对每一行执行一条语句。
请注意,这种批量方法类似于执行原始SQL。它与EF的更改跟踪器没有通信,EF不能协调语句的正确顺序。我认为一般建议是不要将这些批量方法与常规的SaveChanges
调用混合使用。
英文:
The code, as you show it, looks like it should work, although some parts are hidden. Therefore, it's hard to tell how to make it work. But there's room for simplification, which should result in working code.
You want to remove users whose names are specified by userNamesToRemove
from a risk that's specified by riskitem.Id
. Assuming that there's a navigation property RiskItemUser.User
, removing these data could be done by essentially one line of code:
db.RiskItemUser.RemoveRange(
db.RiskItemUser.Where(ru => ru.RiskItemId == riskitem.Id
&& userNamesToRemove.Contains(ru.User.Name)));
await db.SaveChangesAsync().ConfigureAwait(false);
You tagged EFC 6, but as of EFC 7.0, there's support for bulk delete (and update) functions, allowing for single-statement deletion of multiple database records:
db.RiskItemUser
.Where(db.RiskItemUser.Where(ru => ru.RiskItemId == riskitem.Id
&& userNamesToRemove.Contains(ru.User.Name)))
.ExecuteDelete();
This will execute one delete statement, whereas the previous method will execute one statement per row.
Note that this bulk method is like executing raw SQL. There's no communication with EF's change tracker and EF can't coordinate the correct order of statements. I think the general advice should be to not mix these bulk methods with regular SaveChanges
calls.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论