简体   繁体   English

如何使用C#以数据库优先方式拥有审核列?

[英]How can I have audit columns in a database-first approach using c#?

I need to add audit columns (created by, created on, modified by and modified on) for all the tables in my MVC project (web-application). 我需要为我的MVC项目(网络应用程序)中的所有表添加审核列(创建者,创建者,修改者和修改者)。 I thought of having a base class with these 4 columns and inheriting all the other classes. 我想到了一个包含这4列的基类,并继承所有其他类。 But I am using Database-First approach, hence EF has generated all the classes. 但是我使用的是数据库优先方法,因此EF生成了所有类。 I am not sure how can I add the audit columns and the base class inheritance. 我不确定如何添加审核列和基类继承。

I assume that it is not possible to perform your desired operation via EF Database first. 我认为无法首先通过EF数据库执行所需的操作。 But in order to add specific fields to all tables of a database you can use the following code for each field in Sql Server and then update your EF model. 但是,为了将特定字段添加到数据库的所有表中,可以对Sql Server中的每个字段使用以下代码,然后更新EF模型。

DECLARE @TableName VARCHAR(100)
DECLARE @TableSchema VARCHAR(100)
DECLARE @COLUMN_NAME VARCHAR(50)
SET @COLUMN_NAME='CreatedOn' 
DECLARE @COLUMN_DATATYPE VARCHAR(50)
SET @COLUMN_DATATYPE='DateTime'

DECLARE CUR CURSOR FOR
  SELECT TABLE_SCHEMA,
         TABLE_NAME
  FROM   INFORMATION_SCHEMA.TABLES
  WHERE  TABLE_TYPE = 'BASE TABLE'
OPEN CUR
FETCH NEXT FROM CUR INTO @TableSchema,@TableName
WHILE @@FETCH_STATUS = 0
  BEGIN

  DECLARE @SQL NVARCHAR(MAX)
  SET @SQL=NULL
  IF NOT EXISTS ( SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS 
       WHERE TABLE_NAME=@TableName AND COLUMN_NAME = @COLUMN_NAME  and Table_Schema=@TableSchema)
  BEGIN
  SET @SQL='ALTER TABLE '+ @TableSchema+'.'+ @TableName +' ADD '+ @COLUMN_NAME + ' '+ @COLUMN_DATATYPE
    PRINT @SQL
    EXEC(@SQL)
  END

  IF EXISTS ( SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS 
              WHERE TABLE_NAME=@TableName AND COLUMN_NAME=@COLUMN_NAME and Table_Schema=@TableSchema)
  BEGIN
    PRINT 'Column Already exists in Table'
  END
      FETCH NEXT FROM CUR INTO @TableSchema,@TableName
  END
CLOSE CUR
DEALLOCATE CUR

Database first means you have to add all the fields to the database manually and AFAIK there is no inheritance. 首先使用数据库意味着您必须将所有字段手动添加到数据库,并且AFAIK没有继承。 You could write a SQL script adding those columns. 您可以编写一个SQL脚本来添加这些列。 As Mahyar said. 正如Mahyar所说。 But you would still lack the inheritance. 但是您仍然缺少继承。

Or you switch to code first. 或者您先切换到代码。 Then you can define an interface and handle auditing in code. 然后,您可以定义一个接口并用代码处理审核。 As shown here . 如图所示这里 I, for example, use this interface: 例如,我使用以下接口:

    /// <summary>
    /// Adds auditing properties to an entity.
    /// </summary>
    public interface IAuditedEntity
    {
        /// <summary>
        /// Date and time of the entity's creation. Usually in UTC.
        /// </summary>
        /// <remarks>Best set via a <see cref="ITimeProvider"/>.</remarks>
        DateTime DateCreated { get; set; }

        /// <summary>
        /// Identification who created this instance.
        /// </summary>
        /// <remarks>Best set via a <see cref="ICurrentUserIdProvider"/>.</remarks>
        string CreatedBy { get; set; }

        /// <summary>
        /// Date and time of last modification. Usually in UTC.
        /// </summary>
        /// <remarks>Best set via a <see cref="ITimeProvider"/>.</remarks>
        DateTime? DateModified { get; set; }

        /// <summary>
        /// Last one modifiying this instance.
        /// </summary>
        /// <remarks>Best set via a <see cref="ICurrentUserIdProvider"/>.</remarks>
       string ModifiedBy { get; set; }
}

The user provider interface is defined like this: 用户提供者界面的定义如下:

    /// <summary>
    /// Interface for providing the current user's id.
    /// </summary>
    public interface ICurrentUserIdProvider
    {
        /// <summary>
        /// Get the id of the curent user.
        /// </summary>
        /// <returns></returns>
        string GetCurrentUserId();
    }

For testing purposes you now can use an ambient context/service delivering the current user or any user you need to test your logic. 为了进行测试,您现在可以使用环境上下文/服务来交付当前用户或需要测试逻辑的任何用户。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 C# 中的数据库优先方法生成具有所有 CRUD 操作的存储库和模型/实体类 - how to generate the Repository and model/entity classes with all CRUD operations using database-first apporach in C# 使用ViewModel数据库优先方法将表保存在数据库中 - Saving Table in database using a ViewModel database-first approach EDMX使用EF中的数据库优先方法不存在任何关系 - EDMX has no relations present using the database-first approach in EF 如何以数据库优先的方法仅更新一个 model? - How to update only one model in a database-first approach? 数据库优先方法的唯一约束 - Unique constraint in database-first approach EF迁移数据库优先方法? - EF Migrations for Database-first approach? 如何使用 EF Database-First 方法对 Scaffold-DBContext 忽略影子属性并生成单独的可跟踪 class - How to Ignore Shadow Properties using EF Database-First approach to Scaffold-DBContext and Generate Separate Trackable class 在实体框架中刷新使用数据库优先方法模型创建的实体中的数据 - Refresh the data in entity created using database-first approach model in Entity Framework 具有数据库优先方法的 Entity Framework Core 6.0 - 使用包含时选择中的未定义列 - Entity Framework Core 6.0 with database-first approach - unneded column in select when using include 在Entity Framework数据库优先方法中,如何从存储过程返回多个结果集? - In Entity Framework database-first approach how to return multiple result sets from a stored procedure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM