Posts

Showing posts with the label Dapper ORM

Donate

Using Unity IoC Container Dependency Injection, Dapper ORM and SQL Server In Visual Basic .NET Core Or .NET 5 Application

Image
Hi All, This article is the VB.NET version of this post Getting Started With Unity IoC Container Dependency Injection In .NET Core Using Entity Framework Core ORM And SQL Server . We will create a .NET Core Console Application that will utilize the Unity IoC Container for Dependency Injection, SQL Server and Dapper ORM. Instead of using Entity Framework, I'm using Dapper ORM coz EF does not support reverse engineering in .NET Core. There are existing power tools or other solutions on how to reverse engineer a database table but for now I'll just use Dapper. I'll be using Microsoft's sample database called ContosoRetailsDw as the datasource of this tutorial. Project Setup 1. Start off by creating a Visual Basic .NET Core Console Application targetting the latest framework which is .NET 5.0 2. Add three new folders Models, Repository and UnitOfWork. These folders contain the model and DAL classes used in this project. 3. Add the following NuGet packages: Dapper O...

Getting Started With ASP.NET Core 5.0 MVC Web Application Using Dapper ORM And SQL Server

Image
Hi All, In this tutorial, I will demonstrate on how to create an ASP.NET Core 5.0 MVC CRUD Web Application using Dapper ORM which is considered as the King of Micro ORM and an alternative to both Entity Framework and RepoDB ORM. I have published ASP.NET MVC and ASP.NET Webforms articles before using Dapper ORM in this blog and have used this ORM in an internal application before. With the birth of .NET Core framework, it's time to join the bandwagon of promoting these type of tools that helped maximize the productivity of the developers by creating articles as guide or reference. Enough with the chitchat and lets start coding. :-) I. Project Setup 1. Create a Customer table in a SQL Server database using the script below. USE [Your_Database] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Customer]( [CustomerID] [numeric](18, 0) IDENTITY (1,1) NOT NULL , [CompanyName] [varchar](40) NULL , [Address] [varchar](35)...

ASP.NET MVC CRUD(Create/Update/Delete) With Dapper ORM in VB.NET

Image
Konnichiwa, Here's a VB.NET ASP.NET MVC CRUD project using Dapper ORM which is the counterpart of this post ASP.NET MVC with Dapper ORM . The only file in the Models folder that has updates is Customer.cs class such as applying Display and DisplayAttributes. Imports System.ComponentModel.DataAnnotations Public Class Customer Public Property CustomerID() As Integer Get Return m_CustomerID End Get Set (value As Integer ) m_CustomerID = value End Set End Property Private m_CustomerID As Integer <Display(Name:= "Company Name" )> Public Property CompanyName() As String Get Return m_CompanyName End Get Set (value As String ) m_CompanyName = value End Set End Property Private m_CompanyName As String <Display(Name:= "Address" )> Public Property Address() As String ...

ASP.NET MVC CRUD(Create/Update/Delete) With Dapper ORM

Image
Hi All, Here's an ASP.NET MVC CRUD(Create/Update/Delete) project with Dapper ORM. The model classes and interface are based from this post Using Dapper ORM in ASP.NET Web Forms with few modifications in Customer class. The updates are adding Display attribute and DisplayFormat attribute to CreditLimit and IntroDate properties. using System; using System.ComponentModel.DataAnnotations; namespace ASPMVCDapper.Models { public class Customer { public int CustomerID { get ; set ; } [Display(Name="Company Name")] public string CompanyName { get ; set ; } [Display(Name = "Address")] public string Address { get ; set ; } [Display(Name = "City")] public string City { get ; set ; } [Display(Name = "State")] public string State { get ; set ; } [Display(Name = "Intro Date")] [DisplayFormat(DataFormatString = "{0:yyyy-MM...

Unable to cast object of type 'DapperRow' to return Type

Aloha, Given the following code which generates an exception "Unable to cast object of type 'DapperRow", the cause for this is that the return type of FirstOrDefault() dynamic. public Customer FindById( int Id) { return this ._db.Query( "SELECT * FROM Customer WHERE CustomerID=@Id" , new { Id = Id }).FirstOrDefault(); } In order to solve this error, you have several options. One is to use Query.<TReturn>() instead of Query() wherein you can explicity specify the type. public Customer FindById(int Id) { return this._db.Query<Customer>( "SELECT * FROM Customer WHERE CustomerID=@Id" , new { Id = Id }).FirstOrDefault(); } Another option is to modify the code with issue, that is to store the result of the query in a dynamic variable and then assign the dynamic properties value to the class properties. public Customer FindById( int Id) { dynamic customerRecord = this ._db.Query( "SELECT * FROM Customer WHERE CustomerID=@Id...

Using Dapper ORM in ASP.NET Web Forms (Visual Basic.NET)

Image
Hi, This is a conversion of this post Using Dapper ORM in ASP.NET WebForm to VB.NET language. Customer.vb Public Class Customer Public Property CustomerID() As Integer Get Return m_CustomerID End Get Set (value As Integer ) m_CustomerID = Value End Set End Property Private m_CustomerID As Integer Public Property CompanyName() As String Get Return m_CompanyName End Get Set (value As String ) m_CompanyName = Value End Set End Property Private m_CompanyName As String Public Property Address() As String Get Return m_Address End Get Set (value As String ) m_Address = Value End Set End Property Private m_Address As String Public Property City() As String Get Return m_City End Get Set (...

Donate