I have found several questions related to System.DateTime which cover a similar problem. However, it seemed none of them really initialized something.
I am using an sqlserverce
connection. You can find a sample application that demonstrates the problem here. I must note that I do not have this problem when using a memory database.
My Player class contains a DateTime field.
[HierarchyRoot]
internal class Player : Entity
{
public Player(Session session)
: base(session)
{
Created = DateTime.Now;
}
[Field]
public DateTime Created { get; set; }
}
I have a function to get a Player object and create a new Player object. (As I cannot access the Session object from outside the service that holds these query functions.)
public List<Player> GetPlayers(Func<Player, bool> predicate)
{
IEnumerable<Player> result = _connection.Session.Query.Execute(
q => q.All<Player>().Where(predicate));
return result.Count() > 0 ? result.ToList() : new List<Player>(0);
}
public Player GetNewPlayer()
{
return new Player(_connection.Session);
}
In my final application I have two functions which are more or less similar to this.
public void DoSomething()
{
Player p1 = _service.GetPlayers(p => p.Name.Equals("first")).FirstOrDefault();
if (p1 == null) {
p1 = _service.GetNewPlayer();
p1.Name = "first";
_service.Save();
}
Player p2 = _service.GetPlayers(p => p.Name.Equals("second")).FirstOrDefault();
if (p2 == null) {
p2 = _service.GetNewPlayer();
p2.Name = "second";
_service.Save();
}
}
When I attempt to retrieve the second player the following exception is thrown:
A first chance exception of type 'Xtensive.Orm.VersionConflictException' occurred in Xtensive.Orm.dll
Version of entity with key 'Player, (1)' differs from the expected one.
Now, from what I understand from the other issues (which are quite old and based on older DO versions) it's because an incorrect DateTime value is used to initialize this. Bugs have been filed but I have not found any bug that was fixed or any mention of a solution.
I could of course create my own private structures to handle a DateTime object properly and use the OnInitialize() override to convert those into a System.DateTime, but that seems like a lot of unneeded work.
What's the proper solution to handle DateTime objects?
asked
Apr 13 '11 at 06:22
jensen
39●9●9●13
Hello jensen,
I'll check the sample you provided. Thanks for that!