Issue with linq2sql with this specific stituation - linq-to-sql

Software used:
Visual studio 2008 professional with services pack 1
Sql Server 2005 Standard Edition (9.00.4266.00)
Windows XP SP3
I have these 3 tables:
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Table_2](
[table2id] [int] IDENTITY(1,1) NOT NULL,
[table2filler] [varchar](max) NULL,
CONSTRAINT [PK_Table_2] PRIMARY KEY CLUSTERED
(
[table2id] 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Table_1](
[table1id] [int] IDENTITY(1,1) NOT NULL,
[table1guid] [uniqueidentifier] NOT NULL,
CONSTRAINT [PK_Table_1] PRIMARY KEY CLUSTERED
(
[table1id] 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
CREATE UNIQUE NONCLUSTERED INDEX [IX_Table_1] ON [dbo].[Table_1]
(
[table1guid] 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Table_3](
[tableguid] [uniqueidentifier] NOT NULL,
[table2id] [int] NOT NULL,
[table3filler] [varchar](max) NULL,
CONSTRAINT [PK_Table_3] PRIMARY KEY CLUSTERED
(
[tableguid] ASC,
[table2id] 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
ALTER TABLE [dbo].[Table_3] WITH CHECK ADD CONSTRAINT [FK_Table_3_Table_1] FOREIGN KEY([tableguid])
REFERENCES [dbo].[Table_1] ([table1guid])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Table_3] CHECK CONSTRAINT [FK_Table_3_Table_1]
GO
ALTER TABLE [dbo].[Table_3] WITH CHECK ADD CONSTRAINT [FK_Table_3_Table_2] FOREIGN KEY([table2id])
REFERENCES [dbo].[Table_2] ([table2id])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Table_3] CHECK CONSTRAINT [FK_Table_3_Table_2]
GO
INSERT INTO [dbo].[Table_2]
([table2filler])
VALUES
('test')
print 'table2id:'
print scope_identity()
GO
declare #guid uniqueidentifier
set #guid=newid()
print 'table1guid:'
print #guid
INSERT INTO [dbo].[Table_1]
([table1guid])
VALUES
(#guid)
GO
now open a new web apps project, create a new dbml and drag&drop these 3 tables
now just put that code in a webpage codebehind
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim test As New Table_3
Dim db As New DataClasses1DataContext
test.table2id = 1
test.tableguid = New Guid("guid from table 1")
test.table3filler = "a"
db.Table_3s.InsertOnSubmit(test)
db.SubmitChanges()
End Sub
and run it
you will get an invalid cast error
only way so far for me to be able to run that code is to remove the link between the table inside the DBML
is there a way to do that insert without removing the link between the tables?

I actually created your database just as you specified and ran exactly this code it locally on my box. I get no such error when I substitute the a real GUID in this line:
test.tableguid = New Guid("guid from table 1")
Are you sure your GUIDs are in the right format? Are you sure your tables are created exactly like you specified? Double check it... My guess is that if you recreate this sample db from scratch, you won't see this problem.

I believe Linq2sql doesn't like it when you set a foreign key id directly. It prefers you to set the foreign object itself.
test.table_2 = db.Table_2.First(t2 => t2.table2id = 1);
test.tableguid = New Guid("guid from table 1")
test.table3filler = "a"

ok, it's in fact a bug with .net 3.5 and fixed with .net 4.0
but there is a hotfix, see detail here
everything work like it should after that hotfix is installed

Related

F# LINQ to SQL: System.NotSupportedException: Query operator 'AsQueryable' not supported

I'm trying to translate a LINQ to SQL from C# to F#. This is what I've come up with:
let get (roles : List<string>) =
query {
for t in db.Tabs do
join r in db.Regions on (t.Id = r.FkTab)
join l in db.Links on (r.Id = l.FkRegion)
where (roles.Contains(l.Role) || roles.Contains("Administrator"))
groupValBy (t,r,l) (t.Id, t.Label) into tt
select {
Label = snd tt.Key;
Regions = query {
for xr in db.Regions do
join xl in db.Links on (xr.Id = xl.FkRegion)
where (xr.FkTab.Equals (fst tt.Key) && (roles.Contains(xl.Role) || roles.Contains("Administrator")))
groupValBy (xr, xl) (xr.Id, xr.Label) into rr
select {
Label = snd rr.Key;
Links = query {
for yl in db.Links do
where (yl.FkRegion.Equals (fst rr.Key) && (roles.Contains(yl.Role) || roles.Contains("Administrator")))
select {
Label = yl.Label;
Url = yl.Url;
Role = yl.Role
}
} |> Seq.toList
}
} |> Seq.toList
}
} |> Seq.toList
Instead of classes I've used records, here they are:
type Link = {Url : string; Role : string; Label : string}
type Region = {Label : string; Links : List<Link>}
type Tab = {Label : string; Regions : List<Region> }
Unfortunately I'm getting an exception at runtime:
A first chance exception of type 'System.NotSupportedException' occurred in System.Data.Linq.dll
The message is: System.NotSupportedException: Query operator 'AsQueryable' not supported.
I'm very green to F#, what am I missing? Perhaps I cannot use records in LINQ to SQL? I cannot nest in that manner?
Thanks
EDIT:
db is the database context:
let db = SqlConnection.GetDataContext()
If you wish to try this at home, here is the script to create the three tables needed:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Links](
[id] [int] IDENTITY(1,1) NOT NULL,
[fkRegion] [int] NOT NULL,
[label] [varchar](50) NOT NULL,
[role] [varchar](50) NOT NULL,
[url] [varchar](100) NOT NULL,
CONSTRAINT [PK_Links] 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
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[Regions] Script Date: 22/08/2015 12:06:49 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Regions](
[id] [int] IDENTITY(1,1) NOT NULL,
[fkTab] [int] NOT NULL,
[label] [varchar](50) NOT NULL,
CONSTRAINT [PK_Regions] 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
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[Tabs] Script Date: 22/08/2015 12:06:49 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Tabs](
[id] [int] IDENTITY(1,1) NOT NULL,
[label] [varchar](50) NOT NULL,
CONSTRAINT [PK_Tabs] 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
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[Links] WITH CHECK ADD CONSTRAINT [FK_Links_Regions] FOREIGN KEY([fkRegion])
REFERENCES [dbo].[Regions] ([id])
GO
ALTER TABLE [dbo].[Links] CHECK CONSTRAINT [FK_Links_Regions]
GO
ALTER TABLE [dbo].[Regions] WITH CHECK ADD CONSTRAINT [FK_Regions_Tabs] FOREIGN KEY([fkTab])
REFERENCES [dbo].[Tabs] ([id])
GO
ALTER TABLE [dbo].[Regions] CHECK CONSTRAINT [FK_Regions_Tabs]
GO
EDIT2:
The issue seems to be with the result of the query. I tried changing the record type to IEnumerable and removing the Seq.toList. However, whenever I try any operation on the enumerable, the same exception pops up, e.g. Model.Count() (where Model is an IEnumerable). Help?

Add new columns while generating scripts in sql server 2008

I' trying to generate the scripts for ma DB in Sql Server 2008.. and i'm able to do that, the scripts generated is :
USE [Cab_Booking]
GO
/****** Object: Table [dbo].[User] Script Date: 05/19/2013 10:33:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[User]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[User](
[U_Id] [int] IDENTITY(1,1) NOT NULL,
[UserName] [nvarchar](50) NOT NULL,
[Password] [nvarchar](50) NOT NULL,
add column new int not null,
CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED
(
[U_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]
END
GO
What should i do if i need to add a new column in my table through scripts...
I know this sounds easy...But, i dont know what am i missing...
thanks..
Right click on the table name on SQL Server Management Studio and select "Design". Then add a column using designer, but don't save. Right click anywhere on table designer and select "Generate Change Script". Now you have the script required to add new column to table. This method also works for removing columns, changing data types, etc.

data flow from multiple source to single destinantion in SSIS

I need to import the data from the multiple distributed database ( around 70 ) to the single source table .So how is it possible through SSIS 2008
Assuming that you can run the same query against each of the 70 source servers, you can use a ForEach Loop with a single Data Flow Task. The source connection manager's ConnectionString should be an expression using the loop variables.
Here's an example reading the INFORMATION_SCHEMA.COLUMNS view from multiple DBs. I created the following tables on my local instance:
<!-- language: lang-sql -->
CREATE TABLE [MultiDbDemo].[SourceConnections](
[DatabaseKey] [int] IDENTITY(1,1) NOT NULL,
[ServerName] [varchar](50) NOT NULL,
[DatabaseName] [varchar](50) NOT NULL,
CONSTRAINT [PK_SourceConnections] PRIMARY KEY CLUSTERED
(
[DatabaseKey] 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
CREATE TABLE [MultiDbDemo].[SourceColumns](
[ColumnKey] [int] IDENTITY(1,1) NOT NULL,
[ServerName] [varchar](50) NOT NULL,
[DatabaseName] [varchar](50) NOT NULL,
[SchemaName] [varchar](50) NOT NULL,
[TableName] [varchar](50) NOT NULL,
[ColumnName] [varchar](50) NOT NULL,
CONSTRAINT [PK_SourceColumns] PRIMARY KEY CLUSTERED
(
[ColumnKey] 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
This is the control flow for the SSIS package:
The Source_AdoDotNet connection manager's ConnectionString property is set to the following expression:
SQL_GetSourceList's SQLStatement property is SELECT ServerName, DatabaseName FROM MultiDbDemo.SourceConnections, and the ResultSet is mapped to the User::SourceList variable.
The ForEach Loop task is configured thusly:
Note that the ADO object source variable is set to the User::SourceList variable populated in the SQL_GetSourceList task.
And the data flow looks like this:
ADO_SRC_SourceInfo is configured thusly:
The next effect of all this is that, for each database listed in the SourceConnections table, we execute the query SELECT LEFT(TABLE_SCHEMA, 50) AS SchemaName, LEFT(TABLE_NAME, 50) AS TableName, LEFT(COLUMN_NAME, 50) AS ColumnName FROM INFORMATION_SCHEMA.COLUMNS and save the results in the SourceColumns table.
You will still need 70 destination components. Simply specify the same table in all of them.

Instead Of Delete Trigger with FileStream

Why can't i use an Instead Of Delete Trigger on a table that has a varbinary filestream column?
Is there a workaround for it?
I want to update a field when a delete command is performed.
Thanks
You may create two tables istead of one:
CREATE TABLE [dbo].[FileStreams](
[Id] [int] IDENTITY(1,1) NOT NULL,
[RowGuid] [uniqueidentifier] ROWGUIDCOL NOT NULL,
[Data] [varbinary](max) FILESTREAM NULL,
CONSTRAINT [PK_FileStreams] 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],
UNIQUE NONCLUSTERED
(
[RowGuid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] FILESTREAM_ON [FilestreamGroup]
GO
CREATE TABLE [dbo].[Files](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](1024) NULL,
[FileStreamId] [int] NOT NULL,
CONSTRAINT [PK_Files] 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
ALTER TABLE [dbo].[Files] WITH CHECK ADD CONSTRAINT [FK_Files_FileStreams] FOREIGN KEY([FileStreamId])
REFERENCES [dbo].[FileStreams] ([Id])
GO
ALTER TABLE [dbo].[Files] CHECK CONSTRAINT [FK_Files_FileStreams]
GO
Foreign key must be NO ACTION. It allows you to create INSTEAD OF trigger for table Files
All types of INSTEAD OF triggers are generally not supported with filestream tables - and DELETE is no different in this case. The reason is probably that it would have very complicated semantics in case of UPDATE triggers and out-of-band updates (from .Net or Win32).
If you provide some more details regarding your scenario then perhaps someone will be able to give you a workaround.

What is a reason that LINQ to SQL wouldn't generate a collection based on a relationship?

I have a relationship between two entities (e1 and e2) and e1 has a collection of e2, however I have a similar relationship set up between (e2 and e3), yet e2 does not contain a collection of e3's, any reason why this would happen? Anything I can post to make this easier to figure out?
Edit: I just noticed that the relationship between e1 and e2 is solid and between e2 and e3 is dotted, what causes that? Is it related?
Using this setup, everything worked.
1) LINQ to SQL Query, 2) DB Tables, 3) LINQ to SQL Data Model in VS.NET 2008
1 - LINQ to SQL Query
DataClasses1DataContext db = new DataClasses1DataContext();
var results = from threes in db.tableThrees
join twos in db.tableTwos on threes.fk_tableTwo equals twos.id
join ones in db.tableOnes on twos.fk_tableOne equals ones.id
select new { ones, twos, threes };
2 - Database Scripts
--Table One
CREATE TABLE tableOne(
[id] [int] IDENTITY(1,1) NOT NULL,
[value] [nvarchar](50) NULL,
CONSTRAINT [PK_tableOne] 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];
--Table Two
CREATE TABLE tableTwo(
[id] [int] IDENTITY(1,1) NOT NULL,
[value] [nvarchar](50) NULL,
[fk_tableOne] [int] NOT NULL,
CONSTRAINT [PK_tableTwo] 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];
ALTER TABLE tableTwo WITH CHECK
ADD CONSTRAINT [FK_tableTwo_tableOne]
FOREIGN KEY([fk_tableOne])
REFERENCES tableOne ([id]);
ALTER TABLE tableTwo CHECK CONSTRAINT [FK_tableTwo_tableOne];
--Table Three
CREATE TABLE tableThree(
[id] [int] IDENTITY(1,1) NOT NULL,
[value] [nvarchar](50) NULL,
[fk_tableTwo] [int] NOT NULL,
CONSTRAINT [PK_tableThree] 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];
ALTER TABLE tableThree WITH CHECK
ADD CONSTRAINT [FK_tableThree_tableTwo]
FOREIGN KEY([fk_tableTwo])
REFERENCES tableTwo ([id]);
ALTER TABLE tableThree CHECK CONSTRAINT [FK_tableThree_tableTwo];
3 - LINQ to SQL Data Model in Visual Studio
alt text http://i478.photobucket.com/albums/rr148/KyleLanser/ThreeLevelHierarchy.png
the FK_Contraints are set up like this:
ALTER TABLE [dbo].[e2] WITH CHECK ADD CONSTRAINT [FK_e2_e1] FOREIGN KEY([E1Id]) REFERENCES [dbo].[e1] ([Id])
ALTER TABLE [dbo].[e3] WITH CHECK ADD CONSTRAINT [FK_e3_e2] FOREIGN KEY([E2Id]) REFERENCES [dbo].[e2] ([Id])
is this what you were asking for?