Re: SqlServerCe foreign key problem
- From: "Laxmi Narsimha Rao Oruganti" <laxmi.online@xxxxxxxxxx>
- Date: Fri, 19 Dec 2008 09:55:48 +0530
Here is the problem as found by my friend Imran:
1) VolumeId and FieldId are identity columns and so are auto generated by
engine (i.e. not set by application)
2) Just for display purpose, all rows that are not sent to engine, but are
in dataset would show "-1" as identity value
3) When Volumes table row goes to engine, a identity value gets assigned to
VolumeId
4) When Files table row goes to engine, engine tries to validate the
constraint, but there is no value (or default value -1) is being sent to
engine and there is now row in Volumes table with VolumeId as "-1"; the
insert fails.
Hope that helps.
Thanks,
Laxmi
<marnix.koerselman@xxxxxxxxx> wrote in message
news:07444966-684c-41d0-9597-de901b4b659a@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi,
I'm probably an idiot, but I can't figure out what's wrong with this
situation:
-- create database --
string connectionString = @"Data Source = |DataDirectory|
\MyData.sdf";
SqlCeEngine engine = new SqlCeEngine(connectionString);
engine.CreateDatabase();
SqlCeCommand cmd = new SqlCeCommand();
cmd.Connection = new SqlCeConnection(connectionString);
cmd.Connection.Open();
cmd.CommandText = "CREATE TABLE Volumes (VolumeId int IDENTITY
CONSTRAINT PK_Volume PRIMARY KEY, Label nvarchar(20))";
cmd.ExecuteNonQuery();
cmd.CommandText = "CREATE TABLE Files(FileId int IDENTITY
CONSTRAINT PK_Files PRIMARY KEY,
Path nvarchar(256), VolumeId int NOT NULL,
CONSTRAINT FK_Volume FOREIGN KEY(VolumeId) REFERENCES
Volumes(VolumeId))";
cmd.ExecuteNonQuery();
... So now I have 2 tables, where each file row references a specific
volume row (right?)
-- run the code above, then import the generated database file
(VS2008) to create Data Connection, Data Source, MyDataSet,
TableAdapters ---
-- try to put some data in the tables --
TableAdapterManager tam = new TableAdapterManager();
tam.FilesTableAdapter = new FilesTableAdapter();
tam.VolumesTableAdapter = new VolumesTableAdapter();
tam.UpdateOrder =
TableAdapterManager.UpdateOrderOption.InsertUpdateDelete;
MyDataDataSet dataSet = new MyDataDataSet();
tam.VolumesTableAdapter.Fill(dataSet.Volumes);
tam.FilesTableAdapter.Fill(dataSet.Files);
MyDataDataSet.VolumesRow volume = dataSet.Volumes.AddVolumesRow
("label");
MyDataDataSet.FilesRow file = dataSet.Files.AddFilesRow("path",
volume);
tam.UpdateAll(dataSet);
... UpdateAll throws an exception: "A foreign key value cannot be
inserted because a corresponding primary key value does not exist.
[ Foreign key constraint name = FK_Volume ]"
I've also tried updating the Volumes table first using the
VolumesTableManager, or adding the records in a different manner, and
many other silly things, but same difference. Please... any advice/
ideas?
Regards, Marnix
.
- References:
- SqlServerCe foreign key problem
- From: marnix . koerselman
- SqlServerCe foreign key problem
- Prev by Date: Re: SqlServerCe foreign key problem
- Next by Date: Re: SqlServerCe foreign key problem
- Previous by thread: Re: SqlServerCe foreign key problem
- Next by thread: Re: SqlServerCe foreign key problem
- Index(es):
Relevant Pages
|