Public Class Form1
Private _Entity As MyEntity.Entities
Public Property Entity() As MyEntity.Entities
Get
Return _Entity
End Get
Set(ByVal value As MyEntity.Entities)
_Entity = value
End Set
End Property
Private _MyRandom As New Random
Public Property MyRandom() As Random
Get
Return _MyRandom
End Get
Set(ByVal value As Random)
_MyRandom = value
End Set
End Property
”’
”’ Entity instance oluşturur.
”’
”’
”’
Private Function GetEntityInstance() As MyEntity.Entities
If (Me.Entity Is Nothing) Then _
Me.Entity = New MyEntity.Entities
Return Me.Entity
End Function
Private Sub GetList(ByVal startWith As Char)
Dim entity As MyEntity.Entities = Me.GetEntityInstance()
Dim str As String = startWith.ToString()
Dim locations = From l In entity.Location _
Where l.Name.StartsWith(str) _
Select l
Me.DataGridView1.DataSource = locations
End Sub
Private Sub GetList()
Dim entity As MyEntity.Entities = Me.GetEntityInstance()
Dim locations = From l In entity.Location _
Select l
Me.DataGridView1.DataSource = locations
” // *********************************************//
” // ALTERNATİF //
” // *********************************************//
‘Using entity As New MyEntity.Entities
‘ Dim locations = From l In entity.Location _
‘ Select l
‘ Me.DataGridView1.DataSource = locations
‘End Using
End Sub
Private Sub InsertLocation()
” // *********************************************//
” // ALTERNATİF //
” // *********************************************//
‘Using entity As New MyEntity.Entities
‘ Dim newObject As New MyEntity.Location
‘ newObject.ModifiedDate = DateTime.Now.Date
‘ newObject.Name = “F” & “_Yeni_” & Me.MyRandom.Next(0, 250).ToString()
‘ newObject.CostRate = 0
‘ newObject.Availability = 1
‘ entity.AddToLocation(newObject)
‘ entity.SaveChanges()
‘End Using
” // *********************************************//
” // ALTERNATİF //
” // *********************************************//
Dim entity As MyEntity.Entities = Me.GetEntityInstance()
Dim newobject As New MyEntity.Location
newobject.ModifiedDate = DateTime.Now.Date
newobject.Name = “f” & “_yeni_” & Me.MyRandom.Next(0, 250).ToString()
newobject.CostRate = 0
newobject.Availability = 1
entity.AddToLocation(newobject)
entity.SaveChanges()
End Sub
Private Sub DeleteLocation(ByVal id As Integer)
” // *********************************************//
” // ALTERNATİF //
” // *********************************************//
” Item Primary key kolonu değerine göre elde edilir.
”
‘Dim key As New EntityKey(“Entities.Location”, _
‘ New KeyValuePair(Of String, Object)() _
‘ { _
‘ New KeyValuePair(Of String, Object)(“LocationID”, CType(id, Int16)) _
‘ })
‘Dim location As MyEntity.Location = Me.Entity.GetObjectByKey(key)
‘If (location IsNot Nothing) Then
‘ Me.Entity.Location.Context.DeleteObject(location)
‘ Me.Entity.SaveChanges()
‘End If
” // *********************************************//
” // ALTERNATİF //
” // *********************************************//
” Item Primary key kolonu değerine göre elde edilir.
”
‘Using entity As New MyEntity.Entities
‘ ‘ Aranan item özelliklerinin belirtildiği nesne..
‘ Dim obj As New MyEntity.Location
‘ obj.LocationID = id
‘ ” Entity içinde nesneye erişmek için key oluşturulur.
‘ Dim key As EntityKey = entity.CreateEntityKey(obj.GetType().Name, obj)
‘ ” Item entity içinde eşleşirse..
‘ If (entity.TryGetObjectByKey(key, obj)) Then
‘ ” Sistemden silinir.
‘ entity.DeleteObject(obj)
‘ entity.SaveChanges()
‘ End If
‘End Using
” // *********************************************//
” // ALTERNATİF //
” // *********************************************//
” Silinecek nesne elde edilir.
Dim deleteObj As MyEntity.Location = Me.Entity.Location.Where( _
Function(loc) _
loc.LocationID = id).FirstOrDefault()
If (deleteObj IsNot Nothing) Then
” Kullanıcıdan silme onayı alınır..
Dim diaResult As DialogResult = _
MessageBox.Show(“Kayıt silinsin mi?”, “Silme İşlemi”, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
If (diaResult = Windows.Forms.DialogResult.Yes) Then
” Nesne silinir..
Me.Entity.DeleteObject(deleteObj)
Me.Entity.SaveChanges()
End If
End If
End Sub
Private Sub UpdateLocation(ByVal id As Integer)
” // *********************************************//
” // ALTERNATİF //
” // *********************************************//
‘ Dim entity As MyEntity.Entities = Me.GetEntityInstance()
‘ ” Güncellenecek nesne elde edilir.
‘ Dim updObj As MyEntity.Location = entity.Location.Where _
‘ (Function(obj) obj.LocationID = id).FirstOrDefault()
‘ ” Özelliklerine yeni değerler set edilir.
‘ With updObj
‘ .Name = “Türkiye” & id.ToString
‘ End With
‘ entity.SaveChanges()
” // *********************************************//
” // ALTERNATİF //
” // *********************************************//
Using entity As New MyEntity.Entities
” Güncellenecek nesne elde edilir.
Dim updObj As MyEntity.Location = entity.Location.Where _
(Function(obj) obj.LocationID = id).FirstOrDefault()
” Özelliklerine yeni değerler set edilir.
With updObj
.Name = “Türkiye” & id.ToString
End With
entity.SaveChanges()
End Using
End Sub
Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click
Me.GetList(“F”)
End Sub
Private Sub btnSelectAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectAll.Click
Me.GetList()
End Sub
Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
Me.InsertLocation()
Me.GetList()
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
” Seçili satır ID ‘leri silme işlemi için ilgili fonksiyona gönderilir.
For Each row As DataGridViewRow In Me.DataGridView1.SelectedRows
Dim locID As Integer = row.Cells(“LocationID”).Value
Me.DeleteLocation(locID)
Next
” Opsiyonel..
”Me.GetList()
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
” Seçili satır ID ‘leri silme işlemi için ilgili fonksiyona gönderilir.
For Each row As DataGridViewRow In Me.DataGridView1.SelectedRows
Dim locID As Integer = row.Cells(“LocationID”).Value
Me.UpdateLocation(locID)
Next
Me.GetList()
End Sub
End Class