locked
dbcontext.savechanges() error - trying to add multiple tables; error: value cannot be null: entity RRS feed

  • Question

  • User-309523270 posted

    Hi,

    I'm trying to save 2 tables into one transaction in the Service Layer.

    I'm running into the error

    value cannot be null. parameter name entity  at Microsoft.EntityFrameworkCore.Utilities.Check.NotNull

    {System.ArgumentNullException: Value cannot be null.Parameter name: entity at Microsoft.EntityFrameworkCore.Utilities.Check.NotNull[T](T value, String parameterName) at Microsoft.EntityFrameworkCore.DbContext.Add[TEntity](TEntity entity) at ToolsOracle.Business.Repository.InventoryRepository.ReceiveIntoInventory(ReceiveViewModel vmReceive) in C:\My Workspace\Apps\ToolsOracle\ToolsOracle.Business\Repository\InventoryRepository.cs:line 207 at ToolsOracle.Business.Service.ReceiveService.AddReceive(ReceiveViewModel vmReceive) in C:\My Workspace\Apps\ToolsOracle\ToolsOracle.Business\Service\ReceiveService.cs:line 117}
    Message: 

    Name Value Type
    Message "Value cannot be null.\r\nParameter name: entity" string

    I filled all the required columns in both Receive and Inventory.  In ReceiveService.cs, If I remove the line

    inv = _inventoryRepository.ReceiveIntoInventory(vmReceive);

    it's working fine.

    What am I missing?

    I'm using ASP.Core 2.1

    Thanks,

    tinac99

    ===========================

    Inventory.cs (generated by scaffold db) Oracle 11

        public partial class Inventory
        {
            public Inventory()
            {
                IssueTransaction = new HashSet<IssueTransaction>();
                Repair = new HashSet<Repair>();
                ReturnTransaction = new HashSet<ReturnTransaction>();
                Salvage = new HashSet<Salvage>();
            }

            public long InventoryId { get; set; }
            public string MaterialCode { get; set; }
            public int ToolRoomId { get; set; }
            public string PropertyId { get; set; }
            public string SerialNumber { get; set; }
            public string Description { get; set; }
            public int Quantity { get; set; }
            public string Bin { get; set; }
            public string Barcode { get; set; }
            public decimal? Uniqueununiqueconvert { get; set; }
            public string CreatedBy { get; set; }
            public DateTime CreatedDt { get; set; }

            public virtual Material MaterialCodeNavigation { get; set; }
            public virtual ToolRoom ToolRoom { get; set; }
            public virtual ICollection<IssueTransaction> IssueTransaction { get; set; }
            public virtual ICollection<Repair> Repair { get; set; }
            public virtual ICollection<ReturnTransaction> ReturnTransaction { get; set; }
            public virtual ICollection<Salvage> Salvage { get; set; }
        }

    ReceiveTransaction.cs

    namespace ToolsOracle.Data.Models
    {
        public partial class ReceiveTransaction
        {
            public long ReceiveId { get; set; }
            public string MaterialCode { get; set; }
            public int ToolRoomId { get; set; }
            public string PropertyId { get; set; }
            public string SerialNumber { get; set; }
            public int Quantity { get; set; }
            public DateTime ReceiveDate { get; set; }
            public string Rms { get; set; }
            public string PoOrd { get; set; }
            public string CreatedBy { get; set; }
            public DateTime CreatedDt { get; set; }

            public virtual Material MaterialCodeNavigation { get; set; }
            public virtual ToolRoom ToolRoom { get; set; }
        }
    }

    Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {

    services.AddMvc();
    services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

    var ora_conn = "connectionstring";
    services.AddDbContext<ToolsDbContext>(options => options.UseOracle(ora_conn,
        b=>b.UseOracleSQLCompatibility("11")));

    services.AddScoped<ReceiveService>();
    services.AddScoped<ReceiveRepository>();
    services.AddScoped<InventoryRepository>();
    services.AddScoped<MaterialRepository>();

    }

    ReceiveService.cs

    namespace ToolsOracle.Business.Service
    {
        public class ReceiveService
        {
            protected ToolsDbContext _db;

            private ReceiveRepository _receiveRepository;
            private MaterialRepository _materialRepository;
            private InventoryRepository _inventoryRepository;

            private StringBuilder sbErrors;

            public ReceiveService(ToolsDbContext db, ReceiveRepository receiveRepository, MaterialRepository materialRepository, InventoryRepository inventoryRepository)
            {
                _db = db;
                _receiveRepository = receiveRepository;
                _materialRepository = materialRepository;
                _inventoryRepository = inventoryRepository;

                sbErrors = new StringBuilder();
            }

           public ReceiveViewModel AddReceive(ReceiveViewModel vmReceive)
            {
                ReceiveTransaction receive = null;
                Inventory inv = null;
                long? lInventoryId = (long?)null;
                try
                {

                    vmReceive.PoOrd = vmReceive.Rms;
                    vmReceive.CreatedDt = DateTime.Now;

                    receive = _receiveRepository.Add(vmReceive);
                    inv = _inventoryRepository.ReceiveIntoInventory(vmReceive);

                    _db.SaveChanges();

                }

                catch (Exception ex)
                {
                    sbErrors.Append(ex.Message);
                    sbErrors.Append(System.Environment.NewLine);
                    sbErrors.Append(ex.InnerException.Message );

                    //base.RollbackTransaction();

                    LogWriter.LogWrite(sbErrors.ToString());

                    throw new Exception(sbErrors.ToString());
                }
                finally
                {
                   
                }

                return vmReceive;
            }

    ReceiveRepository.cs


    namespace ToolsOracle.Business.Repository
    {
        public class ReceiveRepository
        {
            protected ToolsDbContext db;
            MapperConfiguration config;

            public ReceiveRepository(ToolsDbContext _db)
            {
                db = _db;
            }


            public ReceiveTransaction Add(ReceiveViewModel vmReceive)
            {
                ReceiveTransaction destination = null;
                if (IsValid(vmReceive))
                {
                    var config = new MapperConfiguration(cfg => {
                        cfg.CreateMap<ReceiveTransaction, ReceiveViewModel>().ReverseMap();
                    });


                    IMapper iMapper = config.CreateMapper();
                    destination = iMapper.Map<ReceiveViewModel, ReceiveTransaction>(vmReceive);

                    db.ReceiveTransaction.Add(destination);

                    vmReceive = iMapper.Map<ReceiveTransaction, ReceiveViewModel>(destination);
                }

                return destination;
            }

    }

    InventoryRepository.cs

        public class InventoryRepository 
        {
            protected ToolsDbContext db;
            private MaterialRepository _materialRepository;
            MapperConfiguration config;


            public InventoryRepository(ToolsDbContext _db, MaterialRepository materialRepository)
            {
                db = _db;
                _materialRepository = materialRepository;
               
            }

            public Inventory FindExact(int aiToolRoomid, string asMaterialCode)
            {
                return FindExact(aiToolRoomid, asMaterialCode, null);
            }

            public Inventory FindExact(int aiToolRoomid, string asMaterialCode, string asPropertyId)
            {

                var query = db.Inventory
                        .Where(item => (item.MaterialCode == asMaterialCode) &&
                              (item.ToolRoomId == aiToolRoomid)) ;

                return query.FirstOrDefault();

            }

            public Inventory ReceiveIntoInventory(ReceiveViewModel vmReceive)
            {
                Inventory rec = FindExact(vmReceive.ToolRoomId, vmReceive.MaterialCode, vmReceive.PropertyId);

                if (rec == null)
                {

                    Inventory inv = new Inventory();
                    inv.MaterialCode = vmReceive.MaterialCode ;
                    inv.ToolRoomId = vmReceive.ToolRoomId ;
                    inv.PropertyId = vmReceive.PropertyId ;
                    inv.SerialNumber = vmReceive.SerialNumber ;
                    inv.Quantity = vmReceive.Quantity ;
                    inv.Bin = vmReceive.Bin ;
                    inv.CreatedBy = vmReceive.CreatedBy ;
                    inv.CreatedDt = vmReceive.CreatedDt ;

                    db.Add(rec);
                }
                return rec;
            }

    Monday, July 22, 2019 10:25 PM

Answers

  • User-309523270 posted

    I got it: 

    InventoryRepository.ReceiveIntoInventory(ReceiveViewModel vmReceive)

                    db.Add(rec);  => changed to db.Add(inv); 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 22, 2019 10:32 PM