LinQ to SQL数据插入问题

时间:2009-08-15 14:14:16

标签: linq-to-sql

我有两个表Settlement和Violations.SettlementID是Settlements表中的主键和Violations中的外键。

在我的dbml文件中,我拖放了两个表并设置了关联。

在我的VB.net代码中,当我尝试将记录插入数据库时​​,它没有提供将违规集合添加到结算中的选项

我正在尝试使用Settlements和Violation读取xml文件并插入SQL。但只有插入记录表才会插入结算表。

我无法将_v添加到结算s中,因为它未列出。

请帮忙

Dim Orders As XDocument = XDocument.Load("c:/inetpub/violations.xml")

    Dim _Orderss = violations.<Collections>.<Settlement>

    For Each _settlement In _settlements
        Dim s As New Settlement

        s.CamisID = _settlement.<CamisID>.Value 
        s.DocketID = _settlement.<DocketID>.Value
        s.RespFName = _settlement.<RespFName>.Value
        s.RespMName = _settlement.<RespMName>.Value
        s.RespLName = _settlement.<RespLName>.Value
        s.DispDate = Date.Parse(_settlement.<DispDate>.Value)

        Dim _violations = _settlement.<ViolationList>.<Violation>

        For Each _violation In _violations
            Dim _v As New Violation

            _v.DocketID = _settlement.<DocketID>.Value
            _v.ViolationInfo = _violation.<ViolationInfo>.Value
            _v.violationCD = _violation.<ViolationCd>.Value
            _v.Severity = Integer.Parse(_violation.<Severity>.Value)
            _v.Point = Integer.Parse(_violation.<Point>.Value)
            _v.Occurence = Integer.Parse(_violation.<Occurence>.Value)


        Next
        _db.Settlements.InsertOnSubmit(s)
        _db.SubmitChanges()
    Next

1 个答案:

答案 0 :(得分:0)

您需要设置每个子项的父属性,而不是将子项添加到父项。

您是否可以尝试设置每个_v的Settelment属性,而不是将违规行为添加到结算中?

For Each _settlement In _settlements
    Dim s As New Settlement

    s.CamisID = _settlement.<CamisID>.Value 
    s.DocketID = _settlement.<DocketID>.Value
    s.RespFName = _settlement.<RespFName>.Value
    s.RespMName = _settlement.<RespMName>.Value
    s.RespLName = _settlement.<RespLName>.Value
    s.DispDate = Date.Parse(_settlement.<DispDate>.Value)



    Dim _violations = _settlement.<ViolationList>.<Violation>

    For Each _violation In _violations
        Dim _v As New Violation

        _v.DocketID = _settlement.<DocketID>.Value
        _v.ViolationInfo = _violation.<ViolationInfo>.Value
        _v.violationCD = _violation.<ViolationCd>.Value
        _v.Severity = Integer.Parse(_violation.<Severity>.Value)
        _v.Point = Integer.Parse(_violation.<Point>.Value)
        _v.Occurence = Integer.Parse(_violation.<Occurence>.Value)
        _v.Settelment = s
        _db.Violations.InsertOnSubmit(_v)
    Next

    _db.Settlements.InsertOnSubmit(s)
    _db.SubmitChanges()

Next