我认为你可以用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()
答案 0 :(得分:1)
必须设置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()
谢谢卢卡斯!