在订单中插入项目 [英] Inserting items in Order

查看:81
本文介绍了在订单中插入项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然在寻找实体框架和LINQ to Entities的方法。我有一个使用Angular JS的MVC应用程序。我有一个交易,存款和支票表。 Transactions表具有Deposits的外键。 Checks表具有来自Transactions的外来
键。交易可以有一个或多个支票。用户输入支票或支票,然后当他们保存每个支票时,支票会保存到表中,并带有自己的ID和交易ID。 

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. 

这是对象:

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.

这是存储库代码:

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;
        }

然而,当它到达_context.Set< Transaction>()。Add(transaction);它保存了一整套检查。只有第一个元素有id分配,而其他元素的GUID仍然是0。

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?

推荐答案

你好rsford31,

Hi rsford31,

>>有没有办法一次一个地保存每个支票?或者这甚至是最好的路径?如果我只是添加日期/时间或排序顺序列并对其进行排序会更好吗?

根据您的描述,它似乎是一对一 - 很多插入。一个事务有多个检查。我不明白为什么要插入 同一个事务多次,只需要插入一个时间。

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

致以最诚挚的问候,

Cole Wu


这篇关于在订单中插入项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