【问题标题】:Acumatica PXGraph.CreateInstance throwing errorAcumatica PXGraph.CreateInstance 抛出错误
【发布时间】:2019-08-14 23:54:23
【问题描述】:

我有自己的 PXGraph 类用于我自己的表,当我 CreateInstance() 时它会引发错误。错误是“System.NullReferenceException:对象引用未设置为对象的实例。”出于某种原因,它不会发生在我的开发环境中——仅在部署后的生产环境中发生。

调用栈:

   at PX.Data.PXSelectorAttribute.populateFields(PXCache sender, Boolean bypassInit)
   at PX.Data.PXSelectorAttribute.CacheAttached(PXCache sender)
   at PX.Data.PXEventSubscriberAttribute.InvokeCacheAttached(PXCache cache)
   at PX.Data.PXCache`1..ctor(PXGraph graph)
   at PX.Data.PXCacheCollection.get_Item(Type key)
   at PX.Data.PXView.get_Cache()
   at PX.Data.PXRestrictorAttribute.AlterCommand(PXCache sender)
   at PX.Data.PXRestrictorAttribute.CacheAttached(PXCache sender)
   at PX.Data.PXAggregateAttribute.CacheAttached(PXCache sender)
   at PX.TM.PXOwnerSelectorAttribute.CacheAttached(PXCache sender)
   at PX.Data.PXEventSubscriberAttribute.InvokeCacheAttached(PXCache cache)
   at PX.Data.PXCache`1..ctor(PXGraph graph)
   at PX.Data.PXCacheCollection.get_Item(Type key)
   at PX.Data.PXView..ctor(PXGraph graph, Boolean isReadOnly, BqlCommand select)
   at PX.Data.PXSelect`2..ctor(PXGraph graph)
   at _Initialize(PXGraph )
   at PX.Data.PXGraph._InitializeDelegate.Invoke(PXGraph graph)
   at PX.Data.PXGraph..ctor()
   at PX.Data.PXGraph`1..ctor()
   at Aktion.Acumatica.RoyalBay.Customizations.SubscribeTRGData.Graphs.AKTRGItemClassMapMaint..ctor()
   at PX.Data.PXGraph.CreateInstance(Type graphType, String prefix)
   at PX.Data.PXGraph.CreateInstance(Type graphType)
   at PX.Data.PXGraph.CreateInstance[Graph]()

图表类:

 public class AKTRGItemClassMapMaint : PXGraph<AKTRGItemClassMapMaint>
    {
        public PXSelect<AKTRGItemClassMap,
            Where<AKTRGItemClassMap.trgItemClassCD, Equal<Required<AKTRGItemClassMap.trgItemClassCD>>>> TRGItemClassMaps;

        public PXSelect<INSetup> Settings;

        public PXSelect<INItemClass,
            Where<INItemClass.itemClassID, Equal<Required<INItemClass.itemClassID>>>> ClassItems;
    }

CreateInstance 调用:

var classMapGraph = PXGraph.CreateInstance<AKTRGItemClassMapMaint>();

表结构:

CREATE TABLE [dbo].[AKTRGItemClassMap](
    [CompanyID] [int] NOT NULL,
    [ItemClassID] [int] NOT NULL,
    [ItemClassCD] [nvarchar](30) NOT NULL,
    [TRGItemClassCD] [nvarchar](30) NOT NULL,
 CONSTRAINT [PK_EEdiDocType] PRIMARY KEY CLUSTERED 
(
    [CompanyID] ASC,
    [ItemClassID] ASC,
    [TRGItemClassCD] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

还有DAC类:

[System.SerializableAttribute()]
public class AKTRGItemClassMap : PX.Data.IBqlTable
{
    public abstract class itemClassID : IBqlField { }

    [PXDBInt]
    public virtual int? ItemClassID { get; set; }

    public abstract class itemClassCD : IBqlField { }

    [PXDBString(30, IsUnicode = true)]
    public virtual string ItemClassCD { get; set; }

    public abstract class trgItemClassCD : IBqlField { }

    [PXDBString(30, IsUnicode = true)]
    public virtual string TRGItemClassCD { get; set; }
}

有什么想法吗?

【问题讨论】:

  • 您页面的主要视图是什么?你能分享一个预期布局的模型吗?我很好奇为什么你在两个 BQL 查询中有Required。
  • 我同意所需的似乎不合适,并且不要忘记您的 dac 字段 ItemClassID 和 TRGItemClassCD 上的 iskey
  • Gabriel,此图没有 UI。我现在只是用它来轻松地查询一些带有视图的表。要求只是将参数传递给查询。将来可能会有一个 UI 来填充表格,但不是现在。
  • Brendan,我可以添加 IsKey,但它们真的有必要吗?
  • 如果 DAC 将值存储在数据库中,是的,您需要指出哪些字段是关键字段。过滤器的 DAC 可能是唯一不需要 iskey 的场景。

标签: acumatica


【解决方案1】:

解决我的问题的方法是使用 PXLoginScope 来包装任何图形实例的创建和使用,以提供登录用户上下文。

using (var pxLoginScope = new PXLoginScope(userName, new string[0]))
{
    var membershipUser = Membership.GetUser(pxLoginScope.UserName);

    if (membershipUser != null)
    {
        pxLoginScope.UserName = membershipUser.UserName;
        userName = PXContext.PXIdentity.User.Identity.Name;

        if (pxLoginScope.CompanyName != null)
        {
            string[] rolesForUser = System.Web.Security.Roles.GetRolesForUser(pxLoginScope.UserName);
            if (rolesForUser == null || rolesForUser.Length == 0)
                throw new PXException("You are not allowed to login to the company {0}.", new object[1]
                    {
                        (object) pxLoginScope.CompanyName
                    });
            else if (!Enumerable.Contains<string>((IEnumerable<string>)PXDatabase.AvailableCompanies,
                pxLoginScope.CompanyName))
                throw new PXException("You are not allowed to login to the company {0}.", new object[1]
                    {
                        (object) pxLoginScope.CompanyName
                   });
        }

        PXLogin.SetBranchID(pxLoginScope.UserName, pxLoginScope.CompanyName, pxLoginScope.Branch);

        <graph creation and usage goes here>
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 2015-03-24
    • 2011-12-27
    相关资源
    最近更新 更多