tags:

views:

395

answers:

1

Hi, I thought you could do this with linq, but it always throws a foreign key error and the ContactType.id is 0. Is it necessary to call SubmitChanges after inserting the new ContactType, or am I missing something basic?

Dim ct As New ContactType
ct.name = "supervisor"
db.ContactTypes.InsertOnSubmit(ct)

Dim c As New Contact
c.ContactTypeId = ct.id
c.first_name = "fname"
c.last_name = "lname"
db.contacts.InsertOnSubmit(c)

db.SubmitChanges()
+1  A: 

Answered by lucas in this question

It is necessary to set the ContactType object, not the foreign key value.

Dim ct As New ContactType
ct.name = "supervisor"
db.ContactTypes.InsertOnSubmit(ct)

Dim c As New Contact
c.ContactType = ct 'this is the important line
c.first_name = "fname"
c.last_name = "lname"
db.contacts.InsertOnSubmit(c)

db.SubmitChanges()

Thank you Lucas!