Scheduled task - An entity object cannot be referenced by multiple instances of IEntityChangeTracker.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 years ago
Hello!
I have a code like

class ShipmentImportTask : ITask
    {
        private readonly ILogger _logger;
        private readonly ODBCSettings _odbcSettings;
        private readonly IShipmentService _shipmentService;
        private readonly IOrderService _orderService;
        private readonly IRepository<ShipmentExtensionRecord> _shipmentExtRepository;
        private readonly IRepository<Shipment>  _shipmentRepository;
        private readonly IOrderProcessingService _orderProcessingService;

        private ODBCConnector _odbcConnector;
        
        public ShipmentImportTask(ILogger           logger,
                                  ODBCSettings      odbcSettings)
        {
            this._logger = logger;
            this._odbcSettings = odbcSettings;
            this._orderService = EngineContext.Current.Resolve<IOrderService>();
            this._shipmentService = EngineContext.Current.Resolve<IShipmentService>();
            this._orderProcessingService = EngineContext.Current.Resolve<IOrderProcessingService>();
            this._shipmentRepository = EngineContext.Current.Resolve<IRepository<Shipment>>();
            this._shipmentExtRepository = EngineContext.Current.Resolve<IRepository<ShipmentExtensionRecord>>();

            this._odbcConnector = new ODBCConnector(_odbcSettings.ODBCName,
                                                        _odbcSettings.Username,
                                                        _odbcSettings.Password);
        }

public void Execute()
        {
            var strSql = new StringBuilder();

            strSql.Append("SELECT MsgTable.MsgId from MsgTable where (MsgTable.MsgType='Shipments') ");
            strSql.Append("AND (MsgTable.Source='1C' AND MsgTable.Destination='EC') AND (MsgTable.Status='Exported' OR MsgTable.Status='ImportedError') ");
            strSql.Append("ORDER BY MsgTable.MsgId ASC");

            IEnumerable<DataRow> q = from p in _odbcConnector.readData(strSql.ToString()).Tables[0].AsEnumerable() select p ;
            q.ToList().ForEach(p => InsertShipment(Convert.ToInt32(p.ItemArray[0])));
            
        }

                        var record = new Shipment()
                        {
                            OrderId = order.Id,
                            Order = order,
                            TrackingNumber = trackingNumber,
                            TotalWeight = Convert.ToDecimal(p.ItemArray[5]),
                            CreatedOnUtc = DateTime.Now
                        };

                        if (p.ItemArray[4] != null)
                        {
                            record.ShippedDateUtc = Convert.ToDateTime(p.ItemArray[4]);
                        }
                        _shipmentService.InsertShipment(record);



On Local machine all is fine, but on service method _shipmentService.InsertShipment(record) retrieve exception.
An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
Could u please tell me why and what can i do with this one?
6 years ago
Change this (remove Order = order;) and try again


var record = new Shipment()
{
      OrderId = order.Id,
      TrackingNumber = trackingNumber,
      TotalWeight = Convert.ToDecimal(p.ItemArray[5]),
      CreatedOnUtc = DateTime.Now
};



And don't forget to add some items to your shipment. Like in admin OrderController->AddShipment
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.