I use VS2010 to generate a SQL Server Compact database with two simple tables.
Both have a column TheID (int). On one I set unique to true on the other not.
And I set primary key on this column.
CREATE TABLE [TestTab] (
[TheID] int NOT NULL
, [TheVal] nvarchar(100) NOT NULL
);
GO
ALTER TABLE [TestTab] ADD CONSTRAINT [PK_TestTab] PRIMARY KEY ([TheID]);
GO
For the other (with unique set on the column) I get:
CREATE TABLE [TestTab2] (
[TheID] int NOT NULL
, [TheVal] nvarchar(100) NOT NULL
);
GO
ALTER TABLE [TestTab2] ADD CONSTRAINT [PK_TestTab2] PRIMARY KEY ([TheID]);
GO
CREATE UNIQUE INDEX [UQ__TestTab2__00000000000033E9] ON [TestTab2] ([TheID] ASC);
GO
Next I use the SQL Server Compact Toolbox to create a datacontext.
Selecting data works fine – but when I try to update the code it breaks on dc.SubmitChanges().
Simply my WP7 application ends.
A try / catch doesn’t help.
TestTab tT = (from A in dC.TestTabs select A).FirstOrDefault();
if(tT != null) {
tT.TheText += "1";
dC.SubmitChanges();
}
If I drop the unique index, the code works fine.
Any ideas why this happens?
Manfred
It is a LINQ to SQL on Windows Phone bug… Your index is a duplicate (the primary key definition also adds an index) – see my blog post here: http://erikej.blogspot.com/2012/04/windows-phone-local-database-tip.html. To get auto generated values, setting a primary keys is not enough, ypou must define the column as IDENTITY as well.