Not sure if it's correct, but it took the error away and I could run the program. On db.SaveChanges(), it gave the error that Validation failed for one or more entities.
How do I debug with an error? I added the <TutorCommission>() back so that I could debug. But now I'm getting that TutorCommission cannot be constructed in a linq to entities query.
Hi there, I managed to run the code and get database entries to generate by changing a line:
var newCommissions = from enrolment in enrolments
select new TutorCommission()
{
CommissionAmount = enrolment.MonthlyFee,
CommissionMonth = month, // string constant
CommissionStatus = "Unpaid",
Tutor = enrolment.Tutor
};
So I changed the last line to the above from Tutor = new Tutor { TutorNoID = enrolment.Tutor.TutorNoID, TutorCommissionPercentage = enrolment.Tutor.TutorCommissionPercentage } So there are no errors now and database entries are being added. The problem is, a database entry is added per enrollment and not per tutor (with a total amount) and the TutorCommissionPercentage is not having any effect as it is adding the full amount, and not the commission amount. Any thoughts?
As a recap, the full code is this:
public ActionResult CreateBulkCommissions()
{
var month = DateTime.Now.ToString("MMMM");
var enrolments = db.Enrollments.ToList();
var newCommissions = from enrolment in enrolments
select new TutorCommission()
{
CommissionAmount = enrolment.MonthlyFee,
CommissionMonth = month, // string constant
CommissionStatus = "Unpaid",
Tutor = enrolment.Tutor
};
foreach (var newCommission in newCommissions)
{
List<TutorCommission> TutorComs = newCommissions.GroupBy(g => g.Tutor).Select(s => new TutorCommission
{
CommissionAmount = s.Sum(u => u.CommissionAmount) * s.Key.TutorCommissionPercentage,
TutorNoID = s.Key.TutorNoID
}).ToList();
db.TutorCommission.Add(newCommission);
db.SaveChanges();
}
return RedirectToAction("Index");
}
}
amecily1
Member
128 Points
83 Posts
Re: Bulk Database Entries - Commission
Oct 09, 2011 10:29 PM|LINK
I changed some of the code at the end, like this:
ThatsIT
Participant
1026 Points
388 Posts
Re: Bulk Database Entries - Commission
Oct 10, 2011 03:01 AM|LINK
We should back up. take away the <TutorCommission>() as we should not have to convert it.
Also take code out of loop.
we will use the resullt "TutorComs" in teh loop
ThatsIT
Participant
1026 Points
388 Posts
Re: Bulk Database Entries - Commission
Oct 10, 2011 03:10 AM|LINK
Change
var enrolments = db.Enrollments.ToList();
var newCommissions = from enrolment in enrolments
To
var newCommissions = from enrolment in db.Enrollments
newcommiosn should be a list, it needs toList on the end
then debug and look at newCommissions, is it a list of filled items,
amecily1
Member
128 Points
83 Posts
Re: Bulk Database Entries - Commission
Oct 10, 2011 07:37 AM|LINK
Okay, I've got this now:
public ActionResult CreateBulkCommissions() { var month = DateTime.Now.ToString("MMMM"); var newCommissions = from enrolment in db.Enrollments select new TutorCommission() { CommissionAmount = enrolment.MonthlyFee, CommissionMonth = month, // string constant CommissionStatus = "Unpaid", Tutor = new Tutor { TutorNoID = enrolment.Tutor.TutorNoID, TutorCommissionPercentage = enrolment.Tutor.TutorCommissionPercentage } }; List<TutorCommission> TutorComs = newCommissions.GroupBy(g => g.Tutor).Select(s => new TutorCommission { CommissionAmount = s.Sum(u => u.CommissionAmount) * s.Key.TutorCommissionPercentage, TutorNoID = s.Key.TutorNoID }).ToList; foreach (var newCommission in newCommissions) { db.TutorCommission.Add(newCommission); db.SaveChanges(); } return RedirectToAction("Index"); }Still gettin the same error on ToList. What must I put in the loop now?
ThatsIT
Participant
1026 Points
388 Posts
Re: Bulk Database Entries - Commission
Oct 10, 2011 07:43 AM|LINK
Well TutorComs will go in the loop. but first we much find out wy the tolist errors.
can you dubug, from the start we need to find out what in newCommissions
does it have a list of TutorCommission with the propeteis we gave it?
amecily1
Member
128 Points
83 Posts
Re: Bulk Database Entries - Commission
Oct 10, 2011 07:55 AM|LINK
How do I debug with an error? I added the <TutorCommission>() back so that I could debug. But now I'm getting that TutorCommission cannot be constructed in a linq to entities query.
ThatsIT
Participant
1026 Points
388 Posts
Re: Bulk Database Entries - Commission
Oct 10, 2011 08:01 AM|LINK
This class, is this a class you constructed, or it from your entitydiagram
public class TutorCommission
{
amecily1
Member
128 Points
83 Posts
Re: Bulk Database Entries - Commission
Oct 11, 2011 07:55 PM|LINK
Hi there, I managed to run the code and get database entries to generate by changing a line:
var newCommissions = from enrolment in enrolments select new TutorCommission() { CommissionAmount = enrolment.MonthlyFee, CommissionMonth = month, // string constant CommissionStatus = "Unpaid", Tutor = enrolment.Tutor };So I changed the last line to the above from Tutor = new Tutor { TutorNoID = enrolment.Tutor.TutorNoID, TutorCommissionPercentage = enrolment.Tutor.TutorCommissionPercentage } So there are no errors now and database entries are being added. The problem is, a database entry is added per enrollment and not per tutor (with a total amount) and the TutorCommissionPercentage is not having any effect as it is adding the full amount, and not the commission amount. Any thoughts?
As a recap, the full code is this:
public ActionResult CreateBulkCommissions() { var month = DateTime.Now.ToString("MMMM"); var enrolments = db.Enrollments.ToList(); var newCommissions = from enrolment in enrolments select new TutorCommission() { CommissionAmount = enrolment.MonthlyFee, CommissionMonth = month, // string constant CommissionStatus = "Unpaid", Tutor = enrolment.Tutor }; foreach (var newCommission in newCommissions) { List<TutorCommission> TutorComs = newCommissions.GroupBy(g => g.Tutor).Select(s => new TutorCommission { CommissionAmount = s.Sum(u => u.CommissionAmount) * s.Key.TutorCommissionPercentage, TutorNoID = s.Key.TutorNoID }).ToList(); db.TutorCommission.Add(newCommission); db.SaveChanges(); } return RedirectToAction("Index"); } }ThatsIT
Participant
1026 Points
388 Posts
Re: Bulk Database Entries - Commission
Oct 12, 2011 06:06 AM|LINK
I was under the impression that you were loading the data into a object that you created, but you ar laoding into a objcy from the entity model.
try this
public ActionResult CreateBulkCommissions()
{
var month = DateTime.Now.ToString("MMMM");
List<TutorCommission> TutorComs = db.Enrollments.GroupBy(g => new { g.Tutor ).Select(s => new TutorCommission
{
CommissionAmount = s.Sum(u => u.CommissionAmount) * s.Key.TutorCommissionPercentage,
TutorNoID = s.Key.TutorNoID ,
CommissionMonth = month, // string constant
CommissionStatus = "Unpaid"
}).ToList();
foreach (var newCommission in TutorComs
{
db.TutorCommissions.Add(newCommission);
db.SaveChanges();
}
return RedirectToAction("Index");
}