Click here to Skip to main content
15,896,259 members
Articles / Programming Languages / C#

LINQ to SQL - Many to Many Relationships

,
Rate me:
Please Sign up or sign in to vote.
4.82/5 (18 votes)
18 Aug 2009CPOL5 min read 88K   940   27  
Easily support many to many relationships when using LINQ to SQL.
USE master
Create Database [Sample]
GO

USE [Sample]
GO

/****** Object:  Table [dbo].[Role]    Script Date: 08/07/2009 17:24:25 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Role](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](50) NOT NULL,
	[Description] [nvarchar](150) NULL,
	[CreatedDate] [datetime] NOT NULL,
	[ModifiedDate] [datetime] NOT NULL,
	[RowVersion] [timestamp] NOT NULL,
 CONSTRAINT [PK_Role] PRIMARY KEY CLUSTERED 
(
	[Id] 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

USE [Sample]
GO

/****** Object:  Table [dbo].[User]    Script Date: 08/07/2009 17:24:25 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[User](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[EmailAddress] [nvarchar](250) NOT NULL,
	[FirstName] [nvarchar](200) NULL,
	[LastName] [nvarchar](200) NULL,
	[Avatar] [varbinary](max) NULL,
	[CreatedDate] [datetime] NOT NULL,
	[ModifiedDate] [datetime] NOT NULL,
	[RowVersion] [timestamp] NOT NULL,
	[PasswordHash] [char](86) NOT NULL,
	[PasswordSalt] [char](5) NOT NULL,
	[Comment] [text] NULL,
	[IsApproved] [bit] NOT NULL,
	[LastLoginDate] [datetime] NULL,
	[LastActivityDate] [datetime] NOT NULL,
	[LastPasswordChangeDate] [datetime] NULL,
	[AvatarType] [nvarchar](150) NULL,
 CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED 
(
	[Id] 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

SET ANSI_PADDING OFF
GO

USE [Sample]
GO

/****** Object:  Table [dbo].[UserRole]    Script Date: 08/07/2009 17:24:25 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[UserRole](
	[UserId] [int] NOT NULL,
	[RoleId] [int] NOT NULL,
 CONSTRAINT [PK_UserRole] PRIMARY KEY CLUSTERED 
(
	[UserId] ASC,
	[RoleId] 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

ALTER TABLE [dbo].[Role] ADD  DEFAULT (getdate()) FOR [CreatedDate]
GO

ALTER TABLE [dbo].[Role] ADD  DEFAULT (getdate()) FOR [ModifiedDate]
GO

ALTER TABLE [dbo].[User] ADD  CONSTRAINT [DF__User__CreatedDat__1920BF5C]  DEFAULT (getdate()) FOR [CreatedDate]
GO

ALTER TABLE [dbo].[User] ADD  CONSTRAINT [DF__User__ModifiedDa__1A14E395]  DEFAULT (getdate()) FOR [ModifiedDate]
GO

ALTER TABLE [dbo].[User] ADD  CONSTRAINT [DF__User__PasswordHa__3A81B327]  DEFAULT ('') FOR [PasswordHash]
GO

ALTER TABLE [dbo].[User] ADD  CONSTRAINT [DF__User__PasswordSa__3B75D760]  DEFAULT ('') FOR [PasswordSalt]
GO

ALTER TABLE [dbo].[User] ADD  CONSTRAINT [DF__User__IsApproved__3C69FB99]  DEFAULT ((1)) FOR [IsApproved]
GO

ALTER TABLE [dbo].[User] ADD  CONSTRAINT [DF__User__LastLoginD__3D5E1FD2]  DEFAULT (getdate()) FOR [LastLoginDate]
GO

ALTER TABLE [dbo].[User] ADD  CONSTRAINT [DF__User__LastActivi__3E52440B]  DEFAULT (getdate()) FOR [LastActivityDate]
GO

ALTER TABLE [dbo].[UserRole]  WITH NOCHECK ADD  CONSTRAINT [FK_UserRole_Role] FOREIGN KEY([RoleId])
REFERENCES [dbo].[Role] ([Id])
GO

ALTER TABLE [dbo].[UserRole] CHECK CONSTRAINT [FK_UserRole_Role]
GO

ALTER TABLE [dbo].[UserRole]  WITH NOCHECK ADD  CONSTRAINT [FK_UserRole_User] FOREIGN KEY([UserId])
REFERENCES [dbo].[User] ([Id])
GO

ALTER TABLE [dbo].[UserRole] CHECK CONSTRAINT [FK_UserRole_User]
GO


USE [Sample]
GO
Insert Into [User] (EmailAddress, FirstName, LastName, PasswordHash, PasswordSalt)
Values('hunter.cole@sample.com', 'Hunter', 'Cole', '1', '1')

USE [Sample]
GO
Insert Into [Role]([Name])
Values('Newb')

Insert Into [Role]([Name])
Values('Nobody')

Insert Into [Role]([Name])
Values('Admin')

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect CodeSmith Tools, LLC
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Written By
Architect CodeSmith Tools
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.
This is a Organisation (No members)


Comments and Discussions