zoukankan      html  css  js  c++  java
  • 在EF6.0中打印数据库操作日志

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Text;  
    using System.Threading.Tasks;  
    using System.IO;  
    namespace EFLogging {  
        class Program {  
            private static CSharpCornerEntities context = new CSharpCornerEntities();  
            static void Main(string[] args) {  
                context.Database.Log = logInfo => FileLogger.Log(logInfo);  
                InsertEmployeeRecord();  
                UpdateEmployeeRecord(10024);  
                context.Database.Log = Console.Write;  
            }  
            private static void UpdateEmployeeRecord(long employeeID) {  
                //Linq  
                var empData = (from employee in context.Employees where employee.ID == employeeID select employee).FirstOrDefault();  
                //Lambda  
                //var empData = context.Employees.Where(a => a.ID.Equals(employeeID)).ToList();  
                empData.Location = "UK";  
                context.Entry(empData).State = System.Data.Entity.EntityState.Modified;  
                context.SaveChanges();  
            }  
            private static void InsertEmployeeRecord() {  
                context.Employees.Add(new Employee {  
                    Designation = "Software Engineer ", Location = "Chennai", Name = "DummyRecord"  
                });  
                context.SaveChanges();  
            }  
        }  
        public class FileLogger {  
            public static void Log(string logInfo) {  
                File.AppendAllText(@ "C:UsersSMKGDesktopLogger.txt", logInfo);  
            }  
        }  
    }  
  • 相关阅读:
    关于LINUX文件与目录的问题说明
    poj1094拓扑排序
    poj3026(bfs+prim)最小生成树
    快速幂
    hdu4255筛素数+广搜
    网易2012校园招聘笔试题目
    网新恒天2011.9.21招聘会笔试题
    HDU3344(小广搜+小暴力
    HDU3348(贪心求硬币数
    HDU3345广搜 (P,E,T,#)
  • 原文地址:https://www.cnblogs.com/luhe/p/9259820.html
Copyright © 2011-2022 走看看