Den här tråden kanske kan ge dig lite tips :)
http://www.webforum.nu/showthread.php?t=163666&highlight=entity
Sitter och labbar lite med entity framework, och dyker på lite funderingar. Inga speciella stora svårigheter ännu, håller mig till rätt simpla saker.
Byggt ett baseobject för repositories med lite grundläggande funktionalitet:
Imports System.Linq.Expressions
Imports System.Data.Objects
Imports System.Configuration
Public MustInherit Class RepositoryBase(Of TEntity)
#Region "Mustoverride methods"
Public MustOverride Sub MarkAsDeleted(ByVal entity As TEntity)
#End Region
#Region "Generall methods"
Public Function GetAll() As IEnumerable(Of TEntity)
Dim transaction As IDbTransaction = Context.Connection.BeginTransaction
Dim entities As IEnumerable(Of TEntity) = Nothing
Try
entities = Context.CreateQuery(Of TEntity)(String.Format("[{0}]", GetType(TEntity).Name))
transaction.Commit()
Context.Connection.Close()
Catch ex As Exception
transaction.Rollback()
Context.Connection.Close()
End Try
Return entities
End Function
Public Function GetByKey(ByVal where As Func(Of TEntity, Boolean)) As TEntity
Dim entity As TEntity = Nothing
Dim transaction As IDbTransaction = Context.Connection.BeginTransaction
Try
If Context.CreateQuery(Of TEntity)(String.Format("[{0}]", GetType(TEntity).Name)).Where(where).Count > 1 Then
Throw New Exception("Not unique entity.")
Else
entity = Context.CreateQuery(Of TEntity)(String.Format("[{0}]", GetType(TEntity).Name)).Where(where).First
End If
transaction.Commit()
Context.Connection.Close()
Catch ex As Exception
transaction.Rollback()
Context.Connection.Close()
End Try
Return entity
End Function
Public Function GetByFilter(ByVal where As Func(Of TEntity, Boolean)) As IEnumerable(Of TEntity)
Dim entities As IEnumerable(Of TEntity) = Nothing
Dim transaction As IDbTransaction = Context.Connection.BeginTransaction
Try
entities = Context.CreateQuery(Of TEntity)(String.Format("[{0}]", GetType(TEntity).Name)).Where(where)
transaction.Commit()
Context.Connection.Close()
Catch ex As Exception
transaction.Rollback()
Context.Connection.Close()
End Try
Return entities
End Function
#End Region
#Region "Datacontext"
Friend Function Context() As Domain.DataModelContainer
Dim entityConnectionString As String = ConfigurationManager.ConnectionStrings("DataModelContainer").ConnectionString
Dim connection As New EntityClient.EntityConnection(entityConnectionString)
connection.Open()
Return New Domain.DataModelContainer(connection)
End Function
#End Region
End Class
Funkar bra än så länge i alla fall.. på test och labb nivå. :)
När man lägger till entiteter från databasen ex. Customers, Employees, Suppliers.
Så skapar designern exakt samma objekt.. Customers, Employees Suppliers..
Känns lite dumt bara, skulle vara bättre om det var som i Linq To Sql, att de då blir customer, employee och supplier.
Alternativ är ju att byta namn på dem i modellen eller i databasen, byter jag namn på dem i modellen så funkar givetvis inte min ovanstående kod.
Låter så dumt tycker jag:
Dim employee As New Employees
With employee
.FirstName = "Kalle"
.LastName = "Andersson
End employee
ge mig lite tankar och lite vägledning kanske, och ge gärna någon kommentar på koden i baseobjektet också. :)


