fellows C hashtag programmers.
So... I have this strange problem, I don't even know to describe it properly. Let's begin.
I have my Unit model:
public class Unit {
public int UnitID { get; set; }
public string Name { get; set; }
public int Version { get; set; }
}
Looks simple, but UnitID and Version together are my primary key for various reasons. When you edit Unit, it just creates new version with same UnitID, but increase Version. For example:
+--------+---------+---------+
| UnitID | Name | Version |
+--------+---------+---------+
| 1 | Unit1 | 1 |
| 1 | Unit1 | 2 |
+--------+---------+---------+
And now my model, which is using Unit class:
public class WorkerUnit {
public int WorkerID { get; set; }
public string Worker { get; set; }
public int UnitID { get; set; }
public Unit Unit { get; set; }
}
I want to get Unit with newest version, when I ask Worker for his Unit. Something like this.
_context.Workers.SingleOrDefault(w => w.WorkerID == 1).Unit;
How can I resolve this? Maybe should I add some method to Unit model, which return newest Unit. Like:
public Unit GetNewestUnitWithThisID() { }