Entity Framework partial classes with custom attributes

asp.net c# entity-framework

Question

In a scenario when I utilize partial classes with the EF-generated classes, how should I handle a custom property?

As for the setup:

The timesheet table is where an employee's hours are kept.
the TimeSheetID (auto, int, PK)
Date of Entry (DateTime)
- Hours (int)
Employee ID (int)

The employee's current hourly wage is kept in the EmployeeHourlyRate database. There is also storage for previous exchange rates.
The RateID (int, PK)
- DateInViolation (int, PK)
- Rate (double)

The link between the EmployeeHourlyRate and the TimeSheet is one to many. I would use the maximum effectiveDate that is later than the entryDate of the timeSheet in order to determine an employee's rate.

I've created a TimeSheet partial class and given it a new property named "Rate" in order to make things easier. I want to fill it up myself using the same query that fills out my collection of TimeSheets. There is just no quick and simple solution that I am aware of.

I could, for instance, carry it out as follows:

var list = from ts in Context.TimeSheets
          ....
          select new TimeSheet() {
                 TimeSheetID = ts.TimeSheetID,
                 EntryDate = ts.EntryDate,
                 Hours = ts.Hours,
                 EmployeeID = ts.EmployeeID,
                 Rate = SomeRate   //real code has been omitted 
          };

Although this should theoretically work, the EF complains at run-time that I'm using an entity-generated class for some reason (no idea why — it works fine if I create my own custom class). Even if it worked, I would still need to keep track of the data and keep mapping from my EF to a single class, which would be an issue if/when I added more fields to the TimeSheet table. Retyping all of that information is also foolish.

Therefore, I want to know how individuals typically respond to this situation. Is there a way to implement my join rule (about choosing the right effective date depending on my EntryDate) in the datamodel so that it can manage this)?

1
0
5/11/2009 10:38:34 PM

Accepted Answer

To fully understand what you are attempting to do, I would want to see the whole LINQ query (with the "SomeRate" code), but maybe something similar to this might work:

Air code, beware.

public partial class TimeSheet
{
    public double Rate
    {
        get //Calculate your rate here... e.g.
        {
            if ((this.Employee == null) || (this.Employee.EmployeeHourlyRates.Count == 0))
                //throw an exception

            EmployeeHourlyRate maxRate;
            foreach (EmployeeHourlyRate rate in this.Employee.EmployeeHourlyRates)
            {
                if ((rate.EffectiveDate <= this.EntryDate)
                    && ((maxRate == null) || (maxRate.EffectiveDate < rate.EffectiveDate)))
                {
                    maxRate = rate;
                }
            }

            if (maxRate == null)
                //throw exception
            else
                return maxRate.Rate;
        }
    }
}

EDIT: A sample of eager loading is now included to prevent database round trips.

var list = from ts in Context.TimeSheets.Include("Employee.EmployeeHourlyRate")
           where blah blah
           select ts;
2
5/12/2009 10:43:26 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow