// -- < For .Net WebApplication Log > -- public static void insertOneLog(ApplicationVM apperrVM) { using (DatabaseModel.ErrorModelDbContext db = new DatabaseModel.ErrorModelDbContext()) { db.Database.Initialize(false); var queryApp = (from app in db.ApplicationSet where app.ApplicationId == apperrVM.ApplicationId select app).First(); if (queryApp != null) { foreach (var errVM in apperrVM.Errors) { queryApp.Errors.Add(new Error() { ErrorMessage = errVM.ErrorMessage, Time = errVM.Time, LogLevel = errVM.LogLevel, ExMessage = errVM.ExMessage }); } } db.SaveChanges(); } }
// assign an application to some user / note in appFVM, personid could = 0! public static void updateAppForActiveAndOwnership(ApplicationPersonIdFlatVM appFVM) { using (DatabaseModel.ErrorModelDbContext db = new DatabaseModel.ErrorModelDbContext()) { db.Database.Initialize(false); var queryDbApp = (from a in db.ApplicationSet where a.ApplicationId == appFVM.ApplicationId select a).First(); queryDbApp.AppName = appFVM.AppName; queryDbApp.IsActive = appFVM.IsActive; // the updated PersonId could be 0, means not related to any user if (appFVM.PersonId > 0) { var queryDbPerson = (from p in db.PersonSet where p.PersonId == appFVM.PersonId select p).FirstOrDefault(); if (queryDbPerson != null) { // only update user's app if he hasn't own it if (false == queryDbApp.Persons.Any(p => p.PersonId == queryDbPerson.PersonId)) { queryDbApp.Persons.Add(queryDbPerson); } } } db.SaveChanges(); } }
/// <summary> /// run on a dedicated thread in static constructor using BlockingQ. /// List paramter help improve DB performance, opening DB once and insert chunk logs /// </summary> public static void insertLogs(ICollection <ApplicationVM> appErrList) { using (DatabaseModel.ErrorModelDbContext db = new DatabaseModel.ErrorModelDbContext()) { db.Database.Initialize(false); foreach (var appErrVM in appErrList) { var queryApp = (from app in db.ApplicationSet where app.ApplicationId == appErrVM.ApplicationId select app).First(); if (queryApp == null) { continue; } appErrVM.Errors.ToList().ForEach(errVM => queryApp.Errors.Add( new Error() { ErrorMessage = errVM.ErrorMessage, Time = errVM.Time, LogLevel = errVM.LogLevel, ExMessage = errVM.ExMessage } )); } db.SaveChanges(); } }
/// <summary> /// Delete one item of errLog via ErrId /// </summary> /// <param name="ApplicationId"></param> public static void deleteOneLog(int ErrId) { using (DatabaseModel.ErrorModelDbContext db = new DatabaseModel.ErrorModelDbContext()) { // Initialize the DB - false doesn't force reinitialization if the DB already exists db.Database.Initialize(false); var dbQueryErr = (from n in db.ErrorSet where n.ErrorId == ErrId select n).First(); db.ErrorSet.Remove(dbQueryErr); db.SaveChanges(); } }
public static void updateLoginTime(int loginId, DateTime now) { using (DatabaseModel.ErrorModelDbContext db = new DatabaseModel.ErrorModelDbContext()) { db.Database.Initialize(false); var dbQuery = (from n in db.LoginSet where n.LoginId == loginId select n).First(); dbQuery.LoginTime = now; db.SaveChanges(); } }
public static void createApp(ApplicationVM app) { using (DatabaseModel.ErrorModelDbContext db = new DatabaseModel.ErrorModelDbContext()) { // Initialize the DB - false doesn't force reinitialization if the DB already exists db.Database.Initialize(false); db.ApplicationSet.Add( new Application() { AppName = app.AppName, IsActive = app.IsActive } ); db.SaveChanges(); } }
/// <summary> /// delete all logs under an application /// </summary> /// <param name="ApplicationId"></param> public static void deleteAllLogsBelongToApp(int ApplicationId) { using (DatabaseModel.ErrorModelDbContext db = new DatabaseModel.ErrorModelDbContext()) { // Initialize the DB - false doesn't force reinitialization if the DB already exists db.Database.Initialize(false); var dbQueryApp = (from n in db.ApplicationSet where n.ApplicationId == ApplicationId select n).First(); //dbQueryApp.Errors.Clear(); it doesn't delete rows in ErrorTable dbQueryApp.Errors.ToList().ForEach( err => db.ErrorSet.Remove(err) ); db.SaveChanges(); } }
public static void updateLoginPersion(LoginInfoVM lg) { using (DatabaseModel.ErrorModelDbContext db = new DatabaseModel.ErrorModelDbContext()) { db.Database.Initialize(false); var dbinfo = (from n in db.LoginSet where n.LoginId == lg.LoginId select n).First(); dbinfo.Password = SharedCode.Authentication.Authentication.HashPassword(lg.OnePerson.Email, lg.Password); dbinfo.Role = lg.Role; dbinfo.IsLive = lg.IsLive; dbinfo.OnePerson.FirstName = lg.OnePerson.FirstName; dbinfo.OnePerson.LastName = lg.OnePerson.LastName; dbinfo.OnePerson.Email = lg.OnePerson.Email; dbinfo.LoginTime = dbinfo.LoginTime < lg.LoginTime ? lg.LoginTime : dbinfo.LoginTime; db.SaveChanges(); } }
// toDo, use SALT, prevent duplicate registering public static void createLoginPerson(LoginInfoVM lo) { using (DatabaseModel.ErrorModelDbContext db = new DatabaseModel.ErrorModelDbContext()) { // Initialize the DB - false doesn't force reinitialization if the DB already exists db.Database.Initialize(false); Person person = new Person() { FirstName = lo.OnePerson.FirstName, LastName = lo.OnePerson.LastName, Email = lo.OnePerson.Email }; string hashedPwd = SharedCode.Authentication.Authentication.HashPassword(person.Email, lo.Password); db.LoginSet.Add(new LoginInfo() { Password = hashedPwd, IsLive = true, Role = (int)RoleEnum.User, OnePerson = person }); db.SaveChanges(); } }