Just spent a number of hours trying to figure out why my Linq was not updating my foreign key when adding a record with a one-to-one relationship. Do not know if it is an issue or by design, but it took me a while to try the obvious....
Here is the simple tables I was using in this test:
Table Contacts
(
Contact_ID [int] IDENTITY(1,1) NOT NULL,
CreatedAt] [datetime] NOT NUL,
FullName [nvarchar](128) NOT NULL,
CONSTRAINT [PK_Contacts] PRIMARY KEY CLUSTERED
(
Contact_ID ASC
)
Table Comments
(
Comment_ID [int] IDENTITY(1,1) NOT NULL,
Contact_ID [int] NOT NULL,
LastEditedAt [datetime] NULL,
Comment [nvarchar](1024) NULL,
CONSTRAINT [PK_Comments] PRIMARY KEY CLUSTERED
(
[Comment_ID] ASC,
[Contact_ID] ASC
)
Table AdditionalInfo
(
Contact_ID [int] NOT NULL,
LastUpdatedAt [datetime] NULL,
AdditionalInfo [nvarchar](1024) NULL,
CONSTRAINT [PK_ContactAdditionalInfo] PRIMARY KEY CLUSTERED
(
Contact_ID ASC
)
The table "Comments" has a one-to-many relationship with "Contacts" and the "AdditionalInfo" table has a one-to-one relationship to "Contacts".
When these tables are pulled into the designer in VS 2008 beta 1, it seems to generate the correct code for them. Adding a new contact and a comment is easy enough and works correctly (I prefixed all the table entities with "Our":
ContactClassesDataContext db = new ContactClassesDataContext();
OurContact contact = new OurContact
{
FullName = "Ben Here Too",
CreatedAt = DateTime.Now,
Comments =
{ new OurComment
{ LastEditedAt = DateTime.Now,
Comment = "Typical Comment" } }
};
db.OurContacts.Add(contact);
db.SubmitChanges();
So, since there is a one-to-one relationship to the AdditionalInfo table, it seemed we only had to set the property to a new instance of the OurAdditionalInfo and it would be good to go, like:
ContactClassesDataContext db = new ContactClassesDataContext();
OurContact contact = new OurContact
{
FullName = "Ben There Too",
CreatedAt = DateTime.Now,
Comments =
{ new OurComment
{ LastEditedAt = DateTime.Now,
Comment = "Still no Joy" } }
AdditionalInfo = new OurAdditionalInfo
{
LastUpdatedAt = DateTime.Now,
AdditionalInfo = "Some new Stuff",
} ;
};
db.OurContacts.Add(contact);
db.SubmitChanges();
No such luck, it kept blowing errors on the primary key (contact_ID) as Linq was setting it to zero. So, something was different between the one-to-one and the one-to-many relationships. After much banging around with the code, I finally decided to try setting the "contact" property on the new OurAdditionalInfo object such as:
ContactClassesDataContext db = new ContactClassesDataContext();
OurContact contact = new OurContact
{
FullName = "Ben Here Finally"
CreatedAt = DateTime.Now,
Comments =
{ new OurComment
{ LastEditedAt = DateTime.Now,
Comment = "Simple, answer" } }
};
contact.AdditionalInfo = new OurAdditionalInfo
{
LastUpdatedAt = DateTime.Now,
AdditionalInfo = "Some new Stuff",
Contact = contact
} ;
db.OurContacts.Add(contact);
db.SubmitChanges();
That seemed to do the trick! Like I said, not sure if this is by design, but it would make more sense to me to not have to set the contact the same as you can do with the comment entries. Just glad I found that and can move on
.