zoukankan      html  css  js  c++  java
  • EF6 对于实体字段类型转换扩展

    转换类:
    public class UtcDbDataReader : DbDataReader
    {
        private readonly DbDataReader source;
    
        public UtcDbDataReader(DbDataReader source)
        {
            this.source = source;
        }
    
        public override DateTime GetDateTime(int ordinal)
        {
            return DateTime.SpecifyKind(source.GetDateTime(ordinal), DateTimeKind.Utc);
        }        
    
        // you need to fill all overrides. Just call the same method on source in all cases
    
        public new void Dispose()
        {
            source.Dispose();
        }
    
        public new IDataReader GetData(int ordinal)
        {
            return source.GetData(ordinal);
        }
    }
    

      



    定义检查类,这个就是在实体和数据库映射时候提供的AOP编程接口

    public class UtcInterceptor : DbCommandInterceptor
    {
        public override void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
        {
            base.ReaderExecuted(command, interceptionContext);
    
            if (interceptionContext?.Result != null && !(interceptionContext.Result is UtcDbDataReader))
            {
                interceptionContext.Result = new UtcDbDataReader(interceptionContext.Result);
            }
        }
    }
    

      将检查类处理为可配置:

    internal class MyDbConfiguration : DbConfiguration
    {
        protected internal MyDbConfiguration ()
        {           
            AddInterceptor(new UtcInterceptor());
        }
    }
    

      最后配置给数据库上下文:

    [DbConfigurationType(typeof(MyDbConfiguration ))]
    internal class MyDbContext : DbContext
    {
        // ...
    }
    

      这个我目前还没测试过,只是提供思路,有兴趣的可以自己实践。

  • 相关阅读:
    Qt的.pro文件
    AI_八数码
    安装 MINGW GCC 4.4.0
    VC中应用Excel
    八数码问题_启发搜索
    【收集】【收藏】【转帖】game development resouce
    QT小记之在VS2005中使用QT
    [转文]VS2008 安装 Boost 1.43.0
    搬家到博客园了
    转 暴雪总裁总结游戏十条经验
  • 原文地址:https://www.cnblogs.com/ck0074451665/p/13321458.html
Copyright © 2011-2022 走看看