Advertisement
faizinfy

EFDbContext.vb

Dec 13th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 9.44 KB | None | 0 0
  1. Imports EmployeePerformanceCore
  2. Imports Microsoft.AspNet.Identity.EntityFramework
  3. Imports System.Data.Entity
  4. Imports System.Data.Entity.Validation
  5. Imports System.Data.Entity.Infrastructure
  6. Imports System.Data.Entity.ModelConfiguration.Conventions
  7. Imports System.Data.Entity.ModelConfiguration.Configuration
  8. Imports System.Web.Script.Serialization
  9.  
  10. Public Class EFDbContext
  11.     Inherits IdentityDbContext(Of User, UserIdentityRole, Integer, UserLogin, UserRole, UserClaim)
  12.     Implements IDataContext
  13.  
  14.     Public Sub New()
  15.         MyBase.New()
  16.         Me.Database.CommandTimeout = 240
  17.         Database.SetInitializer(New MigrateDatabaseToLatestVersion(Of EmployeePerformanceInfrastructure.EFDbContext, EmployeePerformanceInfrastructure.Migrations.Configuration))
  18.     End Sub
  19.  
  20.  
  21.     Public Shadows ReadOnly Property configuration() As DbContextConfiguration Implements IDataContext.configuration
  22.         Get
  23.             Return MyBase.Configuration()
  24.         End Get
  25.     End Property
  26.  
  27.  
  28.     Public Function save(Optional currentuserid As Integer = 0, Optional currentcompanyid As Integer = 0, Optional remarks As String = "") As Result Implements IDataContext.save
  29.         Dim r As New Result
  30.  
  31.  
  32.         Try
  33.                 If currentuserid > 0 And currentcompanyid > 0 Then
  34.                     Dim company As Company = Me.Companies.Where(Function(a) a.id = currentcompanyid).FirstOrDefault
  35.                     If Not company Is Nothing Then
  36.                         Dim changedentities As IEnumerable(Of DbEntityEntry) = MyBase.ChangeTracker.Entries.ToList
  37.                         For Each item In changedentities
  38.  
  39.                             Dim currentstate As EntityState = item.State
  40.  
  41.                             If Not currentstate = EntityState.Unchanged Then
  42.                                 Dim currentvalues As DbPropertyValues
  43.                                 If currentstate = EntityState.Deleted Then
  44.                                     currentvalues = item.OriginalValues
  45.                                 Else
  46.                                     currentvalues = item.CurrentValues
  47.                                 End If
  48.  
  49.                                 If Not item.Entity.ToString.Contains("BackgroundJobJson") And Not item.Entity.ToString.Contains("Email") And Not item.Entity.ToString.Contains("Role") Then
  50.                                     Dim auditlog As New AuditLog
  51.                                     auditlog.domainname = item.Entity.ToString
  52.                                     auditlog.state = [Enum].GetName(GetType(EntityState), currentstate)
  53.                                 auditlog.remarks = remarks
  54.  
  55.                                 Dim dictionary As New Dictionary(Of String, String)
  56.                                 For Each propertyname In currentvalues.PropertyNames
  57.                                     dictionary.Add(propertyname, currentvalues.Item(propertyname))
  58.                                 Next
  59.  
  60.                                 Dim serializer = New JavaScriptSerializer()
  61.                                 Dim json As String = serializer.Serialize(dictionary)
  62.                                 auditlog.jsonvalue = json
  63.                                 If currentuserid > 0 Then
  64.                                         auditlog.createdbyuserid = currentuserid
  65.                                     End If
  66.                                     If currentcompanyid > 0 Then
  67.                                         auditlog.companyid = currentcompanyid
  68.                                     End If
  69.                                     'auditlog.description = ""
  70.                                     auditlog.datetimecreated = DateTime.Now
  71.                                     MyBase.Entry(auditlog).State = EntityState.Added
  72.                                 End If
  73.                             End If
  74.                         Next
  75.                     End If
  76.  
  77.  
  78.                 End If
  79.             Catch ex As Exception
  80.                 ' failed to add to audit log
  81.                 r.Warnings.Add("Failed to add to audit log because " & ex.Message)
  82.             End Try
  83.  
  84.  
  85.             Try
  86.             MyBase.SaveChanges()
  87.         Catch ex As DbEntityValidationException
  88.             For Each item In ex.EntityValidationErrors
  89.                 For Each one In item.ValidationErrors
  90.                     r.AddError(item.Entry.Entity.ToString & " " & one.ErrorMessage)
  91.                 Next
  92.             Next
  93.  
  94.             Return r
  95.         Catch ex As DbUpdateException
  96.             If Not ex.InnerException Is Nothing Then
  97.                 If Not ex.InnerException.InnerException Is Nothing Then
  98.                     If ex.InnerException.InnerException.Message.ToLower.Trim.Contains("reference constraint") Then
  99.                         r.AddError(EnumErrorResult.Undefined)
  100.                     End If
  101.  
  102.                     r.Errors.Add(ex.InnerException.InnerException.Message)
  103.                 Else
  104.                     r.Errors.Add(ex.InnerException.Message)
  105.                 End If
  106.             Else
  107.                 r.Errors.Add(ex.Message)
  108.             End If
  109.  
  110.             Return r
  111.         Catch ex As Exception
  112.             If ex.InnerException Is Nothing Then
  113.                 r.Errors.Add(ex.Message)
  114.             Else
  115.                 r.Errors.Add(ex.InnerException.Message)
  116.             End If
  117.  
  118.             Return r
  119.         End Try
  120.  
  121.  
  122.         Return r
  123.     End Function
  124.  
  125.     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
  126.         Dim r As New Result
  127.  
  128.         Try
  129.             If currentuserid > 0 And currentcompanyid > 0 Then
  130.                 Dim company As Company = Me.Companies.Where(Function(a) a.id = currentcompanyid).FirstOrDefault
  131.                 If Not company Is Nothing Then
  132.                     Dim changedentities As IEnumerable(Of DbEntityEntry) = MyBase.ChangeTracker.Entries.ToList
  133.                     For Each item In changedentities
  134.  
  135.                         Dim currentstate As EntityState = item.State
  136.  
  137.                         If Not currentstate = EntityState.Unchanged Then
  138.                             Dim currentvalues As DbPropertyValues
  139.                             If currentstate = EntityState.Deleted Then
  140.                                 currentvalues = item.OriginalValues
  141.                             Else
  142.                                 currentvalues = item.CurrentValues
  143.                             End If
  144.  
  145.                             If Not item.Entity.ToString.Contains("BackgroundJobJson") And Not item.Entity.ToString.Contains("Email") And Not item.Entity.ToString.Contains("Role") Then
  146.                                 Dim auditlog As New AuditLog
  147.                                 auditlog.domainname = item.Entity.ToString
  148.                                 auditlog.state = [Enum].GetName(GetType(EntityState), currentstate)
  149.                                 auditlog.remarks = auditlogdescription
  150.  
  151.                                 Dim dictionary As New Dictionary(Of String, String)
  152.                                 For Each propertyname In currentvalues.PropertyNames
  153.                                     dictionary.Add(propertyname, currentvalues.Item(propertyname))
  154.                                 Next
  155.  
  156.                                 Dim serializer = New JavaScriptSerializer()
  157.                                 Dim json As String = serializer.Serialize(dictionary)
  158.                                 auditlog.jsonvalue = json
  159.                                 If currentuserid > 0 Then
  160.                                     auditlog.createdbyuserid = currentuserid
  161.                                 End If
  162.                                 If currentcompanyid > 0 Then
  163.                                     auditlog.companyid = currentcompanyid
  164.                                 End If
  165.                                 'auditlog.description = ""
  166.                                 auditlog.datetimecreated = DateTime.Now
  167.                                 MyBase.Entry(auditlog).State = EntityState.Added
  168.                             End If
  169.                         End If
  170.                     Next
  171.                 End If
  172.  
  173.  
  174.             End If
  175.         Catch ex As Exception
  176.             ' failed to add to audit log
  177.             r.Warnings.Add("Failed to add to audit log because " & ex.Message)
  178.         End Try
  179.  
  180.         Try
  181.             MyBase.Entry(obj).State = EntityState.Modified
  182.             MyBase.SaveChanges()
  183.         Catch ex As Exception
  184.             r.Errors.Add(ex.Message)
  185.         End Try
  186.  
  187.         Return r
  188.     End Function
  189.  
  190.     Public Shadows Function entry(Of TEntity As Class)(entity As TEntity) As DbEntityEntry(Of TEntity) Implements IDataContext.entry
  191.         Return MyBase.Entry(entity)
  192.     End Function
  193.  
  194.     Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
  195.         ' remove will cascade on delete by default
  196.         modelBuilder.Conventions.Remove(Of System.Data.Entity.ModelConfiguration.Conventions.OneToManyCascadeDeleteConvention)()
  197.         modelBuilder.Conventions.Remove(Of System.Data.Entity.ModelConfiguration.Conventions.ManyToManyCascadeDeleteConvention)()
  198.  
  199.         Precision.ConfigureModelBuidler(modelBuilder)
  200.         MyBase.OnModelCreating(modelBuilder)
  201.  
  202.     End Sub
  203.  
  204.     'Public Overrides Property Users As IDbSet(Of User) Implements IDataContext.Users
  205.     'Public Property Companies As IDbSet(Of Company) Implements IDataContext.Companies
  206.  
  207. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement