我需要为azure DB创建db触发器,当用户单击表中的“更改”时,触发器会将此事件写入表,写入已更改的内容,并编写时间表等。
目前我有两个基本问题。
1.这是我的模型
public class UserRoles
{
public int Id { get; set; }
public string UserName { get; set; }
public Boolean Address { get; set; }
public Boolean Email { get; set; }
public Boolean Work { get; set; }
public Boolean Home { get; set; }
}
public class UserRolesDbContext : DbContext
{
public UserRolesDbContext()
: base("DefaultConnection")
{
this.Database.Connection.ConnectionString = My.GetDBConnectionString();
if (!String.IsNullOrEmpty(this.Database.Connection.ConnectionString))
{
this.Database.SqlQuery("here put the SQL")
}
}
public DbSet<UserRoles> UserRolesCollection { get; set; }
}如果我想在部署云服务时创建这个触发器(在sqlQuery中),我应该这样做吗?
2.假设我想将带有DB triger的所有字段从UserRoles表复制到UserRolesCopy,并且当它们像审计一样发生变化时,我应该如何编写SQL代码?
my schema name is :UserDB
MyOrginal table is:UserRoles
I want it to copy to UserRoleCopy我熟悉这个主题,并在MSDN中阅读了下面的页面
http://msdn.microsoft.com/en-us/library/ms189799%28v=sql.120%29.aspx
- Windows数据库语法
在插入、更新或删除表或视图的语句(DML触发器)上触发
CREATE TRIGGER [ schema_name . ]trigger_name
ON { table | view }
[ WITH <dml_trigger_option> [ ,...n ] ]
{ FOR | AFTER | INSTEAD OF }
{ [ INSERT ] [ , ] [ UPDATE ] [ , ] [ DELETE ] }
AS { sql_statement [ ; ] [ ,...n ] [ ; ] > }
<dml_trigger_option> ::=
[ EXECUTE AS Clause ] 发布于 2014-06-23 06:27:21
可以使用以下方法审核表更改:
Create Trigger yourSchema.TriggerName ON YourTable
AFTER INSERT, Update, Delete
AS Begin
-- Log Deleted Rows and Old Value in Updated Rows
Insert Into LogTable(UserName, Columns)
Select CURRENT_USER, Columns
From Deleted
-- Log Inserted Row and New Value in Updated Rows
Insert Into LogTable(UserName, Columns)
Select CURRENT_USER, Columns
From Inserted
Endhttps://stackoverflow.com/questions/24359552
复制相似问题