"Server=.;Database=MyLittleSqlDatabase;Trusted_Connection=True"
•
Server=. means the database server it's connecting to is the default SQL Server "instance" (the windows process) running on your computer (the default one is always called MSSQLSERVER). If you've already installed it and used a different name than MSSQLSERVER (SQL Management Studio will tell you the name at the top), then it will be ".\Name" instead. For example, if you installed SQL Express then I you'll have ended up with one called ".\SQLEXPRESS" instead.
•
Database=MyLittleSqlDatabase is (you probably guessed...) the name of the database running inside the SQL Server Instance, Try it with a database name which doesn't exist yet, or it'll may throw an error. When the database doesn't exist, Entity Framework will always create it.
•
Trusted_Connection=True means it'll connect as your Windows User which should already have full admin permission -- i.e. when you're running the program, it'll use your windows login automatically.
1.
Create a New VB.NET Project in Visual Studio (Console Project just to make this super quick and easy, but a Windows Forms project uses exactly the same code)
2.
In Visual Studio, go to the 'Tools' Menu at the top. Navigate to 'NuGet Package Manager'. choose 'Manage NuGet Packages for Solution'
3.
A screen called NuGet Package Manager will appear. There's a tab at the top called Browse. Search for Entity Framework -- click on it. click Install.
' This is just a normal VB class, If you feed it to Entity Framework it will
' connect the class to a database table
Public Class Department
' Entity Framework assumes that Id is your primary key
Property Id As Integer
' These are just plain old data fields....
Property Name As String
Property Manager As String
' Entity Framework assumes that ICollection is a one-to-many relationship.
' Employee class has a foreign key "DepartmentId" Integer for this to work!
Property Employees As ICollection(Of Employee) = New List(Of Employee)
End Class
Public Class Employee
' Entity Framework will assume that this is the primary key to the Employee table
Property Id As Integer
' Plain old data fields in the database
Property Name As String
' a questionmark is useful because DateTime normally doesn't allow nulls.
' But the questionmark means "Nullable DateTime" in the database.
Property Dob As DateTime?
' Entity Framework will assume that this is the foreign key to the Department Table
Property DepartmentId As Integer
End Class
Imports System.Data.Entity
' This is the class which derives from Entity Framework to create a database
'
' Get used to manually deleting this database from SQL Server because every time
' you change one of the classes you will need to delete the whole database and let
' Entity Framework create it again fresh.
' Otherwise it complains about a "model context change".
Public Class MyDbContext
' DbContext contains loads of EntityFramework code that connects to the DB,
' and reflects the data schema into VB.
Inherits DbContext
' The Constructor. If the database doesn't exist when this runs,
' Entity Framework will create it
Public Sub New()
' This is the SQL Server connection string for MyLittleDatabase
MyBase.New("Server=.;Database=MyLittleDatabsae;Trusted_Connection=True")
End Sub
' Entity Framework looks at every "DbSet" and turns those into a table
Property Employees As DbSet(Of Employee)
Property Departments As DbSet(Of Department)
End Class
Module Module1
Sub Main()
' Connects to the database and maybe creates the database if it doesn't exist.
' It's fine to keep connecting/closing over and over.
' Any data-related stuff needs to happen when the connection is open.
' Not after it closes.
Using db As New MyDbContext
' Create a couple of simple VB objects -- a new employee and a department
Dim emp As Employee = New Employee
Dim dept As Department = New Department
' Adds the employee to the department. These are still just VB objects
dept.Employees.Add(emp)
' Adds the department to Entity framework's "cache" for the Departments table
db.Departments.Add(dept)
' Saves (commits) the Entity Framework changes into the database
' i.e. creates the Employee and Department.
db.SaveChanges()
' Now check the database in SQL management studio
' it will have 2 tables called Employee and Department.
' Each with one row in them
' "End Using" Closes the DB connection. Like the end of a Transaction.
' Everything with the data (read or modify) needs to happen before this.
End Using
End Sub
End Module
•
Having an Integer Property called "Id" will magically be the primary key for that Class' corresponding table.
•
Having another Integer Property like "TableNameId" will magically be a foreign key out to another Table/Class called TableName.
•
Having a list Property based on ICollection(Of SomeTable) will assume a one-to-many to the Class/Table "SomeTable". (Needs a foreign key integer property on the other table...)
Last reply 3 days ago
Is MathsFM French Alevels good for a CS degree at top uni (OX, UCL, Durham etc)Last reply 4 days ago
OCR A-level Computer Science Paper 1 (H446/01) - 12th June 2023 [Exam Chat]Last reply 2 weeks ago
OCR A-level Computer Science Paper 2 (H446/02) - 19th June 2023 [Exam Chat]Last reply 3 weeks ago
AQA A-level Computer Science Paper 1 (7517 1A - 1E) - 12th June 2023 [Exam Chat]Last reply 1 month ago
Not receiving any response from University of Birmingham.Last reply 3 months ago
OCR GCSE Computer Science Paper 1 (J277/01) - 19th May 2023 [Exam Chat]Last reply 3 months ago
AQA GCSE Computer Science Paper 2 (8525/2) - 25th May 2023 [Exam Chat]Last reply 3 days ago
Is MathsFM French Alevels good for a CS degree at top uni (OX, UCL, Durham etc)Last reply 4 days ago
OCR A-level Computer Science Paper 1 (H446/01) - 12th June 2023 [Exam Chat]Last reply 2 weeks ago
OCR A-level Computer Science Paper 2 (H446/02) - 19th June 2023 [Exam Chat]Last reply 3 weeks ago
AQA A-level Computer Science Paper 1 (7517 1A - 1E) - 12th June 2023 [Exam Chat]Last reply 1 month ago
Not receiving any response from University of Birmingham.Last reply 3 months ago
OCR GCSE Computer Science Paper 1 (J277/01) - 19th May 2023 [Exam Chat]Last reply 3 months ago
AQA GCSE Computer Science Paper 2 (8525/2) - 25th May 2023 [Exam Chat]