StevenRasmussen / EFCore.SqlServer.NodaTime

NodaTime support for EF Core
MIT License
105 stars 18 forks source link

Instant saved with low pression #7

Closed yellowpanda closed 4 years ago

yellowpanda commented 4 years ago

If I save a NodaTime Instant in the database then there is only 2 decimals on nanoseconds.

The test below fails with:

  Message: 
    Assert.Equal() Failure
                                     ↓ (pos 21)
    Expected: ···020-09-06T13:07:42.8196022
    Actual:   ···020-09-06T13:07:42.8200000
                                     ↑ (pos 21)

It seems to be the insert part that does not work.

    public class Test
    {
       [Fact]
        public void ShowBug()
        {
            var databaseContext = new DatabaseContext();
            databaseContext.Database.EnsureDeleted();
            databaseContext.Database.EnsureCreated();
            var customer = new Customer()
            {
                Created = SystemClock.Instance.GetCurrentInstant(),
            };

            databaseContext.Add(customer);
            databaseContext.SaveChanges();

            databaseContext.Dispose();

            //Create a new context to be sure there is no cache involved.
            databaseContext = new DatabaseContext();
            Customer customerFromDatabase = databaseContext.Customers.First();
            Assert.Equal(customer.Created.ToString("yyyy-MM-ddTHH:mm:ss.fffffff", CultureInfo.InvariantCulture), customerFromDatabase.Created.ToString("yyyy-MM-ddTHH:mm:ss.fffffff", CultureInfo.InvariantCulture));

            databaseContext.Dispose();
        }
    }

    public class DatabaseContext : DbContext
    {
        public DbSet<Customer> Customers { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlServer(@"Data Source=.;Initial Catalog=SampleDatabase;Integrated Security=True;Pooling=False"
                    , optionsBuilder => optionsBuilder.UseNodaTime()
                    );
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
        }
    }

    public class Customer
    {
        public int Id { get; set; }

        public Instant Created { get; set; }
    }

Ps. Could you in the README consider to write something about your thoughts on ZonedDateTime. I know that SQLServer does not have a data type that can hold it. Properly the reason why you did not include it.

StevenRasmussen commented 4 years ago

@yellowpanda - Thanks for reporting this. This should be fixed in the latest release 1.3.1 just published.