In the previous article “Repository and UnitOfWork Pattern - Part Two”, we implemented the following concepts.
- Implementation of Repository pattern example using UnitOfWork Pattern. UnitOfWork (UOW), is the common pattern that is used to resolve data concurrency issues which arise when each repository implements and maintains separate Data context object. The UnitOfWork (UOW) pattern implementation manages in-memory database operations on entities as one transaction. So, if one of the operations is failing then entire database operations will be rolled back.
- Designed a database model as shown below. This database contains two tables, “Order” and “OrderItem”. Table “Order” contains column “OrderId” of type varchar and defined as primary key and non-identity column ( Non-identity column specifies that value should be supplied from the application/user). Create a foreign key relationship between primary key column “OrderId” of table “Order” and column “OrderId” of table “OrderItem” which means value of “OrderItem.OrderId” should match the value of “Order.OrderId

- Always generating the new “Order.OrderId” using Guid.NewGuid().ToString() and passing the same value to OrderItem.OrderId
- Making the entries to both tables “Order” and “OrderItem” in a single transaction. In case, any transaction fails then we rollback the entries from both the tables
Now a question arises if the “Order.OrderId” is set to Identity Column (Identity column of a table is a column whose value increases automatically. The value in an identity column is created by the database server) in database then how will auto generated value of “Order.OrderId” be retrieved and passed to column “OrderItem.OrderId”, and still have the execution of these statements happen within a single transaction.
Now, let’s have a look at the implementation of Repository pattern example using UnitOfWork Pattern and Primary key (identity column).
Database Model Changes
- Change the type of “Order.OrderId” from varchar to int
- Add column “OrderItem.ItemId” of type int
- Add unique constraint on column “OrderItem.OrderId” and “OrderItem.ItemId” to ensure that order must contain one entry of each unique item

Database Script
- -- --------------------------------------------------
- -- Entity Designer DDL Script for SQL Server 2005, 2008, 2012 and Azure
- -- --------------------------------------------------
- -- Date Created: 08/24/2019 19:18:08
- -- Generated from EDMX file: C:\Personal Data\Training\RepositoryPatternExample\RepositoryPatternExample\DataModel\OrderManagementModel.edmx
- -- --------------------------------------------------
- SET QUOTED_IDENTIFIER OFF;
- GO
- USE [OrderManagement];
- GO
- IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
- GO
- -- --------------------------------------------------
- -- Dropping and create Table [dbo].[Orders]
- -- --------------------------------------------------
- /****** Object: Table [dbo].[Orders] Script Date: 08/24/2019 19:20:39 ******/
- IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Orders]') AND type in (N'U'))
- DROP TABLE [dbo].[Orders]
- GO
- /****** Object: Table [dbo].[Orders] Script Date: 08/24/2019 19:20:39 ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- CREATE TABLE [dbo].[Orders](
- [OrderId] [int] IDENTITY(1,1) NOT NULL,
- [OrderDate] [datetime] NOT NULL,
- [OrderStatus] [nvarchar](50) NOT NULL,
- CONSTRAINT [PK_Orders] PRIMARY KEY CLUSTERED
- (
- [OrderId] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- GO
- SET IDENTITY_INSERT [dbo].[Orders] ON
- GO
- -- --------------------------------------------------
- -- Dropping existing FOREIGN KEY constraints
- -- --------------------------------------------------
- IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_OrderItems_Orders]') AND parent_object_id = OBJECT_ID(N'[dbo].[OrderItems]'))
- ALTER TABLE [dbo].[OrderItems] DROP CONSTRAINT [FK_OrderItems_Orders]
- GO
- -- --------------------------------------------------
- -- Dropping and create Table [dbo].[OrderItems]
- -- --------------------------------------------------
- /****** Object: Table [dbo].[OrderItems] Script Date: 08/24/2019 19:22:18 ******/
- IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[OrderItems]') AND type in (N'U'))
- DROP TABLE [dbo].[OrderItems]
- GO
- CREATE TABLE [dbo].[OrderItems](
- [OrderItemId] [varchar](50) NOT NULL,
- [OrderId] [int] NOT NULL,
- [ItemName] [varchar](50) NOT NULL,
- [Quantity] [int] NOT NULL,
- [ItemId] [int] NOT NULL,
- CONSTRAINT [PK_OrderItems] PRIMARY KEY CLUSTERED
- (
- [OrderItemId] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- GO
- SET ANSI_PADDING OFF
- GO
- -- --------------------------------------------------
- -- Create FOREIGN KEY constraints
- -- --------------------------------------------------
- ALTER TABLE [dbo].[OrderItems] WITH CHECK ADD CONSTRAINT [FK_OrderItems_Orders] FOREIGN KEY([OrderId])
- REFERENCES [dbo].[Orders] ([OrderId])
- GO
- ALTER TABLE [dbo].[OrderItems] CHECK CONSTRAINT [FK_OrderItems_Orders]
- GO
- USE [OrderManagement]
- GO
- /****** Object: Index [IX_UQ_OrderItems] Script Date: 08/24/2019 19:32:47 ******/
- IF EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[OrderItems]') AND name = N'IX_UQ_OrderItems')
- DROP INDEX [IX_UQ_OrderItems] ON [dbo].[OrderItems] WITH ( ONLINE = OFF )
- GO
- USE [OrderManagement]
- GO
- /****** Object: Index [IX_UQ_OrderItems] Script Date: 08/24/2019 19:32:47 ******/
- CREATE NONCLUSTERED INDEX [IX_UQ_OrderItems] ON [dbo].[OrderItems]
- (
- [OrderId] ASC,
- [ItemId] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- GO
- -- --------------------------------------------------
- -- Script has ended
- -- --------------------------------------------------







Join the conversation! Your thoughts help the community grow.