实体框架 4 删除调用不起作用

发布于 2024-10-11 02:18:10 字数 1194 浏览 1 评论 0原文

我尝试使用 EF 从表中删除一条记录,但没有任何反应。代码执行时没有错误,但记录只是保留在数据库中。我在其他地方有几乎相同的代码可以工作。

                using (var DB = new PTNWebConfigurationModel.PTNWebConfigurationEntities())
                {
                    var account = DB.Accounts.Where(a => a.LoginID == loginID).FirstOrDefault();


                        //Load existing session for the Account, otherwise create a new one.
                        if (!LoadExistingSession(ip, DB, account))
                        {
                            CreateNewSession(ip, DB, account);
                        }
                        AccountsSession sessionsToDelete = DB.AccountsSessions.Where(a => a.RemoteIP == ip && a.AccountID == 1).FirstOrDefault();
                        if (sessionsToDelete != null)
                        {
                            DB.DeleteObject(sessionsToDelete);
                            DB.SaveChanges();
                        }
                 }

我也尝试过使用这些选项:

DB.DeleteObject(sessionsToDelete);
DB.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave);
DB.AcceptAllChanges();

我也从不使用块和仅实例化实体对象开始,但这也不起作用。

I'm trying to delete a record from my table using EF, and nothing is happening. The code executes with no errors, but the record just sticks around in the DB. I have nearly identical code elsewhere that's working.

                using (var DB = new PTNWebConfigurationModel.PTNWebConfigurationEntities())
                {
                    var account = DB.Accounts.Where(a => a.LoginID == loginID).FirstOrDefault();


                        //Load existing session for the Account, otherwise create a new one.
                        if (!LoadExistingSession(ip, DB, account))
                        {
                            CreateNewSession(ip, DB, account);
                        }
                        AccountsSession sessionsToDelete = DB.AccountsSessions.Where(a => a.RemoteIP == ip && a.AccountID == 1).FirstOrDefault();
                        if (sessionsToDelete != null)
                        {
                            DB.DeleteObject(sessionsToDelete);
                            DB.SaveChanges();
                        }
                 }

I've also tried it with these options:

DB.DeleteObject(sessionsToDelete);
DB.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave);
DB.AcceptAllChanges();

I also started with no using block and just and instantiated entity object, but that didn't work either.

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

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

发布评论

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

评论(2

酒解孤独 2024-10-18 02:18:10

我喜欢这样的问题。

大多数时候,我看到 ORM 不执行命令(并且没有错误)是由于底层数据表的更改未在该表的生成类中表示。

您可以刷新您的架构/类模型,然后重试。

I love issues like this.

Most of the times that I have seen an ORM not execute a command (and without error) is due to changes to the underlying data tables that aren't represented in the generated classes for that table.

You might refresh your schema / class model and try again.

等你爱我 2024-10-18 02:18:10

继续克里斯的回答我的另一件事过去发现,如果您首先进行 EF 数据库建模,那么您可能已经创建了链接两个关联表的外键,但该键尚未在关系中设置为删除级联。这可能会导致 EF 有点困难。因此,请检查数据库中的所有 FK。希望有帮助。

Carrying on from Chris' answer the other thing I've found in the past is if you are doing EF database first modelling then you may have created the foreign keys linking two associated tables but the key hasn't been set as delete cascade in the relationship. This can cause EF to be a little difficult. So check any FKs in your db. Hope that helps.

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