无法附加到此EntityContainer,因为它已附加到另一个EntityContainer。 Lightswitch自动更新集合

时间:2013-04-06 07:15:01

标签: c# silverlight visual-studio-lightswitch

 partial void updateRecords_Execute()
    {
        // Write your code here.

        using (var tempWorkspace = new DataWorkspace())
        {
            Saving NewSavings = tempWorkspace.ApplicationData.Savings.AddNew();

            var koo = from a in FromMainCompanies
                      select a;





            foreach (var i in koo)
                {


                    if (i.Member != null)
                    {
                        NewSavings.CaptureDate = DateTime.Now;
                        NewSavings.Amount = i.Member.Savings.Select(a => a.Amount).FirstOrDefault();
                        NewSavings.FinancialYear = tempWorkspace.ApplicationData.FinancialYears.FirstOrDefault();
                        NewSavings.Member = i.Member;
                        NewSavings.NewSavingsAmount = i.Member.Savings.Select(a => a.NewSavingsAmount).FirstOrDefault();

                        try
                        {
                            tempWorkspace.ApplicationData.SaveChanges();

                        }
                        catch (Exception e)
                        {
                            this.ShowMessageBox(e.Message);
                        }   
                    }



                }





        }

}

尝试在Lightswitch中自动更新记录,我收到以下错误:

实体'成员:6'无法附加到此EntityContainer,因为它已附加到另一个EntityContainer。

我有以下表格,会员,Reconcilliation(s),FromMainCompany,ToMainCompany&保存(S)。会员与储蓄有关,即会员有储蓄。而会员也与FromMainCompany有关(FromMainCompany是会员及其总薪资账户扣除的集合 - 并非那么重要

我的Reconcilliation界面中有一个名为UpdateRecords 的按钮(其上面有其Execute()方法的代码)我想要点击这个按钮,以便自动将会员每月节省成本添加到所有会员保存表但现在我收到此错误 - 实体'成员:6'无法附加到此EntityContainer,因为它已附加到另一个EntityContainer。

-Thanks。

1 个答案:

答案 0 :(得分:1)

partial void updateRecords_Execute()
    {
        // Write your code here.

        using (var tempWorkspace = new DataWorkspace())
        {
            var mymembers = tempWorkspace.ApplicationData.Members;
            //var myscreen = this.Reconcilliations1.SelectedItem.FromMainCompanies.Where(a => a.Member != null).Select(b => b.Member);
            //Member myMember = new Member();
            foreach (Member item in mymembers)
            {
            Saving NewSavings = tempWorkspace.ApplicationData.Savings.AddNew();

            ////var koo = from a in FromMainCompanies
            ////          select a.Member;


                NewSavings.CaptureDate = DateTime.Now;
                NewSavings.Amount = item.Savings.Select(a=>a.Amount).LastOrDefault();
                NewSavings.FinancialYear = tempWorkspace.ApplicationData.FinancialYears.FirstOrDefault();
                NewSavings.Member = item;
                NewSavings.NewSavingsAmount = item.Savings.Select(a=>a.NewSavingsAmount).LastOrDefault();
            }





            try
            {
                tempWorkspace.ApplicationData.SaveChanges();

            }
            catch (Exception e)
            {
                this.ShowMessageBox(e.Message);
            } 

        }

        }

想出来,AddNew()应该是在foreach循环中。 - 干杯