To start working on NHibernate you have to download the assemblies for NHibernate. The binaries can be downloaded from source forge. I have used the 2.1.2 alpha version for creating the sample application. We would also require the Linq-2.1.2 binaries for working with LINQ to NHibernate samples.
Creating the DB
In this article I’m going to demonstrate the features of NHibernate by creating a Customer-Order application. First we need to create the DB and the respective tables for the sample. I have used SQL server express edition as the DB server for the application. Create a new database - NHibernateSampleDB and execute the below given scripts to create the tables required in our sample app.
USE [NHibernateSampleDB]
CREATE TABLE [dbo].[Customers](
[CustomerIntId] [bigint] IDENTITY(1,1) NOT NULL,
[Version] [timestamp] NULL,
[CreatedDate] [datetime] NULL,
[ChangedDate] [datetime] NULL,
[FirstName] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[LastName] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Address] [nvarchar](150) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
PRIMARY KEY CLUSTERED
(
[CustomerIntId] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[Products](
[ProductIntId] [bigint] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Amount] [decimal](10, 2) NOT NULL,
[Model] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[CreatedDate] [datetime] NOT NULL,
[ChangedDate] [datetime] NOT NULL,
[Version] [timestamp] NOT NULL,
CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED
(
[ProductIntId] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[Orders](
[OrderIntId] [bigint] IDENTITY(1,1) NOT NULL,
[OrderNumber] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[CustomerIntId] [bigint] NOT NULL,
[CreatedDate] [datetime] NOT NULL,
[ChangedDate] [datetime] NOT NULL,
[Version] [timestamp] NOT NULL,
CONSTRAINT [PK_Orders] PRIMARY KEY CLUSTERED
(
[OrderIntId] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[Orders] WITH CHECK ADD CONSTRAINT [FK_Orders_Customers] FOREIGN KEY([CustomerIntId])
REFERENCES [dbo].[Customers] ([CustomerIntId])
GO
ALTER TABLE [dbo].[Orders] CHECK CONSTRAINT [FK_Orders_Customers]
CREATE TABLE [dbo].[OrderProduct](
[OrderProductIntId] [bigint] IDENTITY(1,1) NOT NULL,
[OrderIntId] [bigint] NOT NULL,
[ProductIntId] [bigint] NOT NULL,
CONSTRAINT [PK_OrderProduct] PRIMARY KEY CLUSTERED
(
[OrderProductIntId] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[OrderProduct] WITH CHECK ADD CONSTRAINT [FK_OrderProduct_Orders] FOREIGN KEY([OrderIntId])
REFERENCES [dbo].[Orders] ([OrderIntId])
GO
ALTER TABLE [dbo].[OrderProduct] CHECK CONSTRAINT [FK_OrderProduct_Orders]
GO
ALTER TABLE [dbo].[OrderProduct] WITH CHECK ADD CONSTRAINT [FK_OrderProduct_Products] FOREIGN KEY([ProductIntId])
REFERENCES [dbo].[Products] ([ProductIntId])
GO
ALTER TABLE [dbo].[OrderProduct] CHECK CONSTRAINT [FK_OrderProduct_Products]
After successful execution of these scripts your database should look like.
In the next part of the series we’ll start working on the C# code for the application and see how to configure NHibernate for accessing the database we have created.
No comments:
Post a Comment