简体   繁体   English

从多个表中删除用户记录ASP.Net MVC

[英]Delete user records from more than one table ASP.Net mvc

I need to delete a record from two tables (Asp.netUser table - holds all the registration details & Checking Account Tables - holds all users details). 我需要从两个表中删除一条记录(Asp.netUser表-保存所有注册详细信息&Checking Account表-保存所有用户详细信息)。 The delete action method allows the deletion process only on the checking Accounts table. 删除操作方法仅允许在“检查帐户”表上进行删除过程。 what query / code should i write to delete a record from all the tables? 我应该写什么查询/代码从所有表中删除一条记录?

Delete Action Method 删除动作方法

public ActionResult Delete(int? id)
        {
            if (id == null)

                return HttpNotFound();
                var del = db.checkAccounuts.Find(id);

            if (del == null)
                return HttpNotFound();

            db.checkAccounuts.Remove(del);
            db.SaveChanges();

            return RedirectToAction("ViewAccounts");

        } 

Records can be deleted from this table 可以从该表中删除记录

CREATE TABLE [dbo].[CheckingAccount] (
    [Id]            INT             IDENTITY (1, 1) NOT NULL,
    [AccountNumber] VARCHAR (10)    NOT NULL,
    [FirstName]     NVARCHAR (MAX)  NOT NULL,
    [LastName]      NVARCHAR (MAX)  NOT NULL,
    [Balance]       DECIMAL (18, 2) NOT NULL,
    [AppUserId]     NVARCHAR (MAX)  NOT NULL,
    [User_Id]       NVARCHAR (128)  NULL,
    CONSTRAINT [PK_dbo.CheckingAccounts] PRIMARY KEY CLUSTERED ([Id] ASC),
    CONSTRAINT [FK_dbo.CheckingAccounts_dbo.AspNetUsers_User_Id] FOREIGN KEY ([User_Id]) REFERENCES [dbo].[AspNetUsers] ([Id])
);

But the user details remain in this table! 但是用户详细信息仍保留在该表中!

CREATE TABLE [dbo].[AspNetUsers] (
    [Id]                   NVARCHAR (128) NOT NULL,
    [Email]                NVARCHAR (256) NULL,
    [EmailConfirmed]       BIT            NOT NULL,
    [PasswordHash]         NVARCHAR (MAX) NULL,
    [SecurityStamp]        NVARCHAR (MAX) NULL,
    [PhoneNumber]          NVARCHAR (MAX) NULL,
    [PhoneNumberConfirmed] BIT            NOT NULL,
    [TwoFactorEnabled]     BIT            NOT NULL,
    [LockoutEndDateUtc]    DATETIME       NULL,
    [LockoutEnabled]       BIT            NOT NULL,
    [AccessFailedCount]    INT            NOT NULL,
    [UserName]             NVARCHAR (256) NOT NULL,
    [Pin]                  NVARCHAR (MAX) NULL,
    CONSTRAINT [PK_dbo.AspNetUsers] PRIMARY KEY CLUSTERED ([Id] ASC)
);


GO
CREATE UNIQUE NONCLUSTERED INDEX [UserNameIndex]
    ON [dbo].[AspNetUsers]([UserName] ASC);

Any help would be appreciated! 任何帮助,将不胜感激!

You have to find user details via checking account object and then delete it , modified method will look like 您必须通过检查帐户对象找到用户详细信息,然后将其删除,修改后的方法将如下所示

public ActionResult Delete(int? id)
{
    if (id == null)
        return HttpNotFound();

    var del = db.checkAccounuts.Find(id);

    if (del == null)
        return HttpNotFound();

    aspnetusers user = db.aspnetusers.find(del.User_Id);

    db.checkAccounuts.Remove(del);
    db.aspnetusers.remove(user.ID);
    db.SaveChanges();
    return RedirectToAction("ViewAccounts");
} 

In case you want to perform deletion from backend, you can use SQL table trigger to delete data in user table based on deletion of records from checking table or you can also use a stored procedure to delete records from both tables, just call the stored procedure from your code. 如果要从后端执行删除,则可以使用SQL表触发器根据检查表中记录的删除来删除用户表中的数据,也可以使用存储过程从两个表中删除记录,只需调用存储过程从您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM