Reputation: 1425
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);
}
}
}
}
}
Am trying to update records automatically in Lightswitch and I am getting the following error:
Entity 'Member : 6' cannot be attached to this EntityContainer because it is already attached to another EntityContainer.
I have the following tables, Member(s), Reconcilliation(s), FromMainCompany, ToMainCompany & Saving(s). Member is related to Savings i.e. A member has savings. And Member is also related FromMainCompany (FromMainCompany is a collection of Members and their total salary account deduction - NOT that important)
I have a button in my Reconcilliation screen called UpdateRecords (which has the code above on its Execute() method) that I want when clicked to add Member monthly savings automatically to all the Members Saving table but now I get this error - Entity 'Member : 6' cannot be attached to this EntityContainer because it is already attached to another EntityContainer.
-Thanks.
Upvotes: 0
Views: 920
Reputation: 1425
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);
}
}
}
Figured it out, the AddNew() was supposed to be in the foreach loop. - cheers
Upvotes: 1