none
Inserting items in Order RRS feed

  • Question

  • Hi, I'm still finding my way around the Entity Framwork and LINQ to Entities. I have an MVC app that uses Angular JS. I have a Transactions, Deposits and Checks table. The Transactions table has the a foreign key from Deposits. The Checks table has the foreign key from Transactions. A transaction can have one or more checks. The user enters in a check or checks and then when they save each individual check is saved to the table with it's own id and the transactions id. 

    Here is the object:

    function initializeTransaction() {
                vm.currentTransaction =
                    {
                        Id: null,
                        DepositId: vm.deposits[vm.currentDepositIndex].Id,
                        Deposit: vm.deposits[vm.currentDepositIndex],
                        FiveCount: 0,
                        TenCount: 0,
                        TwentyCount: 0,
                        FiftyCount: 0,
                        HundredCount: 0,
                        ThousandCount: 0,
                        Coin: 0,
                        Checks: [],
                        ToInstitutionId: vm.institution.Id,
                        Date: new moment().format()
                    };
    
                vm.checkTotal = 0;
            }

    A report is available that lists all the checks for each transaction. They all appear in order if there is one check per transaction. However if there are multiple checks per transaction they will not be in order.

    Here is the repository code:

    public Transaction Create(Transaction transaction)
            {
                transaction.Id = Guid.NewGuid();
                transaction.Deposit = _context.Set<Deposit>().Find(transaction.DepositId);
                transaction.Deposit.Processed = true;
    
               
                transaction.Cheques.ToList().ForEach(
                    c =>
                    {
                        c.Id = Guid.NewGuid();
                        c.CurrencyId = c.Currency.Id;
                        c.Currency = null;
                    }
                );
    
               _context.Set<Transaction>().Add(transaction);
    
               _context.SaveChanges();
    
                return transaction;
            }

    I understand that it not inserting in order is by design. From what I've read if the items can be saved one at a time to the table they will be in order. So I tried this:

     public Transaction Create(Transaction transaction)
            {
                transaction.Id = Guid.NewGuid();
                transaction.Deposit = _context.Set<Deposit>().Find(transaction.DepositId);
                transaction.Deposit.Processed = true;
    
                Check[] check = transaction.Check.ToArray();
    
                foreach (Check check in check)
                {
                    check.Id = Guid.NewGuid();
                    check.CurrencyId = check.Currency.Id;
                    check.Currency = null;
                    check.TransactionId = transaction.Id;
                    _context.Set<Transaction>().Add(transaction);
    
                    _context.SaveChanges();
                }
    
    
                return transaction;
            }

    However, when it gets to _context.Set<Transaction>().Add(transaction); it's saving the whole array of checks. Only the first element has ids assigned while the others' GUID are still all 0's.

    Is there a way to save each check with the transaction one at a time? Or is this even the best path to follow? Would it be better if I just add a date/time or sort order column and sort on that instead?

    Monday, May 15, 2017 1:32 AM

All replies

  • Hi rsford31,

    >>Is there a way to save each check with the transaction one at a time? Or is this even the best path to follow? Would it be better if I just add a date/time or sort order column and sort on that instead?

    According to your description, it seems that it is one-to-many insert. one transaction has multiple checks. I don't understand why insert  the same transaction multiple times, which just need to insert a time.

    Please use the middle of code. For more information about one-to-many insert, please refer to:

    http://www.entityframeworktutorial.net/EntityFramework4.3/add-one-to-many-entity-using-dbcontext.aspx

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 16, 2017 7:03 AM
    Moderator