Let’s first establish the purpose of the code.
For this article, the purpose of the code is how to structure your MVC code with repository and unity of work pattern, using Entity Framework.
What is repository and unity of work ?
The repository and unity of work pattern are used to create an abstraction layer between the data access layer and the business logic layer of an application.
For this article, I have the following two tables.
Book Table
- BookID -- bigint -- Primary Key
- BookName -- nvarchar(MAX)
- Descrption -- nvarchar(MAX)
- Price -- decimal(10, 2)
- AutherID -- bigint -- Foregine Key With Auther Table
- AutherID -- bigint -- Primary Key
- AutherName -- nvarchar(50)
The below script is used for generating the above tables.
- USE[DemoUnityRepository]
- GO
- /****** Object: Table [dbo].[Auther] Script Date: 11-01-2017 12:25:22 PM ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- CREATE TABLE[dbo].[Auther](
- [AutherID][bigint] IDENTITY(1, 1) NOT NULL, [AutherName][nvarchar](50) NOT NULL, CONSTRAINT[PK_Auther] PRIMARY KEY CLUSTERED(
- [AutherID] 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
- /****** Object: Table [dbo].[Book] Script Date: 11-01-2017 12:25:22 PM ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- CREATE TABLE[dbo].[Book](
- [BookID][bigint] IDENTITY(1, 1) NOT NULL, [BookName][nvarchar](max) NOT NULL, [Descrption][nvarchar](max) NOT NULL, [Price][decimal](10, 2) NOT NULL, [AutherID][bigint] NOT NULL, [TagID][bigint] NOT NULL, CONSTRAINT[PK_Book] PRIMARY KEY CLUSTERED(
- [BookID] ASC) WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY]) ON[PRIMARY] TEXTIMAGE_ON[PRIMARY]
- GO
- ALTER TABLE[dbo].[Book] WITH CHECK ADD CONSTRAINT[FK_Book_Auther] FOREIGN KEY([AutherID])
- REFERENCES[dbo].[Auther]([AutherID])
- GO
- ALTER TABLE[dbo].[Book] CHECK CONSTRAINT[FK_Book_Auther]
- GO
- ALTER TABLE[dbo].[Book] WITH CHECK ADD CONSTRAINT[FK_Book_Tag] FOREIGN KEY([TagID])
- REFERENCES[dbo].[Tag]([TagID])
- GO
- ALTER TABLE[dbo].[Book] CHECK CONSTRAINT[FK_Book_Tag]
- GO
Step 1 - Create an new MVC Application
- On the File menu, click New >> Project.
- In the "New Project" dialog box under Project types, expand Visual C# >> Web and in the Name box, type "DemoUnityRepository" followed by OK.
- Now, in the dialog box, click on the "MVC" under the ASP.NET 4.5.2 Templates. Then, click on "Change Authentication" which stays on center of the right side and finally, select "No Authentication" and click on OK.
- Add two folders IRepository and Repository.
- IRepository Folder -- for classes that contain interfaces.
- Repository Folder -- for classes that contain interfaces implementation.
Step 2 - Now, it's code time.
Add EDMX
- On the right side, you can find the Solution Explorer.
- In there, right click on "Models" Folder. Then, click on Add >> New Item.
- Now, click on the Visual C# and Select ADO.NET Entity Data Model, Name it "DBModel", and click on "OK".
- Select EF Designer from Database and click on "Next".
- Now, click on new connection, define "Server name", select authentication mode either Windows or SQL Server ( if SQL Server, then enter the username or password) and finally, select database "DemoUnityRepository" under "Connect to a Database". Click OK.
- Now, declare a name of connection string as "DBEntities" under "Save connection" setting in Web.config, then click on Next.
- Select version of Entity Framework. Select "Entity Framework 6.x" then click on "Next".
- Select "Table". Now, give the name space as "Models" under Model Namespace: and then click on OK.
- Now, Build your project by pressing CTRL + B for updating every entity perfectly.
STEP 3 - Implement Code


Ankit KanojiaPosted Mar 12, 2020, 2:04 AM
Thank you so much Humayun Kabir Mamun
Ankit KanojiaPosted Mar 12, 2020, 2:03 AM
Thank you so much Geo J Thachankary
Ankit KanojiaPosted Mar 12, 2020, 2:03 AM
Thank you so much Joe Wilson
Ankit KanojiaPosted Mar 12, 2020, 2:03 AM
Thank you so much Manish Tewary
Ankit KanojiaPosted Mar 12, 2020, 2:03 AM
Thank you so much Deepan
DeepanPosted Aug 5, 2019, 2:45 AM
Thanks for sharing
Manish TewaryPosted Apr 10, 2019, 1:11 PM
What about AddRange method? can you give a short example?
Joe WilsonPosted Jan 15, 2017, 6:29 AM
Well, Thank you for sharing.
Geo J ThachankaryPosted Jan 15, 2017, 5:58 AM
Nice Article. A small correction though. The pattern is called 'Unit of Work' and not 'Unity of Work'.
Humayun Kabir MamunPosted Jan 15, 2017, 1:14 AM
Thanks for this nice article...
Manav PandyaPosted Jan 14, 2017, 8:59 AM
Yehh thanks for sharing this sir Suchit Khunt