[转]一步步学习EF Core(2.事务与日志)
本文转自:http://www.cnblogs.com/GuZhenYin/p/6862505.html
上节我们留了一个问题,为什么EF Core中,我们加载班级,数据并不会出来
其实答案很简单,~ 因为在EF Core1.1.2 中我们在EF6.0+中用到的的延迟加载功能并没有被加入,不过在EF Core 2.0中,这个功能将回归
而且这个功能是否需要被加入进去,社区也在激烈的讨论当中,有兴趣的可以去看看:
https://github.com/aspnet/EntityFramework/issues/3797
那么我们该如何加载关联的班级呢?.
直接通过Linq join当然是可以的. 我们也可以通过贪婪加载来获取,修改查询代码如下:
public IActionResult ListView()
{
return View(_context.UserTable.Include(a=>a.Class).ToList());
}
效果如下:

下面我们开始今天的内容
关于EF Core的事务,其实与EF 6.x几乎一样,代码如下:

using (var tran = _context.Database.BeginTransaction())
{
try
{
_context.ClassTable.Add(new ClassTable { ClassName = "AAAAA", ClassLevel = 2 });
_context.ClassTable.Add(new ClassTable { ClassName = "BBBBB", ClassLevel = 2 });
_context.SaveChanges();
throw new Exception("模拟异常");
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
// TODO: Handle failure
}
}

在异常中Rollback即可回滚,我这里的写法,其实有点无耻.
不过目的是告诉大家,要在Commit之前回滚.
不然会得到一个异常:This SqlTransaction has completed; it is no longer usable.”
下面我们来讲一下关于EF Core中的日志
我们知道,在ASP.NET Core中,大量的使用了IOC的手法来注入我们所需要的类.
EF Core其实也一样,.
首先我们需要创建一个EF日志类,继承Microsoft.Extensions.Logging.ILogger
如下:

private class EFLogger : ILogger
{
private readonly string categoryName; public EFLogger(string categoryName) => this.categoryName = categoryName; public bool IsEnabled(LogLevel logLevel)
{
return true;
} public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{ Debug.WriteLine($"时间:{DateTime.Now.ToString("o")} 日志级别: {logLevel} {eventId.Id} 产生的类{this.categoryName}");
DbCommandLogData data = state as DbCommandLogData;
Debug.WriteLine($"SQL语句:{data.CommandText},\n 执行消耗时间:{data.ElapsedMilliseconds}"); } public IDisposable BeginScope<TState>(TState state)
{
return null;
}
}

我这里面的Debug.WriteLine是为了方便调试.
正常情况下当然是写入日志文件,可以用Log4Net
然后,我们创建一个空的日志类(用来过滤不需要记录的日志)如下:

private class NullLogger : ILogger
{
public bool IsEnabled(LogLevel logLevel)
{
return false;
} public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{ } public IDisposable BeginScope<TState>(TState state)
{
return null;
}
}

然后,我们创建一个日志提供类(注入用,EF Core1.0版本注意注释),如下:

public class MyFilteredLoggerProvider : ILoggerProvider
{
public ILogger CreateLogger(string categoryName)
{
// NOTE: 这里要注意,这是 EF Core 1.1的使用方式,如果你用的 EF Core 1.0, 就需把IRelationalCommandBuilderFactory替换成下面的类
// Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory if (categoryName == typeof(IRelationalCommandBuilderFactory).FullName)
{
return new EFLogger(categoryName);
} return new NullLogger();
}
public void Dispose()
{ }
}

然后我们到Startup.cs的Configure()方法中注入我们的日志提供类
代码如下:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{ loggerFactory.AddProvider(new MyFilteredLoggerProvider());
....省略
}

运行程序,得到如下调试信息:

至此,我们就完成了日志的记录工作.
那么问题来了,在Asp.NET core中,我们可以这样注入进行日志记录.
如果在别的项目(比如控制台)中,怎么办?
下面就来解决这个问题.
在非Asp.NET core的程序中,我们需要把日志提供器从上下文里注入如下:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{ base.OnConfiguring(optionsBuilder);
LoggerFactory loggerFactory = new LoggerFactory();
loggerFactory.AddProvider(new MyFilteredLoggerProvider());
//注入
optionsBuilder.UseLoggerFactory(loggerFactory); }

