【问题标题】:Entity Framework, view and instead of insert trigger. Can't insert a row into the view实体框架,视图而不是插入触发器。无法在视图中插入行
【发布时间】:2014-11-12 21:12:05
【问题描述】:

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

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

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

代码:

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; }
}

谁能帮帮我?

【问题讨论】:

标签: sql-server entity-framework tsql view triggers


【解决方案1】:

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

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 文件。 这是答案的来源: Entity Framework with Instead Of triggers

【讨论】:

    猜你喜欢
    • 2014-11-18
    • 1970-01-01
    • 2017-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-07
    • 2021-05-01
    • 1970-01-01
    相关资源
    最近更新 更多