EntityFramework6.Npgsql icon indicating copy to clipboard operation
EntityFramework6.Npgsql copied to clipboard

EntityFramework: 'Attach' changes value of DateTime fields

Open roji opened this issue 9 years ago • 3 comments

From @alexus1024 on January 27, 2014 9:39

This test demonstrates shifting of DateTime fields for -X hours where X is the current time zone.

[Test]
public void DateTimeIssue()
{
    var createdDate = DateTime.Now;
    var id = new Guid("AB51DED2-0264-4C32-B755-27CA15214A8E");

    using (var db = Db.GetContext()) // Db.GetContext() returns instance of db context
    {
        var r = new Report { CreateDate = createdDate, Id = id };
        db.Reports.Add(r);
        db.SaveChanges();
    }

    using (var db = Db.GetContext())
    {
        var r = db.Reports.Find(id);
        db.AttachAsModified(r); // this method will be described below
        db.SaveChanges();
    }

    using (var db = Db.GetContext())
    {
        var r = db.Reports.Find(id);

        var newDate = r.CreateDate;
        Assert.AreEqual(createdDate, newDate.ToLocalTime()); // ToLocalTime because I send local time but recive utc time
    }
}

AttachAsModified definition:

        public static TEntity AttachAsModified<TEntity>(this IIbsscDbContext ctx, TEntity entity)
            where TEntity : BaseEntity
        {
            ctx.Set<TEntity>().Attach(entity); // Same as DbContext.Set
            ctx.Entry(entity).State = EntityState.Modified; // Same as DbContext.Entry(...)
            return entity;
        }

Ok, I know that Attach here is not needed. But this code reveals buggy spot with processing DateTime with time zones, I think.

And maybe, when time zone in db entry and client local time zone are equal, ORM shoud return DateTime in DateTimeKind.Local form? Now it always have Utc form.

versions:

<package id="EntityFramework" version="5.0.0" targetFramework="net45" />
   <package id="Npgsql" version="2.1.0-beta1" targetFramework="net45" />
  <package id="Npgsql.EntityFrameworkLegacy" version="2.1.0-beta1" targetFramework="net45" />
  <package id="NUnit" version="2.6.3" targetFramework="net45" />
CREATE TABLE "Reports"
(
  "Id" uuid NOT NULL,
  "CreateDate" timestamp with time zone,
...
)

Copied from original issue: npgsql/npgsql#150

roji avatar May 12 '16 12:05 roji

From @mattsteinRRD on February 11, 2014 19:38

I added a comment to issue #147 that may be related to this. Also, I just ran into a problem using EF that may also be related. When I try to use the "Min()" aggregate function in a LINQ query (using group by) that tries to select the minimum value from a timestamptz column, I get a NotSupportedException. If I add the following lines to the "GetDbType" method in the SqlBaseGenerator class, the problem is fixed:

case PrimitiveTypeKind.DateTimeOffset:
return "timestamptz";

I hope this helps in case anyone is paying attention.

roji avatar May 12 '16 12:05 roji

From @franciscojunior on February 11, 2014 21:0

Hi, @mattsteinRRD ! Thanks for your feedback! We are slowly fixing the many bugs which exist in our current implementation. I hope that when we merge #91 we will start to fix more bugs because it adds support for entity framework tests.

roji avatar May 12 '16 12:05 roji

Thank you for the heads up!

On Thu, May 12, 2016 at 7:45 AM, Shay Rojansky [email protected] wrote:

From @mattsteinRRD https://github.com/mattsteinRRD on February 11, 2014 19:38

I added a comment to issue #147 that may be related to this. Also, I just ran into a problem using EF that may also be related. When I try to use the "Min()" aggregate function in a LINQ query (using group by) that tries to select the minimum value from a timestamptz column, I get a NotSupportedException. If I add the following lines to the "GetDbType" method in the SqlBaseGenerator class, the problem is fixed:

case PrimitiveTypeKind.DateTimeOffset:

return "timestamptz";

I hope this helps in case anyone is paying attention.

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/npgsql/EntityFramework6.Npgsql/issues/1#issuecomment-218745791

Thanks, Matt

mattsteinRRD avatar May 12 '16 14:05 mattsteinRRD