I am using Linq 2 Sql to knock out a quick bespoke CMS.
I have a table with an association to itself, so that any children menus are mapped to any parent menus within the same table, but I am having an issue inserting the first parent.
The table looks a little like this:
CMSMenu
CMSMenuID
ParentCMSMenuID
Name
Link
With an association of
CMSMenu.CMSMenuID -> CMSMenu.ParentCMSMenuID
In a previous version of the database/code I would simply add some code like:
itm = new CMSMenu();
itm.ParentCMSMenuID = 0;
rep.CMSMenus.InsertOnSubmit(itm);
But as this database was created automatically with all the foreign keys, this code causes an exception:
The INSERT statement conflicted with the FOREIGN KEY SAME TABLE
constraint “CMSMenu_CMSMenu”. The conflict occurred in database
“sp.ackahs”, table “dbo.CMSMenu”, column ‘CMSMenuID’. The statement
has been terminated.
How do I insert the top level menu (ie with no parent)?
As it does not have a parent so I cannot add:
itm.MyParentCMSMenu=*existing cmsmenu item*;
As I could with a child menu, nor can I add:
itm.MyParentCMSMenu=null;
Any thoughts welcome.
Thanks
Toby
Try setting MyParentCMSMenu to be a nullable column in the SQL.