2009-07-21 109 views
0

我認爲你可以用linq做到這一點,但它總是拋出一個外鍵錯誤,並且ContactType.id爲0.是否需要在插入新的ContactType後調用SubmitChanges,我缺少基本的東西?linq to sql @identity外鍵插入提交

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

在此question

回答lucas有必要設置ContactType對象,而不是外鍵值。

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() 

謝謝盧卡斯!