Last active
December 13, 2015 18:38
-
-
Save eyston/4956444 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Imports System.Data.Entity | |
Imports System.ComponentModel.DataAnnotations | |
Imports System.ComponentModel.DataAnnotations.Schema | |
Public Class PartContext | |
Inherits DbContext | |
Public Sub New() | |
MyBase.New("Data Source=(local);Initial Catalog=TestDB;Trusted_Connection=True;") | |
End Sub | |
Public Property Parts As DbSet(Of Part) | |
Public Property PartRevisions As DbSet(Of PartRevision) | |
Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder) | |
MyBase.OnModelCreating(modelBuilder) | |
modelBuilder.Entity(Of PartRevision)().HasKey(Function(r) r.Id).Property(Function(r) r.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity) | |
End Sub | |
End Class | |
<Table("Parts")> | |
Public Class Part | |
<Key(), DatabaseGenerated(DatabaseGeneratedOption.Identity)> | |
Public Overridable Property Id As Integer | |
Public Property PartNumber As String | |
Public Property Owner As String | |
End Class | |
<Table("PartRevisions")> | |
Public Class PartRevision | |
Inherits Part | |
<Key(), DatabaseGenerated(DatabaseGeneratedOption.Identity)> | |
Public Overrides Property Id As Integer | |
Public Property OriginalId As Integer | |
Public Property RevisionDate As DateTime | |
Public Property RevisionNumber As Integer | |
Public Property RevisionBy As String | |
Public Shared Function Revise(p As Part) As PartRevision | |
Dim rev As New PartRevision() | |
rev.RevisionDate = DateTime.Now | |
rev.RevisionNumber = 1 | |
rev.RevisionBy = "EEP" | |
rev.OriginalId = p.Id | |
rev.PartNumber = p.PartNumber | |
rev.Owner = p.Owner | |
Return rev | |
End Function | |
End Class | |
Module Module1 | |
Sub Main() | |
CreateRevision() | |
End Sub | |
Sub CreateRevision() | |
Using context As New iTracContext() | |
Dim p = context.Parts.Find(1) | |
Dim rev = PartRevision.Revise(p) | |
p.Owner = "XXX" | |
context.PartRevisions.Add(rev) | |
context.SaveChanges() | |
End Using | |
End Sub | |
End Module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment