如何根据对另一个实体的操作将记录插入到实体中?
我有两个断开连接的模型。第一个是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能够通过在 INSERT 和 DELETE 操作上引入 SQL 表触发器来解决该问题。
首先,每次在事件实体中发生新插入时,工单实体(显示未解决的工单)都会被清空并重新填充为表:
其次,我在工单表上添加一个触发器,以将一行插入到 dbo.Events 表中DELETE 操作的
最后,在 Index 视图中添加一个提交按钮来激活 DELETE 操作:
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:
Second, I am adding a trigger on the Tickets table to insert a line into the dbo.Events table INSTEAD of DELETE action
Finally, in the Index view a submit button is added to activate the DELETE action: