查看实体框架,而不是插入触发器。无法在视图中插入行 [英] Entity Framework, view and instead of insert trigger. Can't insert a row into the view

查看:76
本文介绍了查看实体框架,而不是插入触发器。无法在视图中插入行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将实体插入映射视图。
我得到的错误是:

I can't insert an entity into the mapped view. The error I get is:


存储,插入或删除语句影响了意外的行数(0)

Store, insert or delete statement affected an unexpected number of rows (0)

我知道如何使用存储过程,但是对我而言,尝试在插入行视图事件中使用触发器而不是触发器的解决方案更有趣。删除,更新或插入t-sql代码时没有出现任何错误,但是我无法使用EF插入行。更新和删除EF中的工作,但不插入。

I know how to use stored procedures, but it is more interesting for me to try a solution with an instead of trigger on insert row view event. I don't get any error when I delete, update or insert in t-sql code, but I can't INSERT a row using EF. Update and delete work in EF, but INSERT doesn't.

代码:

create view TestInsert
as
    select a.table_id, a.name
    from TableA as a

create trigger tr_works_via_tsql_but_not_ef_for_some_reason
on TestInsert
instead of isert
begin
   insert into TableA (table_id, name)
   select table_id, name from inserted;
end



[Table(TestInsert)]
public class TestInsert
{
   [Key]
   public int table_id { get; set; }
   public string name { get; set; }
}

有人可以帮助我吗?

推荐答案

万岁,我已经找到了解决方案!
的主体,而不是触发器必须返回表的ID

Hooray, I have found the solution! The body of instead of trigger must return an id for the table.

create trigger tr_works_via_tsql_but_not_ef_for_some_reason
on TestInsert
instead of isert
begin
   insert into TableA (table_id, name)
   select table_id, name from inserted;

   **select id from TableA where @@ROWCOUNT > 0 and id = scope_identity()**
end

我使用直接映射,我的项目中没有edmx文件。
这是答案的来源:
使用实体框架代替触发条件

I use direct mapping and there is no edmx file in my project. This is the source of the answer: Entity Framework with Instead Of triggers

这篇关于查看实体框架,而不是插入触发器。无法在视图中插入行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