Hi there,
sorry for the late reply but the week was full of work
so here it comes.
I changed your original setup a little bit to fit mine
Code: Select all
CREATE TABLE `dept` (
`DEPTNO` int(4) NOT NULL,
`DNAME` varchar(14) DEFAULT NULL,
`LOC` varchar(13) DEFAULT NULL,
PRIMARY KEY (`DEPTNO`)
)
CREATE TABLE `emp` (
`EMPNO` int(4) NOT NULL,
`ENAME` varchar(10) DEFAULT NULL,
`JOB` varchar(9) DEFAULT NULL,
`MGR` int(4) DEFAULT NULL,
`HIREDATE` date DEFAULT NULL,
`SAL` int(7) DEFAULT NULL,
`COMM` int(7) DEFAULT NULL,
`DEPTNO` int(4) DEFAULT NULL,
PRIMARY KEY (`EMPNO`),
KEY `key` (`DEPTNO`)
)
CREATE TABLE `new_table` (
`dept_id` int(4) NOT NULL,
`emp_id` int(4) NOT NULL,
`percent` int(11) DEFAULT NULL,
PRIMARY KEY (`dept_id`,`emp_id`),
KEY `dept` (`dept_id`),
KEY `emp` (`emp_id`),
CONSTRAINT `dept` FOREIGN KEY (`dept_id`) REFERENCES `dept` (`DEPTNO`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `emp` FOREIGN KEY (`emp_id`) REFERENCES `emp` (`EMPNO`) ON DELETE NO ACTION ON UPDATE NO ACTION
)
Code: Select all
Sub TestUpdateRelation()
Dim updateNewTable() As NewTable = New NewTable() {
New NewTable With {.EmpId = 1001, .Percent = 10},
New NewTable With {.EmpId = 1002, .Percent = 55}
}
Dim dc As New ScottDataContext() With {.Log = Console.Out}
Dim dept = dc.Depts _
.Where(Function(d) d.Deptno = 30) _
.Single()
' ''dept.NewTables.Clear()
' ''dept.NewTables.Assign(updateNewTable)
''dept.NewTables.Clear()
''For Each item In updateNewTable
'' dept.NewTables.Add(New NewTable With {.EmpId = item.EmpId, .Percent = item.Percent})
''Next
'dept.NewTables.Clear()
'dc.SubmitChanges() ' Row not found or changed.
'For Each item In updateNewTable
' dept.NewTables.Add(New NewTable With {.EmpId = item.EmpId, .Percent = item.Percent})
'Next
dc.NewTables.DeleteAllOnSubmit(dept.NewTables)
For Each item In updateNewTable
dept.NewTables.Add(New NewTable With {.EmpId = item.EmpId, .Percent = item.Percent})
Next
dc.SubmitChanges()
End Sub
Sub Main()
'TestClearRelation()
TestUpdateRelation()
End Sub
As you can see on the count of the comments at the dept.NewTables* that was the way i got it to work.
The first time everything will work like it should but if you fire it up again then there will be dupplicate key errors or rows not found or changed.
btw. I like this approach the most
Code: Select all
dept.NewTables.Clear()
dept.NewTables.Assign(updateNewTable)
Another thing i noticed. You have the nice
in your context initialisation. But there is no single Line of SQL-Code in the output! That's anoying if you are looking for errors
I'm useing VB.net on Visual Studio 2010 SP1
Hope that helps!
Thanks in advance and for the great support Team!!