写在最后,其实在EF Core的路线图中,我们可以看到,在2.0的版本将会提供一个更简单的日志记录方式
这段话是在(Features originally considered but for which we have made no progress and are essentially postponed)之后的:
..上面翻译过来的大概意思就是:我们原来考虑会加入的功能,但是现在并没有进展,基本要推迟的特点.(..总结三个字,然并卵)
- Simple Logging API (#1199) - We want a simple way to log the SQL being executed (like
Database.Logfrom EF6.x). We also want a simple way to view everything being logged. - 嗯..翻译过来的意思就是..我们想提供一个更简单的日志记录,比如像EF6.x中的
Database.Log 这样...()
还有一个比较有趣的东西如下:
在High priority features(高度优先的功能)中还有一段话:
- Simple command interception provides an easy way to read/write commands before/after they are sent to the database.
- 简单的命令拦截,将提供在发送到数据库之前/之后读取/写入命令的简单方法
我觉得这个有点类似于EF6.x的IDbCommandInterceptor.
感兴趣的朋友可以去了解一下,我之前的博文也有介绍:
EntityFramework的多种记录日志方式,记录错误并分析执行时间过长原因(系列4)
好了,就说这么多.
[转]一步步学习EF Core(2.事务与日志)的更多相关文章
- 一步步学习EF Core(2.事务与日志)
前言 上节我们留了一个问题,为什么EF Core中,我们加载班级,数据并不会出来 其实答案很简单,~ 因为在EF Core1.1.2 中我们在EF6.0+中用到的的延迟加载功能并没有被加入,不过在EF ...
- 一步步学习EF Core(1.DBFirst)
前言 很久没写博客了,因为真的很忙,终于空下来,打算学习一下EF Core顺便写个系列, 今天我们就来看看第一篇DBFirst. 本文环境:VS2017 Win7 .NET Core1.1 ...
- 一步步学习EF Core(3.EF Core2.0路线图)
前言 这几天一直在研究EF Core的官方文档,暂时没有发现什么比较新的和EF6.x差距比较大的东西. 不过我倒是发现了EF Core的路线图更新了,下面我们就来看看 今天我们来看看最新的EF Cor ...
- EF Core学习Code First
下面通过实例来学习EF Core Code First,也就是通过EF Core迁移来完成从模型生成数据库. 本实例使用EntityFrameworkCore SQLite 数据库进行介绍,大家也可以 ...
- EF Core 2.0中Transaction事务会对DbContext底层创建和关闭数据库连接的行为有所影响
数据库 我们先在SQL Server数据库中建立一个Book表: CREATE TABLE [dbo].[Book]( ,) NOT NULL, ) NULL, ) NULL, ) NULL, [Cr ...
- [翻译 EF Core in Action 2.3] 理解EF Core数据库查询
Entity Framework Core in Action Entityframework Core in action是 Jon P smith 所著的关于Entityframework Cor ...
- [翻译 EF Core in Action 2.2] 创建应用程序的数据库上下文
Entity Framework Core in Action Entityframework Core in action是 Jon P smith 所著的关于Entityframework Cor ...
- EF Core in Action 中文翻译 第一部分导航
Entityframework Core in action Entityframework Core in action是 Jon P smith 所著的关于Entityframework Core ...
- [翻译 EF Core in Action 2.1] 设置一个图书销售网站的场景
Entity Framework Core in Action Entityframework Core in action是 Jon P smith 所著的关于Entityframework Cor ...
随机推荐
- NET 下载共享文件
执行 public static void Run() { "); if (state) { // 共享文件夹的目录 TransportRemoteToLocal(@"\\192. ...
- .NET 简易方法拦截器
伟大的无产阶级Willaim曾说过:"无论你觉得自己多么的了不起,也永远有人比你更强".对,我说过!我就是william. 今天想记录一下在项目中遇到的一个比较有意思的东西,异常拦 ...
- 20164317《网络对抗技术》Exp3 免杀原理与实践
一.实验要求 1.1 正确使用msf编码器(0.5分),msfvenom生成如jar之类的其他文件(0.5分),veil-evasion(0.5分),加壳工具(0.5分),使用shellcode编程( ...
- jzoj5923
我們可以記f[i]表示i個點的連通圖的個數 則我們可以考慮將i個點不必聯通的圖個數(記為g)減去i個點的不連通圖個數 那麼f[i]=g[i]-c(j-1,i-1)f[j]gi-j 枚舉一個j,強制將j ...
- bhp 阅读笔记 OSX 下 setuptools pip 安装
安装 python-setuptools python-pip 尝试 brew install python-setuptools 失败 brew update 失败 $ cd `brew --pre ...
- poj3233 Matrix Power Series(矩阵快速幂)
题目要求的是 A+A2+...+Ak,而不是单个矩阵的幂. 那么可以构造一个分块的辅助矩阵 S,其中 A 为原矩阵,E 为单位矩阵,O 为0矩阵 将 S 取幂,会发现一个特性: Sk +1右上角 ...
- 第八天,scrapy的几个小技巧
一. 微博模拟登陆 1. 百度搜微博开放平台可满足爬取量不大的情况 2. 微博模拟登陆和下拉鼠标应对ajax加载 from selenium import webdriver import time ...
- jmeter 中使用ServerAgen链接超时可能出错的原因之一ip不对
因为我要压测的服务器是需要使用跳板机转发链接的,所以我开始用的是跳板机的IP+ServerAgen端口,发现连不通,实际上应该使用ServerAgen所在服务器的IP,如果:
- error: failed to push some refs to 'https://github.com/username/python.git'
解决error: failed to push some refs to 'https://github.com/bluepen/python.git' 当我们在使用git工具上传我们自己的代码时,可 ...
- iOS根据图片url获取尺寸
可以在UIImage的分类中加入下面的代码,并且引入系统的ImageIO.framework /** 根据图片的url获取尺寸 @param URL url @return CGSize */ + ( ...