2009-07-21 95 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() 

谢谢卢卡斯!