Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports EmployeePerformanceCore
- Imports Microsoft.AspNet.Identity.EntityFramework
- Imports System.Data.Entity
- Imports System.Data.Entity.Validation
- Imports System.Data.Entity.Infrastructure
- Imports System.Data.Entity.ModelConfiguration.Conventions
- Imports System.Data.Entity.ModelConfiguration.Configuration
- Imports System.Web.Script.Serialization
- Public Class EFDbContext
- Inherits IdentityDbContext(Of User, UserIdentityRole, Integer, UserLogin, UserRole, UserClaim)
- Implements IDataContext
- Public Sub New()
- MyBase.New()
- Me.Database.CommandTimeout = 240
- Database.SetInitializer(New MigrateDatabaseToLatestVersion(Of EmployeePerformanceInfrastructure.EFDbContext, EmployeePerformanceInfrastructure.Migrations.Configuration))
- End Sub
- Public Shadows ReadOnly Property configuration() As DbContextConfiguration Implements IDataContext.configuration
- Get
- Return MyBase.Configuration()
- End Get
- End Property
- Public Function save(Optional currentuserid As Integer = 0, Optional currentcompanyid As Integer = 0, Optional remarks As String = "") As Result Implements IDataContext.save
- Dim r As New Result
- Try
- If currentuserid > 0 And currentcompanyid > 0 Then
- Dim company As Company = Me.Companies.Where(Function(a) a.id = currentcompanyid).FirstOrDefault
- If Not company Is Nothing Then
- Dim changedentities As IEnumerable(Of DbEntityEntry) = MyBase.ChangeTracker.Entries.ToList
- For Each item In changedentities
- Dim currentstate As EntityState = item.State
- If Not currentstate = EntityState.Unchanged Then
- Dim currentvalues As DbPropertyValues
- If currentstate = EntityState.Deleted Then
- currentvalues = item.OriginalValues
- Else
- currentvalues = item.CurrentValues
- End If
- If Not item.Entity.ToString.Contains("BackgroundJobJson") And Not item.Entity.ToString.Contains("Email") And Not item.Entity.ToString.Contains("Role") Then
- Dim auditlog As New AuditLog
- auditlog.domainname = item.Entity.ToString
- auditlog.state = [Enum].GetName(GetType(EntityState), currentstate)
- auditlog.remarks = remarks
- Dim dictionary As New Dictionary(Of String, String)
- For Each propertyname In currentvalues.PropertyNames
- dictionary.Add(propertyname, currentvalues.Item(propertyname))
- Next
- Dim serializer = New JavaScriptSerializer()
- Dim json As String = serializer.Serialize(dictionary)
- auditlog.jsonvalue = json
- If currentuserid > 0 Then
- auditlog.createdbyuserid = currentuserid
- End If
- If currentcompanyid > 0 Then
- auditlog.companyid = currentcompanyid
- End If
- 'auditlog.description = ""
- auditlog.datetimecreated = DateTime.Now
- MyBase.Entry(auditlog).State = EntityState.Added
- End If
- End If
- Next
- End If
- End If
- Catch ex As Exception
- ' failed to add to audit log
- r.Warnings.Add("Failed to add to audit log because " & ex.Message)
- End Try
- Try
- MyBase.SaveChanges()
- Catch ex As DbEntityValidationException
- For Each item In ex.EntityValidationErrors
- For Each one In item.ValidationErrors
- r.AddError(item.Entry.Entity.ToString & " " & one.ErrorMessage)
- Next
- Next
- Return r
- Catch ex As DbUpdateException
- If Not ex.InnerException Is Nothing Then
- If Not ex.InnerException.InnerException Is Nothing Then
- If ex.InnerException.InnerException.Message.ToLower.Trim.Contains("reference constraint") Then
- r.AddError(EnumErrorResult.Undefined)
- End If
- r.Errors.Add(ex.InnerException.InnerException.Message)
- Else
- r.Errors.Add(ex.InnerException.Message)
- End If
- Else
- r.Errors.Add(ex.Message)
- End If
- Return r
- Catch ex As Exception
- If ex.InnerException Is Nothing Then
- r.Errors.Add(ex.Message)
- Else
- r.Errors.Add(ex.InnerException.Message)
- End If
- Return r
- End Try
- Return r
- End Function
- Public Function updatesave(obj As Object, Optional currentuserid As Integer = 0, Optional currentcompanyid As Integer = 0, Optional auditlogdescription As String = "") As Result Implements IDataContext.updatesave
- Dim r As New Result
- Try
- If currentuserid > 0 And currentcompanyid > 0 Then
- Dim company As Company = Me.Companies.Where(Function(a) a.id = currentcompanyid).FirstOrDefault
- If Not company Is Nothing Then
- Dim changedentities As IEnumerable(Of DbEntityEntry) = MyBase.ChangeTracker.Entries.ToList
- For Each item In changedentities
- Dim currentstate As EntityState = item.State
- If Not currentstate = EntityState.Unchanged Then
- Dim currentvalues As DbPropertyValues
- If currentstate = EntityState.Deleted Then
- currentvalues = item.OriginalValues
- Else
- currentvalues = item.CurrentValues
- End If
- If Not item.Entity.ToString.Contains("BackgroundJobJson") And Not item.Entity.ToString.Contains("Email") And Not item.Entity.ToString.Contains("Role") Then
- Dim auditlog As New AuditLog
- auditlog.domainname = item.Entity.ToString
- auditlog.state = [Enum].GetName(GetType(EntityState), currentstate)
- auditlog.remarks = auditlogdescription
- Dim dictionary As New Dictionary(Of String, String)
- For Each propertyname In currentvalues.PropertyNames
- dictionary.Add(propertyname, currentvalues.Item(propertyname))
- Next
- Dim serializer = New JavaScriptSerializer()
- Dim json As String = serializer.Serialize(dictionary)
- auditlog.jsonvalue = json
- If currentuserid > 0 Then
- auditlog.createdbyuserid = currentuserid
- End If
- If currentcompanyid > 0 Then
- auditlog.companyid = currentcompanyid
- End If
- 'auditlog.description = ""
- auditlog.datetimecreated = DateTime.Now
- MyBase.Entry(auditlog).State = EntityState.Added
- End If
- End If
- Next
- End If
- End If
- Catch ex As Exception
- ' failed to add to audit log
- r.Warnings.Add("Failed to add to audit log because " & ex.Message)
- End Try
- Try
- MyBase.Entry(obj).State = EntityState.Modified
- MyBase.SaveChanges()
- Catch ex As Exception
- r.Errors.Add(ex.Message)
- End Try
- Return r
- End Function
- Public Shadows Function entry(Of TEntity As Class)(entity As TEntity) As DbEntityEntry(Of TEntity) Implements IDataContext.entry
- Return MyBase.Entry(entity)
- End Function
- Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
- ' remove will cascade on delete by default
- modelBuilder.Conventions.Remove(Of System.Data.Entity.ModelConfiguration.Conventions.OneToManyCascadeDeleteConvention)()
- modelBuilder.Conventions.Remove(Of System.Data.Entity.ModelConfiguration.Conventions.ManyToManyCascadeDeleteConvention)()
- Precision.ConfigureModelBuidler(modelBuilder)
- MyBase.OnModelCreating(modelBuilder)
- End Sub
- 'Public Overrides Property Users As IDbSet(Of User) Implements IDataContext.Users
- 'Public Property Companies As IDbSet(Of Company) Implements IDataContext.Companies
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement