(NET) NET (2015)

FinancialBroker - MDI application with EF code first database.

1.Database.


In this page I describe start of one my interesting project. This project is huge, and this is only a start. First point is database. This is my first project I try to use EF Code First technology with deploy and migration to MS SQL server. In this project I use relation M:1. 1:M, 1:1 and M:M Визначення зв'язків даних 1:1, 1:M, M:M у базі та EF CodeFirst..

I try to describe CRUD pattern around one entity of this project - Person.



This is main class of definition Person.


   1:  Imports System.ComponentModel.DataAnnotations
   2:   
   3:  Public Class Person
   4:   
   5:      Public Sub New()
   6:          Permission = New HashSet(Of User)
   7:          Phones = New HashSet(Of Phone)
   8:          Type = New HashSet(Of ContactType)
   9:          Mails = New HashSet(Of eMail)
  10:          Sites = New HashSet(Of WebSite)
  11:      End Sub
  12:   
  13:      <Key()>
  14:      Public Property I As Integer
  15:      Public Property PersonID As Guid
  16:   
  17:      Public Overridable Property Permission As ICollection(Of User)
  18:   
  19:      Public Property CrDate As DateTime
  20:   
  21:      '<Required(AllowEmptyStrings:=False)>
  22:      Public Overridable Property Type As ICollection(Of ContactType)
  23:   
  24:      <Required(AllowEmptyStrings:=False)>
  25:      Public Property ToStatusType As StatusType
  26:   
  27:      <MaxLength(50)>
  28:      <Required(AllowEmptyStrings:=False)>
  29:      Public Property NameFirst As String
  30:   
  31:      <MaxLength(50)>
  32:      <Required(AllowEmptyStrings:=False)>
  33:      Public Property NameMiddle As String
  34:   
  35:      <MaxLength(50)>
  36:      <Required(AllowEmptyStrings:=False)>
  37:      Public Property NameLast As String
  38:   
  39:      Public Property Title As String
  40:      Public Property Company As String
  41:   
  42:      Public Overridable Property Phones As ICollection(Of Phone)
  43:      Public Overridable Property Mails As ICollection(Of eMail)
  44:      Public Overridable Property Sites As ICollection(Of WebSite)
  45:   
  46:      Public Property AssignedTo As String
  47:   
  48:      Public Property SourceCompany As String
  49:      Public Property SourceContact As String
  50:   
  51:      Public Property ToCampaign As Campaign
  52:   
  53:      <MaxLength(1000)>
  54:      Public Property AdditionalInfo As String
  55:   
  56:  End Class

Person has relation M:1 to Campaign and StatusType.


   1:  Imports System.ComponentModel.DataAnnotations
   2:  Public Class Campaign
   3:   
   4:      Public Sub New()
   5:   
   6:      End Sub
   7:   
   8:      <Key()>
   9:      Public Property I As Integer
  10:      <MaxLength(50)>
  11:      Public Property Name As String
  12:   
  13:      Public Overridable Property Persons As ICollection(Of Person) = New HashSet(Of Person)
  14:   
  15:  End Class

   1:  Imports System.ComponentModel.DataAnnotations
   2:  Imports System.ComponentModel.DataAnnotations.Schema
   3:   
   4:  Public Enum StatusTypeEnum
   5:      Lead = 1
   6:      Assigned = 2
   7:      Active = 3
   8:      Declined = 4
   9:  End Enum
  10:   
  11:  Public Class StatusType
  12:   
  13:      Public Sub New()
  14:   
  15:      End Sub
  16:   
  17:      Public Sub New(ByVal [Enum] As StatusTypeEnum)
  18:          Id = CInt([Enum])
  19:          Name = [Enum].ToString()
  20:      End Sub
  21:   
  22:      <Key, DatabaseGenerated(DatabaseGeneratedOption.None)>
  23:      Public Property Id As Integer
  24:   
  25:      <Required, MaxLength(100)>
  26:      Public Property Name As String
  27:   
  28:      <MaxLength(100)>
  29:      Public Property Description As String
  30:   
  31:  End Class

StatusType is not simple. It has interesting opportunities - it based on ENUM and ENUM type is sort out during database initialization (SEED) and StatusType catalog is fills by data.

This is a code of SEED method with enumeration ENUM definition.


   1:  Imports System
   2:  Imports System.Data.Entity
   3:  Imports System.Data.Entity.Migrations
   4:  Imports System.Linq
   5:   
   6:  Namespace Migrations
   7:   
   8:      Friend NotInheritable Class Configuration
   9:          Inherits DbMigrationsConfiguration(Of FbDbContext)
  10:   
  11:          Public Sub New()
  12:              AutomaticMigrationsEnabled = False
  13:          End Sub
  14:   
  15:          Protected Overrides Sub Seed(context As FbDbContext)
  16:   
  17:              For Each One As StatusTypeEnum In System.Enum.GetValues(GetType(StatusTypeEnum))
  18:                  context.StatusTypes.AddOrUpdate(New StatusType(One))
  19:              Next
  20:              For Each One As ContactTypeEnum In System.Enum.GetValues(GetType(ContactTypeEnum))
  21:                  context.ContactTypes.AddOrUpdate(New ContactType(One))
  22:              Next
  23:              For Each One As eMailTypeEnum In System.Enum.GetValues(GetType(eMailTypeEnum))
  24:                  context.eMailTypes.AddOrUpdate(New eMailType(One))
  25:              Next
  26:              For Each One As PhoneTypeEnum In System.Enum.GetValues(GetType(PhoneTypeEnum))
  27:                  context.PhoneTypes.AddOrUpdate(New PhoneType(One))
  28:              Next
  29:              context.Users.Add(New User With {.UserName = "all", .UserID = Guid.Parse("00000000-0000-0000-0000-000000000001")})
  30:              context.SaveChanges()
  31:          End Sub
  32:   
  33:   
  34:      End Class
  35:   
  36:  End Namespace

Also this data structure has five M:M relation to another type - Permission, Type, Phones, Mails, Sites.


   1:  Imports System.ComponentModel.DataAnnotations
   2:  Public Class User
   3:   
   4:      Public Sub New()
   5:          ToPerson = New HashSet(Of Person)
   6:      End Sub
   7:   
   8:      <Key()>
   9:      Public Property I As Integer
  10:   
  11:      Public Property UserID As Guid
  12:   
  13:      <MaxLength(50)>
  14:      Public Property UserName As String
  15:   
  16:      Public Overridable Property ToPerson As ICollection(Of Person)
  17:   
  18:  End Class

   1:  Imports System.ComponentModel.DataAnnotations
   2:  Imports System.ComponentModel.DataAnnotations.Schema
   3:   
   4:  Public Enum ContactTypeEnum
   5:      Funder = 1
   6:      Broker = 2
   7:      Merchant = 3
   8:      Collector = 4
   9:      CreditCardProcessor = 5
  10:      Vendor = 6
  11:      Investor = 7
  12:      General = 8
  13:      Other = 9
  14:  End Enum
  15:   
  16:   
  17:  Public Class ContactType
  18:   
  19:      Public Sub New()
  20:          ToPerson = New HashSet(Of Person)
  21:      End Sub
  22:   
  23:      Public Sub New(ByVal [Enum] As ContactTypeEnum)
  24:          Id = CInt([Enum])
  25:          Name = [Enum].ToString()
  26:          ToPerson = New HashSet(Of Person)
  27:      End Sub
  28:   
  29:      <Key, DatabaseGenerated(DatabaseGeneratedOption.None)>
  30:      Public Property Id As Integer
  31:   
  32:      <Required, MaxLength(100)>
  33:      Public Property Name As String
  34:   
  35:      <MaxLength(100)>
  36:      Public Property Description As String
  37:   
  38:      Public Overridable Property ToPerson As ICollection(Of Person)
  39:   
  40:  End Class

   1:  Imports System.ComponentModel.DataAnnotations
   2:  Public Class Phone
   3:   
   4:      Public Sub New()
   5:          ToPerson = New HashSet(Of Person)
   6:      End Sub
   7:   
   8:      <Key()>
   9:      Public Property I As Integer
  10:   
  11:      Public Property Type As PhoneType
  12:   
  13:      <MaxLength(50)>
  14:      Public Property Number As String
  15:   
  16:      Public Overridable Property ToPerson As ICollection(Of Person)
  17:   
  18:  End Class

   1:  Imports System.ComponentModel.DataAnnotations
   2:   
   3:  Public Class eMail
   4:   
   5:      Public Sub New()
   6:          ToPerson = New HashSet(Of Person)
   7:      End Sub
   8:   
   9:      <Key()>
  10:      Public Property I As Integer
  11:   
  12:      Public Property Type As eMailType
  13:   
  14:      <MaxLength(50)>
  15:      Public Property Mail As String
  16:   
  17:      Public Overridable Property ToPerson As ICollection(Of Person)
  18:   
  19:  End Class

   1:  Imports System.ComponentModel.DataAnnotations
   2:  Public Class WebSite
   3:   
   4:      Public Sub New()
   5:          ToPerson = New HashSet(Of Person)
   6:      End Sub
   7:   
   8:      <Key()>
   9:      Public Property I As Integer
  10:   
  11:      <MaxLength(50)>
  12:      Public Property URL As String
  13:   
  14:      Public Overridable Property ToPerson As ICollection(Of Person)
  15:   
  16:  End Class

And Mail and Phones has own catalogue types - eMailTypeEnum and PhoneTypeEnum with relation M:1.


   1:  Imports System.ComponentModel.DataAnnotations
   2:  Imports System.ComponentModel.DataAnnotations.Schema
   3:   
   4:  Public Enum eMailTypeEnum
   5:      Business = 1
   6:      Privates = 2
   7:  End Enum
   8:   
   9:  Public Class eMailType
  10:   
  11:      Public Sub New()
  12:   
  13:      End Sub
  14:   
  15:      Public Sub New(ByVal [Enum] As eMailTypeEnum)
  16:          Id = CInt([Enum])
  17:          Name = [Enum].ToString()
  18:      End Sub
  19:   
  20:      <Key, DatabaseGenerated(DatabaseGeneratedOption.None)>
  21:      Public Property Id As Integer
  22:   
  23:      <Required, MaxLength(100)>
  24:      Public Property Name As String
  25:   
  26:      <MaxLength(100)>
  27:      Public Property Description As String
  28:   
  29:      Public Overridable Property eMails As ICollection(Of eMail) = New HashSet(Of eMail)
  30:   
  31:  End Class

   1:  Imports System.ComponentModel.DataAnnotations
   2:  Imports System.ComponentModel.DataAnnotations.Schema
   3:   
   4:  Public Enum PhoneTypeEnum
   5:      Mobile = 1
   6:      Home = 2
   7:      Work = 3
   8:      Fax = 4
   9:  End Enum
  10:   
  11:   
  12:  Public Class PhoneType
  13:   
  14:      Public Sub New()
  15:   
  16:      End Sub
  17:   
  18:      Public Sub New(ByVal [Enum] As PhoneTypeEnum)
  19:          Id = CInt([Enum])
  20:          Name = [Enum].ToString()
  21:      End Sub
  22:   
  23:      <Key, DatabaseGenerated(DatabaseGeneratedOption.None)>
  24:      Public Property Id As Integer
  25:   
  26:      <Required, MaxLength(100)>
  27:      Public Property Name As String
  28:   
  29:      <MaxLength(100)>
  30:      Public Property Description As String
  31:   
  32:  End Class

This is all around Person entity, except fluent API code, generated to deployment this class to DB.


   1:  Imports System
   2:  Imports System.Data.Entity.Migrations
   3:  Imports Microsoft.VisualBasic
   4:   
   5:  Namespace Migrations
   6:      Public Partial Class _1
   7:          Inherits DbMigration
   8:      
   9:          Public Overrides Sub Up()
  10:              CreateTable(
  11:                  "dbo.Campaigns",
  12:                  Function(c) New With
  13:                      {
  14:                          .I = c.Int(nullable := False, identity := True),
  15:                          .Name = c.String(maxLength := 50)
  16:                      }) _
  17:                  .PrimaryKey(Function(t) t.I)
  18:              
  19:              CreateTable(
  20:                  "dbo.People",
  21:                  Function(c) New With
  22:                      {
  23:                          .I = c.Int(nullable := False, identity := True),
  24:                          .PersonID = c.Guid(nullable := False),
  25:                          .CrDate = c.DateTime(nullable := False),
  26:                          .NameFirst = c.String(nullable := False, maxLength := 50),
  27:                          .NameMiddle = c.String(nullable := False, maxLength := 50),
  28:                          .NameLast = c.String(nullable := False, maxLength := 50),
  29:                          .Title = c.String(),
  30:                          .Company = c.String(),
  31:                          .AssignedTo = c.String(),
  32:                          .SourceCompany = c.String(),
  33:                          .SourceContact = c.String(),
  34:                          .AdditionalInfo = c.String(maxLength := 1000),
  35:                          .ToCampaign_I = c.Int(),
  36:                          .ToStatusType_Id = c.Int(nullable := False)
  37:                      }) _
  38:                  .PrimaryKey(Function(t) t.I) _
  39:                  .ForeignKey("dbo.Campaigns", Function(t) t.ToCampaign_I) _
  40:                  .ForeignKey("dbo.StatusTypes", Function(t) t.ToStatusType_Id, cascadeDelete := True) _
  41:                  .Index(Function(t) t.ToCampaign_I) _
  42:                  .Index(Function(t) t.ToStatusType_Id)
  43:              
  44:              CreateTable(
  45:                  "dbo.eMails",
  46:                  Function(c) New With
  47:                      {
  48:                          .I = c.Int(nullable := False, identity := True),
  49:                          .Mail = c.String(maxLength := 50),
  50:                          .Type_Id = c.Int()
  51:                      }) _
  52:                  .PrimaryKey(Function(t) t.I) _
  53:                  .ForeignKey("dbo.eMailTypes", Function(t) t.Type_Id) _
  54:                  .Index(Function(t) t.Type_Id)
  55:              
  56:              CreateTable(
  57:                  "dbo.eMailTypes",
  58:                  Function(c) New With
  59:                      {
  60:                          .Id = c.Int(nullable := False),
  61:                          .Name = c.String(nullable := False, maxLength := 100),
  62:                          .Description = c.String(maxLength := 100)
  63:                      }) _
  64:                  .PrimaryKey(Function(t) t.Id)
  65:              
  66:              CreateTable(
  67:                  "dbo.Users",
  68:                  Function(c) New With
  69:                      {
  70:                          .I = c.Int(nullable := False, identity := True),
  71:                          .UserID = c.Guid(nullable := False),
  72:                          .UserName = c.String(maxLength := 50)
  73:                      }) _
  74:                  .PrimaryKey(Function(t) t.I)
  75:              
  76:              CreateTable(
  77:                  "dbo.Phones",
  78:                  Function(c) New With
  79:                      {
  80:                          .I = c.Int(nullable := False, identity := True),
  81:                          .Number = c.String(maxLength := 50),
  82:                          .Type_Id = c.Int()
  83:                      }) _
  84:                  .PrimaryKey(Function(t) t.I) _
  85:                  .ForeignKey("dbo.PhoneTypes", Function(t) t.Type_Id) _
  86:                  .Index(Function(t) t.Type_Id)
  87:              
  88:              CreateTable(
  89:                  "dbo.PhoneTypes",
  90:                  Function(c) New With
  91:                      {
  92:                          .Id = c.Int(nullable := False),
  93:                          .Name = c.String(nullable := False, maxLength := 100),
  94:                          .Description = c.String(maxLength := 100)
  95:                      }) _
  96:                  .PrimaryKey(Function(t) t.Id)
  97:              
  98:              CreateTable(
  99:                  "dbo.WebSites",
 100:                  Function(c) New With
 101:                      {
 102:                          .I = c.Int(nullable := False, identity := True),
 103:                          .URL = c.String(maxLength := 50)
 104:                      }) _
 105:                  .PrimaryKey(Function(t) t.I)
 106:              
 107:              CreateTable(
 108:                  "dbo.StatusTypes",
 109:                  Function(c) New With
 110:                      {
 111:                          .Id = c.Int(nullable := False),
 112:                          .Name = c.String(nullable := False, maxLength := 100),
 113:                          .Description = c.String(maxLength := 100)
 114:                      }) _
 115:                  .PrimaryKey(Function(t) t.Id)
 116:              
 117:              CreateTable(
 118:                  "dbo.ContactTypes",
 119:                  Function(c) New With
 120:                      {
 121:                          .Id = c.Int(nullable := False),
 122:                          .Name = c.String(nullable := False, maxLength := 100),
 123:                          .Description = c.String(maxLength := 100)
 124:                      }) _
 125:                  .PrimaryKey(Function(t) t.Id)
 126:              
 127:              CreateTable(
 128:                  "dbo.eMailPersons",
 129:                  Function(c) New With
 130:                      {
 131:                          .eMail_I = c.Int(nullable := False),
 132:                          .Person_I = c.Int(nullable := False)
 133:                      }) _
 134:                  .PrimaryKey(Function(t) New With { t.eMail_I, t.Person_I }) _
 135:                  .ForeignKey("dbo.eMails", Function(t) t.eMail_I, cascadeDelete := True) _
 136:                  .ForeignKey("dbo.People", Function(t) t.Person_I, cascadeDelete := True) _
 137:                  .Index(Function(t) t.eMail_I) _
 138:                  .Index(Function(t) t.Person_I)
 139:              
 140:              CreateTable(
 141:                  "dbo.UserPersons",
 142:                  Function(c) New With
 143:                      {
 144:                          .User_I = c.Int(nullable := False),
 145:                          .Person_I = c.Int(nullable := False)
 146:                      }) _
 147:                  .PrimaryKey(Function(t) New With { t.User_I, t.Person_I }) _
 148:                  .ForeignKey("dbo.Users", Function(t) t.User_I, cascadeDelete := True) _
 149:                  .ForeignKey("dbo.People", Function(t) t.Person_I, cascadeDelete := True) _
 150:                  .Index(Function(t) t.User_I) _
 151:                  .Index(Function(t) t.Person_I)
 152:              
 153:              CreateTable(
 154:                  "dbo.PhonePersons",
 155:                  Function(c) New With
 156:                      {
 157:                          .Phone_I = c.Int(nullable := False),
 158:                          .Person_I = c.Int(nullable := False)
 159:                      }) _
 160:                  .PrimaryKey(Function(t) New With { t.Phone_I, t.Person_I }) _
 161:                  .ForeignKey("dbo.Phones", Function(t) t.Phone_I, cascadeDelete := True) _
 162:                  .ForeignKey("dbo.People", Function(t) t.Person_I, cascadeDelete := True) _
 163:                  .Index(Function(t) t.Phone_I) _
 164:                  .Index(Function(t) t.Person_I)
 165:              
 166:              CreateTable(
 167:                  "dbo.WebSitePersons",
 168:                  Function(c) New With
 169:                      {
 170:                          .WebSite_I = c.Int(nullable := False),
 171:                          .Person_I = c.Int(nullable := False)
 172:                      }) _
 173:                  .PrimaryKey(Function(t) New With { t.WebSite_I, t.Person_I }) _
 174:                  .ForeignKey("dbo.WebSites", Function(t) t.WebSite_I, cascadeDelete := True) _
 175:                  .ForeignKey("dbo.People", Function(t) t.Person_I, cascadeDelete := True) _
 176:                  .Index(Function(t) t.WebSite_I) _
 177:                  .Index(Function(t) t.Person_I)
 178:              
 179:              CreateTable(
 180:                  "dbo.ContactTypePersons",
 181:                  Function(c) New With
 182:                      {
 183:                          .ContactType_Id = c.Int(nullable := False),
 184:                          .Person_I = c.Int(nullable := False)
 185:                      }) _
 186:                  .PrimaryKey(Function(t) New With { t.ContactType_Id, t.Person_I }) _
 187:                  .ForeignKey("dbo.ContactTypes", Function(t) t.ContactType_Id, cascadeDelete := True) _
 188:                  .ForeignKey("dbo.People", Function(t) t.Person_I, cascadeDelete := True) _
 189:                  .Index(Function(t) t.ContactType_Id) _
 190:                  .Index(Function(t) t.Person_I)
 191:              
 192:          End Sub
 193:          
 194:          Public Overrides Sub Down()
 195:              DropForeignKey("dbo.ContactTypePersons", "Person_I", "dbo.People")
 196:              DropForeignKey("dbo.ContactTypePersons", "ContactType_Id", "dbo.ContactTypes")
 197:              DropForeignKey("dbo.People", "ToStatusType_Id", "dbo.StatusTypes")
 198:              DropForeignKey("dbo.People", "ToCampaign_I", "dbo.Campaigns")
 199:              DropForeignKey("dbo.WebSitePersons", "Person_I", "dbo.People")
 200:              DropForeignKey("dbo.WebSitePersons", "WebSite_I", "dbo.WebSites")
 201:              DropForeignKey("dbo.Phones", "Type_Id", "dbo.PhoneTypes")
 202:              DropForeignKey("dbo.PhonePersons", "Person_I", "dbo.People")
 203:              DropForeignKey("dbo.PhonePersons", "Phone_I", "dbo.Phones")
 204:              DropForeignKey("dbo.UserPersons", "Person_I", "dbo.People")
 205:              DropForeignKey("dbo.UserPersons", "User_I", "dbo.Users")
 206:              DropForeignKey("dbo.eMails", "Type_Id", "dbo.eMailTypes")
 207:              DropForeignKey("dbo.eMailPersons", "Person_I", "dbo.People")
 208:              DropForeignKey("dbo.eMailPersons", "eMail_I", "dbo.eMails")
 209:              DropIndex("dbo.ContactTypePersons", New String() { "Person_I" })
 210:              DropIndex("dbo.ContactTypePersons", New String() { "ContactType_Id" })
 211:              DropIndex("dbo.WebSitePersons", New String() { "Person_I" })
 212:              DropIndex("dbo.WebSitePersons", New String() { "WebSite_I" })
 213:              DropIndex("dbo.PhonePersons", New String() { "Person_I" })
 214:              DropIndex("dbo.PhonePersons", New String() { "Phone_I" })
 215:              DropIndex("dbo.UserPersons", New String() { "Person_I" })
 216:              DropIndex("dbo.UserPersons", New String() { "User_I" })
 217:              DropIndex("dbo.eMailPersons", New String() { "Person_I" })
 218:              DropIndex("dbo.eMailPersons", New String() { "eMail_I" })
 219:              DropIndex("dbo.Phones", New String() { "Type_Id" })
 220:              DropIndex("dbo.eMails", New String() { "Type_Id" })
 221:              DropIndex("dbo.People", New String() { "ToStatusType_Id" })
 222:              DropIndex("dbo.People", New String() { "ToCampaign_I" })
 223:              DropTable("dbo.ContactTypePersons")
 224:              DropTable("dbo.WebSitePersons")
 225:              DropTable("dbo.PhonePersons")
 226:              DropTable("dbo.UserPersons")
 227:              DropTable("dbo.eMailPersons")
 228:              DropTable("dbo.ContactTypes")
 229:              DropTable("dbo.StatusTypes")
 230:              DropTable("dbo.WebSites")
 231:              DropTable("dbo.PhoneTypes")
 232:              DropTable("dbo.Phones")
 233:              DropTable("dbo.Users")
 234:              DropTable("dbo.eMailTypes")
 235:              DropTable("dbo.eMails")
 236:              DropTable("dbo.People")
 237:              DropTable("dbo.Campaigns")
 238:          End Sub
 239:      End Class
 240:  End Namespace

2. MDI Form control.

This is conception of MDI-form MDI.avi. This is main form code fragment.


   1:  Imports System.Windows.Forms
   2:   
   3:  Partial Public Class MainMDI
   4:   
   5:      Public AddApplicationForm1 As AddApplicationForm
   6:      Public AddCompanyForm1 As AddCompanyForm
   7:      Public AddDealToolForm1 As AddDealToolForm
   8:      Public AddLeadForm1 As AddLeadForm
   9:      Public AddPersonForm1 As AddPersonForm
  ...   
  49:      Public ReportHistoryForm1 As ReportHistoryForm
  50:      Public ReportsDefaultForm1 As ReportsDefaultForm
  51:      Public ReportsForm1 As ReportsForm
  52:      Public ReportsPerformanceForm1 As ReportsPerformanceForm
  53:      Public ReportsPipelineForm1 As ReportsPipelineForm
  54:      Public ReportsPortfolioForm1 As ReportsPortfolioForm
  55:      Public ReportsRenewalForm1 As ReportsRenewalForm
  56:      Public ReportsRepaidForm1 As ReportsRepaidForm
  57:      Public ReportsRepaymentForm1 As ReportsRepaymentForm
  58:      Public ReportsSyndicationForm1 As ReportsSyndicationForm
  59:   
  60:      Public WithEvents Timer1 As New Timer
  61:      Public LoginForm1 As New LoginForm
  62:   
  63:      Private Sub MainMDI_Load(sender As Object, e As EventArgs) Handles Me.Load
  64:          Text &= " (" & My.Application.Info.Version.ToString & ")"
  65:          Me.WindowState = FormWindowState.Maximized
  66:          Timer1.Interval = 100
  67:          Timer1.Start()
  68:      End Sub
  69:   
  70:      Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
  71:          Timer1.Stop()
  72:          LoginForm1.WindowState = FormWindowState.Normal
  73:          LoginForm1.TopMost = True
  74:          LoginForm1.StartPosition = FormStartPosition.CenterScreen
  75:          LoginForm1.ShowDialog()
  76:      End Sub
  77:   
  78:  #Region "Toolbars"
  79:   
  80:      Private Sub AddCompanyStripButton_Click(sender As Object, e As EventArgs) Handles AddCompanyStripButton.Click
  81:          AddCompanyForm_Open()
  82:      End Sub
  83:   
  84:      Private Sub AddPersonToolStripButton_Click(sender As Object, e As EventArgs) Handles AddPersonToolStripButton.Click
  85:          AddPersonForm_Open()
  86:      End Sub
  87:   
  88:      Private Sub AddApplicationToolStripButton_Click(sender As Object, e As EventArgs) Handles AddApplicationToolStripButton.Click
  89:          AddApplicationForm_Open()
  90:      End Sub
  91:   
  92:      Private Sub AddDealToolStripButton_Click(sender As Object, e As EventArgs) Handles AddDealToolStripButton.Click
  93:          AddDealToolForm_Open()
  94:      End Sub
  95:   
  96:  #End Region
  97:   
  98:  #Region "Home"
  99:   
 100:      Private Sub HomeTaskToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles HomeTaskToolStripMenuItem.Click
 101:          HomeTaskForm_Open()
 102:      End Sub
 103:   
 104:      Private Sub HomeMyDealsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles HomeMyDealsToolStripMenuItem.Click
 105:          HomeMyDealsForm_Open()
 106:      End Sub
 107:   
 108:      Private Sub HomeNotificationToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles HomeNotificationToolStripMenuItem.Click
 109:          HomeNotificationForm_Open()
 110:      End Sub
 111:   
 112:   
 113:  #End Region
  ...   
 346:   
 347:  #Region "Help"
 348:   
 349:      Private Sub HelpContentsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles HelpContentsToolStripMenuItem.Click
 350:          HelpContentsForm_Open()
 351:      End Sub
 352:   
 353:      Private Sub HelpIndexToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles HelpIndexToolStripMenuItem.Click
 354:          HelpIndexForm_Open()
 355:      End Sub
 356:   
 357:      Private Sub HelpSearchToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles HelpSearchToolStripMenuItem.Click
 358:          HelpSearchForm_Open()
 359:      End Sub
 360:   
 361:      Private Sub HelpAboutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles HelpAboutToolStripMenuItem.Click
 362:          HelpAboutForm_Open()
 363:      End Sub
 364:   
 365:   
 366:  #End Region
 367:   
 368:  End Class

Some function I created my own macroprocessor and placed it to second file co continue main form definition file.


   1:  Partial Public Class MainMDI
   2:   
   3:      Public Sub AddApplicationForm_Open()
   4:          If Not (Me.MdiChildren.Any(Function(X) X Is AddApplicationForm1)) Then
   5:              AddApplicationForm1 = AddApplicationForm1.OpenAsMDI
   6:          Else
   7:              AddApplicationForm1.WindowState = FormWindowState.Normal
   8:          End If
   9:          AddApplicationForm1.BringToFront()
  10:      End Sub
  11:   
  12:   
  13:      Public Sub AddCompanyForm_Open()
  14:          If Not (Me.MdiChildren.Any(Function(X) X Is AddCompanyForm1)) Then
  15:              AddCompanyForm1 = AddCompanyForm1.OpenAsMDI
  16:          Else
  17:              AddCompanyForm1.WindowState = FormWindowState.Normal
  18:          End If
  19:          AddCompanyForm1.BringToFront()
  20:      End Sub
  21:   
  ...   
 531:   
 532:   
 533:      Public Sub ReportsSyndicationForm_Open()
 534:          If Not (Me.MdiChildren.Any(Function(X) X Is ReportsSyndicationForm1)) Then
 535:              ReportsSyndicationForm1 = ReportsSyndicationForm1.OpenAsMDI
 536:          Else
 537:              ReportsSyndicationForm1.WindowState = FormWindowState.Normal
 538:          End If
 539:          ReportsSyndicationForm1.BringToFront()
 540:      End Sub
 541:   
 542:   
 543:   
 544:  End Class

Third part of MDI-form control is polymorphism extension functions. It also automatically generated by my own macroprocessor.


   1:  Imports System.Runtime.CompilerServices
   2:   
   3:  Module MDIextension1
   4:   
   5:      <Extension>
   6:      Public Function OpenAsMDI(X As AddApplicationForm) As AddApplicationForm
   7:          OpenAsMDI = New AddApplicationForm
   8:          OpenAsMDI.MdiParent = MainMDI
   9:          OpenAsMDI.Show()
  10:          OpenAsMDI.BringToFront()
  11:      End Function
  12:   
  13:   
  14:      <Extension>
  15:      Public Function OpenAsMDI(X As AddCompanyForm) As AddCompanyForm
  16:          OpenAsMDI = New AddCompanyForm
  17:          OpenAsMDI.MdiParent = MainMDI
  18:          OpenAsMDI.Show()
  19:          OpenAsMDI.BringToFront()
  20:      End Function
  21:   
  ...   
 481:   
 482:      <Extension>
 483:      Public Function OpenAsMDI(X As ReportsSyndicationForm) As ReportsSyndicationForm
 484:          OpenAsMDI = New ReportsSyndicationForm
 485:          OpenAsMDI.MdiParent = MainMDI
 486:          OpenAsMDI.Show()
 487:          OpenAsMDI.BringToFront()
 488:      End Function
 489:   
 490:   
 491:   
 492:  End Module

3.CRUD form for Person entity.

This is Add form.



   1:  Imports Microsoft.VisualBasic
   2:   
   3:  Public Class AddPersonForm
   4:   
   5:      Dim db1 As New FbDbContext
   6:   
   7:      Private Sub AddPersonForm_Load(sender As Object, e As EventArgs) Handles Me.Load
   8:          Dim A = db1.Campaigns.ToList
   9:          CampaignsComboBox.DataSource = A
  10:          CampaignsComboBox.DisplayMember = "Name"
  11:          Dim S = db1.StatusTypes.ToList
  12:          StatusComboBox.DataSource = S
  13:          StatusComboBox.DisplayMember = "Name"
  14:          Dim P() As String = {"All", My.Settings.UserName1, My.Settings.UserName2}
  15:          PermissionListView.CheckBoxes = True
  16:          PermissionListView.View = View.Details
  17:          PermissionListView.Columns.Add("User", 120, HorizontalAlignment.Left)
  18:          P.ToList.ForEach(Sub(X) PermissionListView.Items.Add(X))
  19:          Dim T = db1.ContactTypes.ToList
  20:          TypeListView.CheckBoxes = True
  21:          TypeListView.View = View.Details
  22:          TypeListView.Columns.Add("User", 120, HorizontalAlignment.Left)
  23:          T.ForEach(Sub(X) TypeListView.Items.Add(X.Name))
  24:          For Each One As ListViewItem In PermissionListView.Items
  25:              If One.Text = "All" Then
  26:                  One.Checked = True
  27:                  Exit For
  28:              End If
  29:          Next
  30:      End Sub
  31:   
  32:      Dim X As Person
  33:      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  34:          X = New Person
  35:          Try
  36:              With X
  37:                  Dim ListContactType As New List(Of ContactType)
  38:                  For Each One As ListViewItem In TypeListView.Items
  39:                      If One.Checked Then
  40:                          ListContactType.Add(db1.ContactTypes.Where(Function(Q) Q.Name = One.Text).FirstOrDefault)
  41:                      End If
  42:                  Next
  43:   
  44:                  Dim ListPermission As New List(Of User)
  45:                  For Each One As ListViewItem In PermissionListView.Items
  46:                      If One.Checked Then
  47:                          ListPermission.Add(db1.Users.Where(Function(Q) Q.UserName = One.Text).FirstOrDefault)
  48:                      End If
  49:                  Next
  50:   
  51:                  If Not String.IsNullOrWhiteSpace(CampaignsComboBox.Text) Then
  52:                      Dim Camp = db1.Campaigns.Add(New Campaign())
  53:                      Camp.Name = CampaignsComboBox.Text
  54:                      .ToCampaign = Camp
  55:                  Else
  56:                      .ToCampaign = Nothing
  57:                  End If
  58:   
  59:   
  60:                  Dim ST As String() = SiteTextBox.Text.Split(vbCrLf)
  61:                  Dim ListWebSite As New List(Of WebSite)
  62:                  For Each One In ST
  63:                      If Not String.IsNullOrWhiteSpace(One) Then
  64:                          Dim uriResult As Uri
  65:                          Dim ValidURL As Boolean = Uri.TryCreate(One, UriKind.Absolute, uriResult) AndAlso (uriResult.Scheme = Uri.UriSchemeHttp Or uriResult.Scheme = Uri.UriSchemeHttps)
  66:                          If Not ValidURL Then
  67:                              MsgBox(One & vbCrLf & "is not valis URL")
  68:                              Exit Sub
  69:                          End If
  70:                          ListWebSite.Add(New WebSite With {.URL = One})
  71:                      End If
  72:                  Next
  73:   
  74:                  Dim PhoneType1 As PhoneType = db1.PhoneTypes.Where(Function(a) a.Id = PhoneTypeEnum.Home).FirstOrDefault
  75:                  Dim PT As String() = PhonesTextBox.Text.Split(vbCrLf)
  76:                  Dim ListPhone As New List(Of Phone)
  77:                  For Each One In PT
  78:                      If Not String.IsNullOrWhiteSpace(One) Then
  79:                          ListPhone.Add(New Phone With {.Number = One, .Type = PhoneType1})
  80:                      End If
  81:                  Next
  82:   
  83:                  Dim MailType1 As eMailType = db1.eMailTypes.Where(Function(a) a.Id = eMailTypeEnum.Business).FirstOrDefault
  84:                  Dim MT As String() = MailsTextBox.Text.Split(vbCrLf)
  85:                  Dim ListMail As New List(Of eMail)
  86:                  For Each One In MT
  87:                      If Not String.IsNullOrWhiteSpace(One) Then
  88:                          Try
  89:                              Dim M As New Net.Mail.MailAddress(One)
  90:                          Catch ex As FormatException
  91:                              MsgBox(One & vbCrLf & "is not valis eMail")
  92:                              Exit Sub
  93:                          End Try
  94:                          ListMail.Add(New eMail With {.Mail = One})
  95:                      End If
  96:                  Next
  97:   
  98:                  Dim TT = db1.StatusTypes.Where(Function(A) A.Name = StatusComboBox.Text).FirstOrDefault
  99:   
 100:                  .PersonID = Guid.NewGuid
 101:                  .CrDate = Now
 102:                  .Type = ListContactType
 103:                  .ToStatusType = TT
 104:                  .NameFirst = FirstNameTextBox.Text
 105:                  .NameMiddle = MiddleTextBox.Text
 106:                  .NameLast = LastNameTextBox.Text
 107:                  .Title = TitleTextBox.Text
 108:                  .Company = CompanyTextBox.Text
 109:                  .Phones = ListPhone
 110:                  .Mails = ListMail
 111:                  .Sites = ListWebSite
 112:                  .AssignedTo = AssignedToTextBox.Text
 113:                  .SourceCompany = SourceCompanyTextBox.Text
 114:                  .SourceContact = SourceContactTextBox.Text
 115:                  .AdditionalInfo = AdditionalInfoTextBox.Text
 116:                  .Permission = ListPermission
 117:              End With
 118:              db1.Persons.Add(X)
 119:              db1.SaveChanges()
 120:   
 121:              AddPersonStatusLabel.Text = "Person """ & FirstNameTextBox.Text & " " & MiddleTextBox.Text & " " & LastNameTextBox.Text & """ added, ID=" & Guid.NewGuid.ToString
 122:              ToolStrip1.Refresh()
 123:   
 124:              CampaignsComboBox.Text = ""
 125:              FirstNameTextBox.Text = ""
 126:              MiddleTextBox.Text = ""
 127:              LastNameTextBox.Text = ""
 128:              TitleTextBox.Text = ""
 129:              CompanyTextBox.Text = ""
 130:              AdditionalInfoTextBox.Text = ""
 131:              PhonesTextBox.Text = ""
 132:              MailsTextBox.Text = ""
 133:              SiteTextBox.Text = ""
 134:              AssignedToTextBox.Text = ""
 135:              SourceCompanyTextBox.Text = ""
 136:              SourceContactTextBox.Text = ""
 137:              For Each One As ListViewItem In TypeListView.Items
 138:                  One.Checked = False
 139:              Next
 140:              For Each One As ListViewItem In PermissionListView.Items
 141:                  If One.Text = "All" Then
 142:                      One.Checked = True
 143:                  Else
 144:                      One.Checked = False
 145:                  End If
 146:              Next
 147:   
 148:          Catch ex As Exception
 149:              MsgBox(ex.Message)
 150:              db1 = New FbDbContext
 151:          End Try
 152:      End Sub
 153:   
 154:      Private Sub PeoplesToolStripButton_Click(sender As Object, e As EventArgs) Handles PeoplesToolStripButton.Click
 155:          MainMDI.ContactsPeoplesForm_Open()
 156:      End Sub
 157:   
 158:      Private Sub SecurityInfoToolStripButton_Click(sender As Object, e As EventArgs) Handles SecurityInfoToolStripButton.Click
 159:          MainMDI.ContactsSecurityInfoForm_Open()
 160:      End Sub
 161:   
 162:      Private Sub CorrenspondenceToolStripButton_Click(sender As Object, e As EventArgs) Handles CorrenspondenceToolStripButton.Click
 163:          MainMDI.ContactsCorrespondenceForm_Open()
 164:      End Sub
 165:   
 166:      Private Sub QuestionsToolStripButton_Click(sender As Object, e As EventArgs) Handles QuestionsToolStripButton.Click
 167:          MainMDI.ContactsQuestionsForm_Open()
 168:      End Sub
 169:   
 170:      Private Sub PersonalDetailsToolStripButton_Click(sender As Object, e As EventArgs) Handles PersonalDetailsToolStripButton.Click
 171:          MainMDI.ContactsPersonalDetailsForm_Open()
 172:      End Sub
 173:  End Class

This is list form to read and delete.



   1:  Public Class ContactsPeoplesForm
   2:   
   3:      Dim db1 As FbDbContext
   4:      Dim Rows As List(Of Person)
   5:   
   6:      Public Enum nColumn
   7:          Status = 0
   8:          Type = 1
   9:          Name = 2
  10:          Phones = 3
  11:          Email = 4
  12:          Campaign = 5
  13:          Title = 6
  14:          Company = 7
  15:          AssignedTo = 8
  16:          Sites = 9
  17:          Edit = 10
  18:          Del = 11
  19:      End Enum
  20:   
  21:      Private Sub ContactsPeoplesForm_Load(sender As Object, e As EventArgs) Handles Me.Load
  22:          DataGridView1_RowFefresh()
  23:      End Sub
  24:   
  25:      Public Sub DataGridView1_RowFefresh()
  26:          db1 = New FbDbContext
  27:          'db1.Configuration.LazyLoadingEnabled = False
  28:          Rows = db1.Persons.Include("ToStatusType").Include("ToCampaign").ToList
  29:          DataGridView1.AutoGenerateColumns = False
  30:          DataGridView1.DataSource = Rows
  31:          DataGridView1.Refresh()
  32:      End Sub
  33:   
  34:      Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
  35:          Dim senderGrid = DirectCast(sender, DataGridView)
  36:          Dim CurCell As DataGridViewCell = senderGrid(e.ColumnIndex, e.RowIndex)
  37:          If e.ColumnIndex = nColumn.Status Then
  38:              If Rows(e.RowIndex).ToStatusType IsNot Nothing Then
  39:                  CurCell.Value = Rows(e.RowIndex).ToStatusType.Name
  40:              End If
  41:          ElseIf e.ColumnIndex = nColumn.Type Then
  42:              If Rows(e.RowIndex).Type IsNot Nothing Then
  43:                  Dim Str1 As String = ""
  44:                  For Each One As ContactType In Rows(e.RowIndex).Type
  45:                      Str1 &= One.Name & ","
  46:                  Next
  47:                  CurCell.Value = Str1.TrimEnd(New Char() {","})
  48:              End If
  49:          ElseIf e.ColumnIndex = nColumn.Name Then
  50:              CurCell.Value = Rows(e.RowIndex).NameFirst & " " & Rows(e.RowIndex).NameMiddle & " " & Rows(e.RowIndex).NameLast
  51:              CurCell.ToolTipText = Rows(e.RowIndex).AdditionalInfo
  52:          ElseIf e.ColumnIndex = nColumn.Phones Then
  53:              If Rows(e.RowIndex).Phones IsNot Nothing Then
  54:                  Dim Str1 As String = ""
  55:                  For Each One As Phone In Rows(e.RowIndex).Phones
  56:                      Str1 &= One.Number & ","
  57:                  Next
  58:                  CurCell.Value = Str1.TrimEnd(New Char() {","})
  59:              End If
  60:          ElseIf e.ColumnIndex = nColumn.Email Then
  61:              If Rows(e.RowIndex).Mails IsNot Nothing Then
  62:                  Dim Str1 As String = ""
  63:                  For Each One As eMail In Rows(e.RowIndex).Mails
  64:                      Str1 &= One.Mail & ","
  65:                  Next
  66:                  CurCell.Value = Str1.TrimEnd(New Char() {","})
  67:              End If
  68:          ElseIf e.ColumnIndex = nColumn.Campaign Then
  69:              If Rows(e.RowIndex).ToCampaign IsNot Nothing Then
  70:                  CurCell.Value = Rows(e.RowIndex).ToCampaign.Name
  71:              End If
  72:          ElseIf e.ColumnIndex = nColumn.Title Then
  73:          ElseIf e.ColumnIndex = nColumn.Company Then
  74:          ElseIf e.ColumnIndex = nColumn.AssignedTo Then
  75:          ElseIf e.ColumnIndex = nColumn.Sites Then
  76:              If Rows(e.RowIndex).Sites IsNot Nothing Then
  77:                  Dim Str1 As String = ""
  78:                  For Each One As WebSite In Rows(e.RowIndex).Sites
  79:                      Str1 &= One.URL & ","
  80:                  Next
  81:                  CurCell.Value = Str1.TrimEnd(New Char() {","})
  82:              End If
  83:          End If
  84:      End Sub
  85:   
  86:   
  87:      Private Sub AddPersonToolStripButton_Click(sender As Object, e As EventArgs) Handles AddPersonToolStripButton.Click
  88:          MainMDI.AddPersonForm_Open()
  89:      End Sub
  90:   
  91:      Private Sub SecurityInfoToolStripButton_Click(sender As Object, e As EventArgs) Handles SecurityInfoToolStripButton.Click
  92:          MainMDI.ContactsSecurityInfoForm_Open()
  93:      End Sub
  94:   
  95:      Private Sub CorrespondenceToolStripButton_Click(sender As Object, e As EventArgs) Handles CorrespondenceToolStripButton.Click
  96:          MainMDI.ContactsCorrespondenceForm_Open()
  97:      End Sub
  98:   
  99:      Private Sub QuestionsToolStripButton_Click(sender As Object, e As EventArgs) Handles QuestionsToolStripButton.Click
 100:          MainMDI.ContactsQuestionsForm_Open()
 101:      End Sub
 102:   
 103:      Private Sub PersonalDetailToolStripButton_Click(sender As Object, e As EventArgs) Handles PersonalDetailToolStripButton.Click
 104:          MainMDI.ContactsPersonalDetailsForm_Open()
 105:      End Sub
 106:   
 107:      Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
 108:          If e.RowIndex >= 0 Then
 109:              Dim senderGrid = DirectCast(sender, DataGridView)
 110:              Dim CurRow As Person = Rows(e.RowIndex)
 111:              If TypeOf senderGrid.Columns(e.ColumnIndex) Is DataGridViewImageColumn AndAlso e.ColumnIndex = nColumn.Edit Then
 112:                  MsgBox("this future under development" & "will be form like 'add person', but for edition contact")
 113:              ElseIf TypeOf senderGrid.Columns(e.ColumnIndex) Is DataGridViewImageColumn AndAlso e.ColumnIndex = nColumn.Del Then
 114:                  If MsgBox("Do you really want to delete" & CurRow.NameFirst & " " & CurRow.NameMiddle & " " & CurRow.NameLast, MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
 115:                      db1.Persons.Remove(CurRow)
 116:                      db1.SaveChanges()
 117:                      DataGridView1_RowFefresh()
 118:                  End If
 119:              End If
 120:          End If
 121:      End Sub
 122:   
 123:      Private Sub RefreshToolStripButton_Click(sender As Object, e As EventArgs) Handles RefreshToolStripButton.Click
 124:          db1 = New FbDbContext
 125:          Rows = db1.Persons.Include("ToStatusType").Include("ToCampaign").ToList
 126:          DataGridView1.AutoGenerateColumns = False
 127:          DataGridView1.DataSource = Rows
 128:          DataGridView1.Refresh()
 129:      End Sub
 130:  End Class

to be continue....

Comments ( )
Link to this page: //www.vb-net.com/FinancialBroker/Index.htm
< THANKS ME>