efcore oracle

网友投稿 570 2022-10-26

efcore oracle

efcore oracle

[Table("T_SYS_USER")] public class User { [Key] public string USER_ID { get; set; } }

添加上下文

public class SchemeContext : DbContext { public SchemeContext() : base() { } public SchemeContext(DbContextOptions options) : base(options) { } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { //项目中改为从配置中读取数据库连接字符串 optionsBuilder.UseOracle("Data Source=(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.1)(PORT = 1521)))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = Test)));User ID=test;Password=test;", f => f.UseOracleSQLCompatibility("12")); //日志 var loggerFactory = new LoggerFactory(); loggerFactory.AddProvider(new EFLoggerProvider()); optionsBuilder.UseLoggerFactory(loggerFactory); base.OnConfiguring(optionsBuilder); } //public DbSet UserTable { get; set; } public DbSet User { get; set; } }

日志

public class EFLogger : ILogger { private readonly string categoryName; public EFLogger(string categoryName) => this.categoryName = categoryName; public bool IsEnabled(LogLevel logLevel) => true; public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) { //ef core执行数据库查询时的categoryName为Microsoft.EntityFrameworkCore.Database.Command,日志级别为Information //if (categoryName == "Microsoft.EntityFrameworkCore.Database.Command" // && logLevel == LogLevel.Information) //{ var logContent = formatter(state, exception); //Console.WriteLine(); //Console.ForegroundColor = ConsoleColor.Green; //Console.WriteLine(logContent); //Console.ResetColor(); Trace.WriteLine(logContent); //} } public IDisposable BeginScope(TState state) => null; } public class EFLoggerProvider : ILoggerProvider { public ILogger CreateLogger(string categoryName) => new EFLogger(categoryName); public void Dispose() { } }

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:.net5 swagger添加复制链接
下一篇:SOUI - 是轻量级可快速开发window桌面程序开源DirectUI库.
相关文章

 发表评论

暂时没有评论,来抢沙发吧~