Versión Copiada del tfs
This commit is contained in:
104
Extensiones/DbContextExtensions.vb
Normal file
104
Extensiones/DbContextExtensions.vb
Normal file
@@ -0,0 +1,104 @@
|
||||
Imports System.Reflection
|
||||
Imports System.Runtime.CompilerServices
|
||||
Imports Microsoft.EntityFrameworkCore
|
||||
Imports Microsoft.EntityFrameworkCore.Internal
|
||||
Imports System.Linq.Dynamic.Core
|
||||
|
||||
|
||||
Namespace Extensiones
|
||||
Public Module DbContextExtensions
|
||||
<Extension()>
|
||||
Public Function Query(ByVal context As Microsoft.EntityFrameworkCore.DbContext, ByVal entityName As String) As IQueryable
|
||||
Return context.Query(context.Model.FindEntityType(entityName).ClrType)
|
||||
End Function
|
||||
|
||||
<Extension()>
|
||||
Public Function Query(ByVal context As Microsoft.EntityFrameworkCore.DbContext, ByVal entityType As Type) As IQueryable
|
||||
Return CType((CType(context, IDbSetCache)).GetOrAddSet(context.GetDependencies().SetSource, entityType), IQueryable)
|
||||
End Function
|
||||
|
||||
|
||||
<Extension()>
|
||||
Public Function ObtieneCampoIndice(ByVal context As Microsoft.EntityFrameworkCore.DbContext, ByVal Entidad As Object) As String
|
||||
Dim entry = context.Entry(Entidad)
|
||||
Dim pk = entry.Metadata.FindPrimaryKey()
|
||||
Return pk.Properties.FirstOrDefault()?.Name
|
||||
End Function
|
||||
|
||||
<Extension()>
|
||||
Public Function ObtieneMaximaLongitudCampo(bd As Microsoft.EntityFrameworkCore.DbContext, ByVal EspacioNombres As String, ByVal NombreTablaBase As String, ByVal NombreCampo As String) As Integer
|
||||
Try
|
||||
If Not NombreCampo.Contains(".") Then
|
||||
Dim Tabla = bd.Model.FindEntityType(EspacioNombres & "." & NombreTablaBase)
|
||||
If Tabla Is Nothing Then Throw New Exception("Tabla " & NombreTablaBase & " no encontrada")
|
||||
Dim Campo = Tabla.FindProperty(NombreCampo)
|
||||
If Campo Is Nothing Then
|
||||
Return -1 ' Throw New Exception("No existe el campo " & NombreCampo & " en la tabla " & NombreTablaBase)
|
||||
Else
|
||||
Dim LongitudCampo As Integer? = Campo.GetMaxLength()
|
||||
|
||||
If LongitudCampo.HasValue Then
|
||||
Return LongitudCampo.Value
|
||||
Else
|
||||
Return -1
|
||||
End If
|
||||
End If
|
||||
Else
|
||||
Return -1
|
||||
End If
|
||||
Catch ex As Exception
|
||||
Throw New Exception(ex.Message, ex)
|
||||
End Try
|
||||
End Function
|
||||
<Extension()>
|
||||
Public Function ExecSQL(Of T)(dbc As Microsoft.EntityFrameworkCore.DbContext, ByVal query As String) As List(Of T)
|
||||
Dim Command = dbc.Database.GetDbConnection().CreateCommand()
|
||||
Command.CommandText = query
|
||||
Command.CommandType = Data.CommandType.Text
|
||||
dbc.Database.OpenConnection()
|
||||
Dim list As List(Of T) = New List(Of T)()
|
||||
|
||||
Dim result = Command.ExecuteReader()
|
||||
Dim obj As T = Nothing
|
||||
|
||||
While result.Read()
|
||||
obj = Activator.CreateInstance(Of T)()
|
||||
|
||||
For Each prop As PropertyInfo In obj.[GetType]().GetProperties()
|
||||
|
||||
If Not Object.Equals(result(prop.Name), DBNull.Value) Then
|
||||
prop.SetValue(obj, result(prop.Name), Nothing)
|
||||
End If
|
||||
Next
|
||||
|
||||
list.Add(obj)
|
||||
End While
|
||||
dbc.Database.CloseConnection()
|
||||
Return list
|
||||
End Function
|
||||
|
||||
<Extension()>
|
||||
Public Function CompruebaRegistroUnico(bd As Microsoft.EntityFrameworkCore.DbContext, CompruebaIndice As Boolean, EspacioNombres As String, NombreTabla As String, NombreCampo As String, Valor As Object, Entidad As Object) As Boolean
|
||||
Dim Busqueda As String = NombreCampo & "==""" & CStr(Valor) & """"
|
||||
If CompruebaIndice Then
|
||||
Dim CampoIndice As String = bd.ObtieneCampoIndice(Entidad)
|
||||
Dim ValorCampoIndice As String = CStr(bd.Entry(Entidad).[Property](CampoIndice).CurrentValue.ToString())
|
||||
Busqueda += " && " & CampoIndice & "!=" & ValorCampoIndice
|
||||
End If
|
||||
Dim resp As Boolean = bd.Query(EspacioNombres & "." & NombreTabla).Any(Busqueda)
|
||||
Return Not resp
|
||||
End Function
|
||||
|
||||
<Extension()>
|
||||
Public Function AhoraMySql(ByVal context As Microsoft.EntityFrameworkCore.DbContext) As DateTime
|
||||
Dim cn = context.Database.GetDbConnection
|
||||
Dim cmd = cn.CreateCommand
|
||||
cmd.CommandText = "select now() as Ahora"
|
||||
cn.Open()
|
||||
Dim Hora As DateTime = cmd.ExecuteScalar
|
||||
cn.Close()
|
||||
Return Hora
|
||||
End Function
|
||||
|
||||
End Module
|
||||
End Namespace
|
||||
Reference in New Issue
Block a user