12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- using Microsoft.EntityFrameworkCore;
- using PCC.Common.AssemblyInject.Interfaces;
- using PCC.DbEntity;
- namespace PCC.IO;
- internal class DbAccess : IAssemblyInjectSingleton
- {
- public DbAccess(ILogger<DbAccess> logger)
- {
- logger.LogInformation($"Init: Create DbContext Instance...");
- using var db = new PccDbContext();
- logger.LogInformation($"Init: Call CanConnect() ...");
- if (db.Database.CanConnect() == false)
- {
- logger.LogInformation($"Init: Creating Database...");
- db.Database.EnsureCreated();
- logger.LogInformation($"Creating Database: OK");
- }
- else
- {
- logger.LogInformation($"Init: OK");
- }
- }
- public int ChatRecordCreate(ChatRecord entity)
- {
- using var db = new PccDbContext();
- db.ChatRecords.Add(entity);
- db.SaveChanges();
- return entity.Id;
- }
- public void ChatRecordUpdateAckTimestamp(int id, DateTimeOffset ackSenderTimestamp)
- {
- using var db = new PccDbContext();
- var ur = new ChatRecord { Id = id, AckTimestamp = DateTimeOffset.Now };
- db.Attach(ur);
- db.Entry(ur).Property(p => p.AckTimestamp).IsModified = true;
- db.SaveChanges();
- }
- }
|