linq to sql @identity外键insertonsubmit

时间:2009-07-21 07:46:09

标签: linq-to-sql

我认为你可以用linq做到这一点,但它总是抛出一个外键错误而ContactType.id是0.是否有必要在插入新的ContactType后调用SubmitChanges,或者我是否遗漏了一些基本的东西?

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 个答案:

答案 0 :(得分:1)

lucas在此question

中回答{{3}}

必须设置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()

谢谢卢卡斯!