osharp3 整合 dbcontextscope 后,,dbcontextscope 对dbcontext管理的很好,做到,用到时创建,不用时销毁,下面看一个 trace

aspx.page: End PreInit
aspx.page: Begin Init
aspx.page: End Init
aspx.page: Begin InitComplete
aspx.page: End InitComplete
aspx.page: Begin PreLoad
aspx.page: End PreLoad
aspx.page: Begin Load
trace OnLoad: SetControlInPage begin 根据权限标记,控制页面控件的显示和隐藏
trace OnLoad: RegBtnClick begin 根据权限标记,注册按钮事件
: DefaultDbContext ctor object name: System.String
caller name: CanDoo.Data.Entity.Interceptors.EFIntercepterLogging
member name: ReaderExecuted
source line number: 94
time: 20160705 071119 8589791
message: ReaderExecuted执行时间:0 毫秒 \r\n -->ReaderExecuted.Command:\r\nSELECT
[Extent1].[Id] AS [Id],
[Extent1].[CreatedTime] AS [CreatedTime],
[Extent1].[Code] AS [Code],
[Extent1].[SortIndex] AS [SortIndex],
[Extent1].[Remark] AS [Remark],
[Extent1].[ParentId] AS [ParentId],
[Extent1].[Name] AS [Name]
FROM [dbo].[sys_Department] AS [Extent1] : DefaultDbContext Dispose
aspx.page: End Load
aspx.page: Begin LoadComplete
aspx.page: End LoadComplete
aspx.page: Begin PreRender
aspx.page: End PreRender
aspx.page: Begin PreRenderComplete
aspx.page: End PreRenderComplete
aspx.page: Begin SaveState
aspx.page: End SaveState
aspx.page: Begin SaveStateComplete
aspx.page: End SaveStateComplete
aspx.page: Begin Render
aspx.page: End Render

但dbcontext默认是由dbcontextscope创建的,所以,我们的ioc就不能对dbcontext建行注入

不过它留了个接口 IDbContextFactory

    public interface IDbContextFactory
{
TDbContext CreateDbContext<TDbContext>() where TDbContext : DbContext;
DbContext CreateDbContext(Type requestedType);
}

为了在dbcontext注入属性,,我通过这个接口,

将dbcontext的创建收回到ioc,实现这个接口

    public class CandooDbContextFactory : IDbContextFactory
{
private IServiceProvider _provider;
public CandooDbContextFactory(IServiceProvider provider)
{
_provider = provider;
}
public TDbContext CreateDbContext<TDbContext>() where TDbContext : DbContext
{
return (TDbContext)_provider.GetService(typeof(TDbContext));
} public DbContext CreateDbContext(Type requestedType)
{
return (DbContext)_provider.GetService(requestedType);
}
}

修改Startup注入配置

            services.AddTransient<DefaultDbContext>();
services.AddTransient<LoggingDbContext>();
services.AddScoped<IDbContextFactory, CandooDbContextFactory>();

相关文章:

osharp3使用经验:整合DbContextScope 文章 1 http://www.cnblogs.com/shiningrise/p/oshap3_DbContextScope.html

https://github.com/mehdime/DbContextScope

https://github.com/i66soft/osharp

