如何根据对另一个实体的操作将记录插入到实体中?

发布于 2025-01-14 01:36:34 字数 1344 浏览 0 评论 0原文

我有两个断开连接的模型。第一个是 Event,其简化形式如下:

public partial class Event
    {
        public int EventID { get; set; }
        public string Subject { get; set; }
       ...
        public string Status { get; set; }

    }

第二个是 Tickets 模型,源自 Event SQL 表。票证模型仅显示未结票证:

with ds as (select distinct subject from events 
except 
select distinct subject from events where status like 'Closed%')

    select isnull(cast(ROW_NUMBER() OVER(ORDER BY [subject] ASC)  as int),0) as [TicketId], subject as Ticket from ds

票证模型如下:

public partial class Tickets
{
    public int TicketId { get; set; }
    public string Ticket { get; set; }
}

现在,我想单击票证视图上的 ActionLink 将新事件插入到事件 SQL 数据库中。单击以下内容:

@Html.ActionLink("Resolve", "Create", new { id=item.TicketId })

将在 Tickets 控制器中启动以下操作:

[HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Create([Bind(Include = "Subject")] Event events)
    {
        if (ModelState.IsValid)
        {
            db.Events.Add(events);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        return View(events);
    }

问题在于,由于 Tickets 是 SQL 视图,因此它不接受任何 CREATE/UPDATE/DELETE 操作。

有办法解决吗?谢谢

I have two disconnected models. The first one is Event, which is in simplified form below:

public partial class Event
    {
        public int EventID { get; set; }
        public string Subject { get; set; }
       ...
        public string Status { get; set; }

    }

The second is the Tickets model, derived from the Event SQL table. The Tickets model shows open tickets only:

with ds as (select distinct subject from events 
except 
select distinct subject from events where status like 'Closed%')

    select isnull(cast(ROW_NUMBER() OVER(ORDER BY [subject] ASC)  as int),0) as [TicketId], subject as Ticket from ds

The Tickets model is below:

public partial class Tickets
{
    public int TicketId { get; set; }
    public string Ticket { get; set; }
}

Now, I want on clicking on the ActionLink on the Tickets view to insert a new event into the Event SQL database. Something like clicking on the:

@Html.ActionLink("Resolve", "Create", new { id=item.TicketId })

Would fire up the following action in the Tickets controller:

[HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Create([Bind(Include = "Subject")] Event events)
    {
        if (ModelState.IsValid)
        {
            db.Events.Add(events);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        return View(events);
    }

The issue is that because the Tickets is a SQL view, it does not accept any CREATE/UPDATE/DELETE actions.

Is there a way around it? Thanks

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

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

发布评论

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

评论(1

电影里的梦 2025-01-21 01:36:34

我能够通过在 INSERT 和 DELETE 操作上引入 SQL 表触发器来解决该问题。

首先,每次在事件实体中发生新插入时,工单实体(显示未解决的工单)都会被清空并重新填充为表:

    CREATE TRIGGER [dbo].[UpdateEvent] 
       ON   [dbo].[Events]
       AFTER INSERT
    AS 
    
    BEGIN
        ...
    
        IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Tickets' AND TABLE_SCHEMA = 'dbo')
    
       CREATE TABLE [dbo].[Tickets](
        [TicketId] [int] NOT NULL,
        [subject] [nvarchar](300) NOT NULL,
     CONSTRAINT [PK_OpenTickets] PRIMARY KEY CLUSTERED 
    (
        [TicketId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
    ) ON [PRIMARY];
    
...
    
    
    with ds as (select distinct subject from events 
    except 
    select distinct subject from events where status like 'Closed%')
    insert into dbo.Tickets SELECT isnull(cast(ROW_NUMBER() OVER(ORDER BY [subject] ASC)  as int),0) as [TicketId], subject  from ds;
    
    
    delete from dbo.Tickets where subject like 'test%';
    
...
    
    END

其次,我在工单表上添加一个触发器,以将一行插入到 dbo.Events 表中DELETE 操作的

CREATE TRIGGER [dbo].[ResolveTickets] ON [dbo].[Tickets] 
INSTEAD OF delete 
AS BEGIN 
declare @ticket nvarchar(max)= (select [subject] from deleted ins) 

INSERT INTO dbo.Events ( Subject, Description, [Start], [End], Status, Hours) 
SELECT @ticket,'Resolved and Closed',cast(getdate() as date), dateadd(dd,1, cast(getdate() as date)),'Closed',0.00 

END

最后,在 Index 视图中添加一个提交按钮来激活 DELETE 操作:

 @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.subject)
            </td>
            <td>
                <div>


                    @using (Html.BeginForm("Delete", "Tickets", new { id = item.TicketId }, FormMethod.Post, null))
                    {
                        @Html.AntiForgeryToken()

                    <div class="form-actions no-color">

                        **<button type="submit" name="submitButton" value="Delete" style="border:none">Close the Ticket</button>**

                        <style>
                            button {
                                background-color: rgba(255,255,255,0);
                                color: cornflowerblue;
                            }
                        </style>


                    </div>
                    }
                </div>


               
            </td>
        </tr>
    }

I was able to resolve the issue by introducing SQL table triggers on INSERT and DELETE actions.

First, the Ticket entity (shows unresolved tickets) is emptied and repopulated as a table every time a new insert happens into the Event Entity:

    CREATE TRIGGER [dbo].[UpdateEvent] 
       ON   [dbo].[Events]
       AFTER INSERT
    AS 
    
    BEGIN
        ...
    
        IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Tickets' AND TABLE_SCHEMA = 'dbo')
    
       CREATE TABLE [dbo].[Tickets](
        [TicketId] [int] NOT NULL,
        [subject] [nvarchar](300) NOT NULL,
     CONSTRAINT [PK_OpenTickets] PRIMARY KEY CLUSTERED 
    (
        [TicketId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
    ) ON [PRIMARY];
    
...
    
    
    with ds as (select distinct subject from events 
    except 
    select distinct subject from events where status like 'Closed%')
    insert into dbo.Tickets SELECT isnull(cast(ROW_NUMBER() OVER(ORDER BY [subject] ASC)  as int),0) as [TicketId], subject  from ds;
    
    
    delete from dbo.Tickets where subject like 'test%';
    
...
    
    END

Second, I am adding a trigger on the Tickets table to insert a line into the dbo.Events table INSTEAD of DELETE action

CREATE TRIGGER [dbo].[ResolveTickets] ON [dbo].[Tickets] 
INSTEAD OF delete 
AS BEGIN 
declare @ticket nvarchar(max)= (select [subject] from deleted ins) 

INSERT INTO dbo.Events ( Subject, Description, [Start], [End], Status, Hours) 
SELECT @ticket,'Resolved and Closed',cast(getdate() as date), dateadd(dd,1, cast(getdate() as date)),'Closed',0.00 

END

Finally, in the Index view a submit button is added to activate the DELETE action:

 @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.subject)
            </td>
            <td>
                <div>


                    @using (Html.BeginForm("Delete", "Tickets", new { id = item.TicketId }, FormMethod.Post, null))
                    {
                        @Html.AntiForgeryToken()

                    <div class="form-actions no-color">

                        **<button type="submit" name="submitButton" value="Delete" style="border:none">Close the Ticket</button>**

                        <style>
                            button {
                                background-color: rgba(255,255,255,0);
                                color: cornflowerblue;
                            }
                        </style>


                    </div>
                    }
                </div>


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