Featured Article

VB Entity Framework Example Tutorial for Beginners

Kenneth Jul 13, 2026

Leveraging the Visual Basic (VB) Entity Framework (EF) is a powerful way to interact with databases in your .NET applications. It simplifies data access, enabling you to perform CRUD operations and query databases using LINQ. Let's explore VB EF with an example of creating, manipulating, and retrieving data from a simple `Blog` database.

Free Entity Framework Book
Free Entity Framework Book

Before we dive into code, ensure you've installed the EntityFramework NuGet package and set up your DB Context class. In this example, we'll use SQL Server and Entity Framework Core.

Microsoft Excel Object Model (page 1)
Microsoft Excel Object Model (page 1)

Creating Blog Entities and DbContext

Let's start by defining the Blog entity and the DbContext class.

List of VB.Net Projects with Source Code Free Download
List of VB.Net Projects with Source Code Free Download

The Blog entity might look like this:

```vb Public Class Blog Public Property Id As Integer Public Property Name As String Public Property Url As String Public Property Posts As ICollection(Of Post) End Class ```

Now, let's create the DbContext class named ApplicationDbContext:

Code First Approach in Entity Framework in Asp.net MVC with Example - Tutlane
Code First Approach in Entity Framework in Asp.net MVC with Example - Tutlane

```vb Imports Microsoft.EntityFrameworkCore Public Class ApplicationDbContext Inherits DbContext Public Property Blogs As DbSet(Of Blog) Sub New(options As DbContextOptions(Of ApplicationDbContext)) MyBase.New(options) End Sub Protected Overrides Sub OnModelCreating(modelBuilder As ModelBuilder) modelBuilder.Entity(Of Blog)() .HasKey(Function(b) b.Id) .HasMany(Function(b) b.Posts) .WithOne(Function(p) p.Blog) .HasForeignKey(Function(p) p.BlogId) End Sub End Class ```

Configuring and Initializing the DbContext

Before we start performing operations, we need to configure and initialize the DbContext in the Startup.cs class or application's entry point:

```vb Imports Microsoft.EntityFrameworkCore '... Public Sub ConfigureServices(services As IServiceCollection) '... services.AddDbContext(Of ApplicationDbContext)( _ Function(options) options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))) End Sub ```

Here we're using the SQL Server provider and setting up the connection string.

Introduction to Entity Framework Core - The Engineering Projects
Introduction to Entity Framework Core - The Engineering Projects

Migrating the Database

To create the database schema, we'll use the dotnet-ef package to create and apply migrations.

First, create a new migration:

Automated Electronic-Portfolio System using VB.NET
Automated Electronic-Portfolio System using VB.NET

``` dotnet ef migrations add InitialCreate -c ApplicationDbContext ```

Then, apply the migration to create the database and tables:

``` dotnet ef database update -c ApplicationDbContext ```

Performing CRUD Operations

Varvara Vozchikova (20102252)
Varvara Vozchikova (20102252)
How to make an InstallerSetup from your VB.Net Project?
How to make an InstallerSetup from your VB.Net Project?
FillWeight Layout DataGridView Columns using VB.Net
FillWeight Layout DataGridView Columns using VB.Net
VB.Net Project Application Settings Store and Retrieve Information
VB.Net Project Application Settings Store and Retrieve Information
Classes and Objects in VB.net – What is Class and Object in VB.net?
Classes and Objects in VB.net – What is Class and Object in VB.net?
#dotnet #entityframework #webapi #linq #dotnetcore #interviewpreparation #careergrowth #csharp #backenddevelopment | Shaheen Aziz
#dotnet #entityframework #webapi #linq #dotnetcore #interviewpreparation #careergrowth #csharp #backenddevelopment | Shaheen Aziz
VB.NET with MS Access: Creating a Multi-User Role-Based Login System
VB.NET with MS Access: Creating a Multi-User Role-Based Login System
the inside every vcc term sheet is shown in this graphic, it shows how to use
the inside every vcc term sheet is shown in this graphic, it shows how to use
What is a UserForm in VBA? - My Blog
What is a UserForm in VBA? - My Blog

Now that our DbContext and database are set up, let's perform some CRUD operations using LINQ.

Creating a new Blog

To create a new blog, inject the ApplicationDbContext into your controller or service and use the Add method on the Blogs DbSet:

```vb Public Async Function CreateBlog(blog As Blog) As Task(Of IActionResult) _context.Blogs.Add(blog) Await _context.SaveChangesAsync() Return CreatedAtRoute("GetBlog", New With {.id = blog.Id}, blog) End Function ```

Retrieving all Blogs

To retrieve all blogs, use the AsEnumerable method on the Blogs DbSet:

```vb Public Async Function GetAllBlogs() As Task(Of IEnumerable(Of Blog)) Return Await _context.Blogs.ToListAsync() End Function ```

Updating a Blog's Name

To update a blog's name, retrieve the blog from the database, update its property, and call SaveChangesAsync:

```vb Public Async Function UpdateBlog(id As Integer, newName As String) As Task(Of IActionResult) Dim blog = Await _context.Blogs.FindAsync(id) If blog IsNot Nothing Then blog.Name = newName Await _context.SaveChangesAsync() Return NoContent() Else Return NotFound() End If End Function ```

Deleting a Blog

To delete a blog, retrieve it from the database, remove it from the DbSet, and call SaveChangesAsync:

```vb Public Async Function DeleteBlog(id As Integer) As Task(Of IActionResult) Dim blog = Await _context.Blogs.FindAsync(id) If blog IsNot Nothing Then _context.Blogs.Remove(blog) Await _context.SaveChangesAsync() Return NoContent() Else Return NotFound() End If End Function ```

With these examples, you've seen how to use VB Entity Framework Core to perform common database operations. Now, you can extend this knowledge to create, read, update, and delete entities in more complex applications.

Happy coding, and remember to explore more features of Entity Framework, such as optimization techniques,change tracking, and lazy loading, to improve your application's performance and maintainability.