osharp3 整合 dbcontextscope 文章2 将dbcontext的创建收回到ioc管理的更多相关文章

  1. osharp3使用经验:整合DbContextScope 文章 1

    osharp3的事务处理是跳过savechangeing方法来控制的,没有DbContextScope专业 DbContextScope管理dbcontext的优劣本文不讨论 整合过程: 1.在.Da ...

  2. Javascript - ExtJs - 整合百度文章编辑器

    ExtJs - 整合百度文章编辑器(ExtJs UEditor) 第一步:去官网下载最新版本的UEditor,UEditor下载. 第二步:在编辑器根目录创建一个Extjs-Editor.js,录入以 ...

  3. 解决DbContext对象创建问题

    解决DbContext对象创建问题 方法一: 使用CallContext public class BaseController : Controller { public MyContext db ...

  4. dbcontext实例创建问题

    dbcontext初始化 Private DemoContext db=new DemoContext (): 问题:什么时候释放db对象? 使用Using()方法中创建,每次调用会造成频繁的连接关闭 ...

  5. 潭州课堂25班:Ph201805201 django 项目 第三十四课 后台文章标签更新功能 ,创建功能实现(课堂笔记)

    g更改标签:,前台要向后台传来 id, name, 对标签进行校验:标签不能为空,标签是否已经存在, 流程: def put(self, request, tag_id): ''' 更改标签 :par ...

  6. EF Core 2.0中Transaction事务会对DbContext底层创建和关闭数据库连接的行为有所影响

    数据库 我们先在SQL Server数据库中建立一个Book表: CREATE TABLE [dbo].[Book]( ,) NOT NULL, ) NULL, ) NULL, ) NULL, [Cr ...

  7. ssh整合思想 Spring与Hibernate的整合 项目在服务器启动则自动创建数据库表

    Spring整合Hibernate Spring的Web项目中,web.xml文件会自动加载,以出现欢迎首页.也可以在这个文件中对Spring的配置文件进行监听,自启动配置文件, 以及之前Struts ...

  8. 零基础学习java------39---------json格式交互,Restful(不懂),静态资源映射,SSM整合(ssm整合思想,application.xml文件详解(声明式事务管理),)

    一. json格式交互(知道) 1 . 回顾ajax基本语法 $.ajax({ url:"", // 请求的后台路径 data:{"":"" ...

  9. ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第一章:创建基本的MVC Web站点

    在这一章中,我们将学习如何使用基架快速搭建和运行一个简单的Microsoft ASP.NET MVC Web站点.在我们马上投入学习和编码之前,我们首先了解一些有关ASP.NET MVC和Entity ...

随机推荐

  1. 100114D

    这道题用暴力水过了,蒟蒻是这么想的:枚举两个端点,找最小值,因为shift只能用一次,但是这样10^9*2.5要t,所以减掉只有一个黑点的情况,然后复杂度变为10^9*0.6 #include< ...

  2. Maven-生命周期

    Maven的生命周期是为了对所有的构建过程进行了抽象了,便于统一. clean(清理) 此生命周期旨在给工程做清理工作,它主要包含以下阶段: pre-clean - 执行项目清理前所需要的工作. cl ...

  3. python求职之路

    自我介绍 这是一道送分题,万年不变的第一个问题.不过有些小伙伴可能没有太在意,其实这个问题已经在面试官心中决定了你的去留意向.自我介绍的主要结构:个人基本信息 + 基本技术构成 + 项目经验(具体项目 ...

  4. 【HDU 1542】Atlantis(线段树+离散化,矩形面积并)

    求矩形面积并,离散化加线段树. 扫描线法: 用平行x轴的直线扫,每次ans+=(下一个高度-当前高度)*当前覆盖的宽度. #include<algorithm> #include<c ...

  5. cocoaPod的使用

    因为准备在项目中使用bootstrap,在安装bootstrap过程中提示需要Ruby的版本在1.9.2以上,而目前使用的Ruby版本是Mac系统自带的1.8.7.所以需要对Ruby进行升级.这里使用 ...

  6. FAILED java.lang.IllegalArgumentException: java.net.URISyntaxException: Relative path in absolute URI:hdfs:192.*

    运行的参数配置 hdfs:192.168.58.180/cf/userItem.txt 应该写成 hdfs://192.*

  7. 【poj1019】 Number Sequence

    http://poj.org/problem?id=1019 (题目链接) 题意 给出一个数:1 12 123 1234 12345 123456 1234567 12345678 123456789 ...

  8. SQL Server数据同步的研究(单向/双向)

    思路: 1.做中间件(简单:定时采集:复杂:分布式,订阅中心的形式,如微信的中间件:https://github.com/tencent-wechat/phxsql) 2.采用触发器的形式,有数据触发 ...

  9. Ext FileSystem Family、Ext2、Ext3

    catalog . 简介 . Ext2文件系统 . Ext3文件系统 . 小结 1. 简介 VFS虚拟文件系统接口和数据结构构成了一个框架,各个文件系统的实现都必须在框架内运转,但这并不要求每个文件系 ...

  10. js调用刷新

    _self.fireEvent('refresh');