2017-07-03 67 views
1

我正在使用Acumatica框架創建一個圖形,並試圖保存對數據庫的更改,但我在保存時遇到了一個錯誤。保存親子

我有一個典型的父/子(即主/細節)關係,所有東西都應該正確設置。

我的孩子DAC具有PXDBDefault和PXParent條目:

 [PXDBInt()] 
     [PXDBDefault(typeof(DCRuleHeader.ruleHeaderID))] 
     [PXParent(typeof(Select<DCRuleHeader, Where<DCRuleHeader.ruleHeaderID, Equal<Current<DCRule.ruleHeaderID>>>>))] 
     public virtual int? RuleHeaderID 
     { 
      get 
      { 
       return this._RuleHeaderID; 
      } 
      set 
      { 
       this._RuleHeaderID = value; 
      } 
     } 

在標題中,該ID是在數據庫中的標識和如下定義爲DAC一個鍵:

[PXDBIdentity(IsKey = true)] 
    [PXUIField(Enabled = false)] 
    public virtual int? RuleHeaderID 
    { 
     get 
     { 
      return this._RuleHeaderID; 
     } 
     set 
     { 
      this._RuleHeaderID = value; 
     } 
    } 

觀也相應地配置:

public class RulesMaint : PXGraph<RulesMaint, DCRuleHeader> 
    { 

     public PXSelect<DCRuleHeader> RuleHeader; 
     public PXSelect<DCRule, Where<DCRule.ruleHeaderID, Equal<Current<DCRuleHeader.ruleHeaderID>>>> Rules; 
     public PXAction<DCRuleHeader> ViewRule; 

然而,下面的代碼不是worki NG

RulesMaint rulesGraph = PXGraph.CreateInstance<RulesMaint>(); 
DCRuleHeader newHeader = rulesGraph.RuleHeader.Insert(); 
... 
DCRule rule = rulesGraph.Rules.Insert(); 
... 
rulesGraph.Actions.PressSave(); 

當我嘗試了上面,我得到的錯誤「錯誤‘RuleHeaderID’不能爲空」

不宜框架處理一切本身並自動設置子對象的父ID?這不就是PXDBDefault的用途嗎?

+0

在你插入代碼,你沒有提供的情況下插入,你忘了將其添加到這個問題? –

+0

也嘗試在頭中使用PXDBInt(IsKey = true)替換PXDBIdentity,並在DCRule中添加IsKey = true PXDBInt –

+0

感謝您的評論。我沒有通過實例,因爲我只是想用默認值插入。我也嘗試傳遞實例,但同樣的錯誤。將嘗試執行身份替換和更改,因爲您建議 –

回答

2

下面是一個示例,瞭解如何使用Acumatica ERP中的Form Detail視圖獲取父/子DAC。 對於乞討,讓我們創建一個以下列方式親子SQL表: 家長:

/****** Object: Table [dbo].[SOCustomParentTable] Script Date: 07/03/2017 12:55:17 ******/ 
SET ANSI_NULLS ON 
GO 

SET QUOTED_IDENTIFIER ON 
GO 

CREATE TABLE [dbo].[SOCustomParentTable](
    [CompanyID] [int] NOT NULL, 
    [ParentID] [int] IDENTITY(1,1) NOT NULL, 
    [Description] [nvarchar](255) NULL, 
    [SomeOtherField] [nvarchar](50) NULL, 
    [ParentCD] [nvarchar](15) NOT NULL 
) ON [PRIMARY] 

GO 

而且孩子現在我們有SQL表準備好,讓我們創建DAC的爲以下

/****** Object: Table [dbo].[SOCustomChildTable] Script Date: 07/03/2017 12:54:39 ******/ 
SET ANSI_NULLS ON 
GO 

SET QUOTED_IDENTIFIER ON 
GO 

CREATE TABLE [dbo].[SOCustomChildTable](
    [CompanyID] [int] NOT NULL, 
    [ChildID] [int] IDENTITY(1,1) NOT NULL, 
    [ParentID] [int] NOT NULL, 
    [Description] [nvarchar](255) NULL, 
    [SomeOtherField] [nvarchar](50) NULL 
) ON [PRIMARY] 

GO 

類:

家長:

using System; 
using PX.Data; 

namespace DemoParentChild 
{ 
    [Serializable] 
    public class SOCustomParentTable: IBqlTable 
    { 


    #region ParentID 

    [PXDBIdentity()] 
    public int? ParentID { get; set; } 

    public class parentID : IBqlField{} 

    #endregion 


    #region Description 

    [PXDBString(255, IsUnicode = true, InputMask = "")] 
    [PXUIField(DisplayName = "Description")] 
    public string Description { get; set; } 

    public class description : IBqlField{} 

    #endregion 


    #region SomeOtherField 

    [PXDBString(50, IsUnicode = true, InputMask = "")] 
    [PXUIField(DisplayName = "Some Other Field")] 
    public string SomeOtherField { get; set; } 

    public class someOtherField : IBqlField{} 

    #endregion 


    #region ParentCD 

    [PXDBString(15,IsKey = true, IsUnicode = true, InputMask = "")] 
    [PXUIField(DisplayName = "Parent ID")] 
    public string ParentCD { get; set; } 

    public class parentCD : IBqlField{} 

    #endregion 

    } 
} 

那孩子:

using System; 
using PX.Data; 

namespace DemoParentChild 
{ 
    [Serializable] 
    public class SOCustomChildTable: IBqlTable 
    { 


    #region ChildID 

    [PXDBIdentity(IsKey=true)] 
    public int? ChildID { get; set; } 

    public class childID : IBqlField{} 

    #endregion 


    #region ParentID 

    [PXDBInt()] 
    [PXDBDefault(typeof(SOCustomParentTable.parentID))] 
    [PXParent(typeof(Select<SOCustomParentTable, Where<SOCustomParentTable.parentID, Equal<Current<SOCustomChildTable.parentID>>>>))] 
    public int? ParentID { get; set; } 

    public class parentID : IBqlField{} 

    #endregion 


    #region Description 

    [PXDBString(255, IsUnicode = true, InputMask = "")] 
    [PXUIField(DisplayName = "Description")] 
    public string Description { get; set; } 

    public class description : IBqlField{} 

    #endregion 


    #region SomeOtherField 

    [PXDBString(50, IsUnicode = true, InputMask = "")] 
    [PXUIField(DisplayName = "Some Other Field")] 
    public string SomeOtherField { get; set; } 

    public class someOtherField : IBqlField{} 

    #endregion 

    } 
} 

而要完成我們的工作,讓我們創建FormDetail類型的頁面與下圖:

using System; 
using PX.Data; 

namespace DemoParentChild 
{ 
    public class tmp : PXGraph<tmp> 
    { 

    public PXSave<SOCustomParentTable> Save; 
    public PXCancel<SOCustomParentTable> Cancel; 
    public PXPrevious<SOCustomParentTable> Prev; 
    public PXNext<SOCustomParentTable> Next; 


    public PXSelect<SOCustomParentTable> MasterView; 
    public PXSelect<SOCustomChildTable,Where<SOCustomChildTable.parentID,Equal<Current<SOCustomParentTable.parentID>>>> DetailsView; 

    } 
} 

現在,讓我們明白這一切是如何工作。 正如您所看到的,ParentID是SQL中的Identity,並且在DAC中設置爲PXDBIdentity,但它不會被設置爲DAC的關鍵字,因爲我們將使用ParentCD作爲可見的Key。 同樣在Child類中,ChildID被設置爲PXDBIdentity,但它被設置爲Key,因爲我們不需要該行具有用戶的可見鍵。 另外在子類中我們將創造的父/子關係如下:

[PXDBInt()] 
[PXDBDefault(typeof(SOCustomParentTable.parentID))] 
[PXParent(typeof(Select<SOCustomParentTable, Where<SOCustomParentTable.parentID, Equal<Current<SOCustomChildTable.parentID>>>>))] 
public int? ParentID { get; set; } 

凡PXDefault是孩子的設置ParendID到當前父的ID和PXParent創造目前孩子和家長之間的關係。

通常會爲父母上的子女和線路計數器創建線路號碼,以瞭解子女的計數。

完整的定製,你可以下載here

+0

偉大的職位。有一點要提到的是,當從父級的自動生成的密鑰字段設置子值時,使用PXDBDefault與PXDefault。以SOLine.OrderNbr爲例。 – Brendan

+0

@Brendan是的,你是對的。我已經更新了答案 –