动态数据网站的 LINQ 帮助

发布于 2024-07-29 00:23:58 字数 563 浏览 3 评论 0原文

我有动态数据网站,它使用 SQL SP 执行更新操作。我这里有一个问题,我的删除功能也是更新(设置 IsDeleted=1)操作。 我当前正在使用 LINQ 查询并调用 datacontext.SubmitChanges() 进行删除。 问题是,当我调用 SubmitChanges() 时,更新 LINQ 查询(设置 IsDeleted=1)也会调用仅用于更新操作的更新 SP。 是否有任何选项可以直接向数据库触发 LINQ 查询,而不是调用更新 SP?

Employee ild = (from emp in _dataContext.Employee
                                     where emp.IN_ID == int.Parse(ID)
                                     select emp).FirstOrDefault();
ild.IsDeleted=1;
_dataContext.Submitchanges();

上述代码始终调用配置为更新操作的UpdateSP。

I have Dynamic dataWebsite which uses a SQL SP to do update operations..I have a problem here, my delete functionality is also a update (setting IsDeleted=1) operations. I am currently using LINQ query and calling datacontext.SubmitChanges() for deleting. The problem is, the update LINQ query (that sets IsDeleted=1) when I call SubmitChanges() is also calling the update SP which is meant for only Update Operations. Is there any option to fire my LINQ query directly to DB instead of calling update SP?

Employee ild = (from emp in _dataContext.Employee
                                     where emp.IN_ID == int.Parse(ID)
                                     select emp).FirstOrDefault();
ild.IsDeleted=1;
_dataContext.Submitchanges();

The above code always call UpdateSP that is configured to Update operation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

棒棒糖 2024-08-05 00:23:58

在这种情况下,您可以使用删除存储过程吗?它将像更新过程一样被调用。 删除过程不需要实际执行 DELETE FROM 表查询,而是可以对基础表进行更新,并根据需要设置 IsDeleted 标志:

CREATE PROCEDURE
  @Id int
AS
  UPDATE dbo.Employee 
  SET IsDeleted = 1 
  WHERE Id = @Id

然后,您可以将此函数映射到 LINQ to SQL 中的删除行为,就像您一样执行了 Update 方法。 使用此选项,您的客户端代码将在表上执行简单的删除,而不是处理 IsDeleted 标志:

_dataContext.Employee.Remove(ild);
_dataContext.SubmitChanges();

在您的模型中,我认为您根本不应该公开 IsDeleted。 这是数据库实现细节。 使用软删除时,您应该抽象出物理表并通过视图或表值函数公开功能。

作为软删除选项的替代方案,您可以考虑包含一个模仿事务表的逻辑删除表。 在删除操作中,使用存储过程或触发器将记录从事务表移动到逻辑删除表。 这样,您就可以从数据库中消除 IsDeleted 标志,并消除对所有访问(包括报告)进行过滤的需要。

In this case, could you use a delete stored proc which will get called just like your update proc. The delete proc doesn't need to actually perform a DELETE FROM table query, but instead could do an Update on the underlying table setting the IsDeleted flag as appropriate:

CREATE PROCEDURE
  @Id int
AS
  UPDATE dbo.Employee 
  SET IsDeleted = 1 
  WHERE Id = @Id

You would then map this function to the delete behavior in LINQ to SQL just as you did the Update method. With this option, your client code would do a simple Remove on the table rather than dealing with the IsDeleted flag:

_dataContext.Employee.Remove(ild);
_dataContext.SubmitChanges();

In your model, I would argue that you shouldn't expose IsDeleted at all. That is a database implementation detail. When using soft deletes, you should abstract away your physical table and expose the functionality through views or table value functions.

As an alternative to the soft delete option, you could consider including a Tombstone table mimicing your transactional table. On a delete operation, use a stored proc or trigger to move the record from the transactional table to the tombstone table. With that, you could eliminate the IsDeleted flag from the database and elminate the need to include the filtering on all access (including reporting).

骷髅 2024-08-05 00:23:58

我不确定我是否遵循这里的想法。

通常要删除记录,您会说:

_dataContext.Employee.remove(ild);
_dataContext.Submitchanges();

但似乎您只想更新记录以读取任何设置 IsDeleted = 1 作为已删除记录的员工。 通过运行您当前拥有的代码,将生成一个 UPDATE 语句,因此 UpdateSP 将触发。

是否有原因无法使用 .remove() 方法并物理删除该条目?

I'm not 100% sure that I follow the idea here.

generally to delete the record you would say:

_dataContext.Employee.remove(ild);
_dataContext.Submitchanges();

But it seems like you wanted to just update the record to read any Enployee that has a setting IsDeleted = 1 as a deleted record. By running the code you current have there are are generateing an UPDATE statement and so the UpdateSP will fire.

Is there a reason you can't use the .remove() method and ophysically delete the entry?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文