Net Core 3.1 Beta - Error query DateTimeOffset property when using Execution Strategy — oracle-tech

    Forum Stats

  • 3,708,745 Users
  • 2,241,117 Discussions
  • 7,840,574 Comments

Discussions

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Net Core 3.1 Beta - Error query DateTimeOffset property when using Execution Strategy

MikelNovikovMikelNovikov Posts: 2
edited July 2020 in ODP.NET

Hi! I'm using 3.19.0-beta1 and experience strange error when using custom Execution Strategy. Here is a test to reproduce it:

namespace EfCoreOracleTest

{

    [TestFixture]

    public class DateTimeOffsetTest

    {

        public class TestEntity

        {

            [Key]

            [Column("ID")]

            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]

            public long Id { get; protected set; }

            [Column("OPERATION_START_DATE")]

            public DateTimeOffset OperationStartDate { get; set; }

        }

        public class TestDbContext : DbContext

        {

            public TestDbContext(DbContextOptions options)

                : base(options)

            {

            }

            public DbSet<TestEntity> TestEntities { get; set; }

        }

        public class MyExecutionStrategy : ExecutionStrategy

        {

            public MyExecutionStrategy(DbContext context, int maxRetryCount, TimeSpan maxRetryDelay)

                : base(context, maxRetryCount, maxRetryDelay)

            {

            }

            protected override bool ShouldRetryOn(Exception exception) => false;

        }

        private Func<TestDbContext> _contextCreator;

        [SetUp]

        public void SetUp()

        {

            var options = new DbContextOptionsBuilder<TestDbContext>()

                .UseOracle(

                    "Data Source=....",

                    pp =>

                    {

                        // if comment this line test will succeed

                        pp.ExecutionStrategy(_ => new MyExecutionStrategy(_.CurrentContext.Context, 1, TimeSpan.Zero));

                    })

                .EnableSensitiveDataLogging(true)

                .Options;

            _contextCreator = () => new TestDbContext(options);

            using var context = _contextCreator();

            context.Database.EnsureDeleted();

            context.Database.EnsureCreated();

        }

        [Test]

        public async Task TestDateTimeOffset()

        {

            using (var context = _contextCreator())

            {

                context.TestEntities.Add(new TestEntity

                {

                    OperationStartDate = DateTimeOffset.Now,

                });

                context.SaveChanges();

            }

            using (var context = _contextCreator())

            {

                var entity = await context.TestEntities.AsQueryable().Where(e => e.Id > 0).FirstOrDefaultAsync();

                Assert.NotNull(entity);

                Assert.AreEqual(DateTime.Today, entity.OperationStartDate.UtcDateTime.Date);

            }

        }

    }

}

Error says:

  Message:

    System.InvalidCastException : Unable to cast object of type 'System.DateTime' to type 'System.DateTimeOffset'.

  Stack Trace:

    DbDataReader.GetFieldValue[T](Int32 ordinal)

If I remove Execution Strategy "pp.ExecutionStrategy(_ => new MyExecutionStrategy(_.CurrentContext.Context, 1, TimeSpan.Zero));", test will pass.

Tagged:

Answers

  • Alex Keh-OracleAlex Keh-Oracle Posts: 2,720 Employee
    edited July 2020

    Oracle fixed a couple time-related data type bugs in the EF Core 3.1 beta 1 that will be part of the Beta 2, which will probably be released next week. I would try then with the Beta 2 to see if the problem still exists.

  • MikelNovikovMikelNovikov Posts: 2
    edited July 2020

    Check with 3.19.0-beta2, problem still exists

  • Alex Keh-OracleAlex Keh-Oracle Posts: 2,720 Employee
    edited July 2020

    I was able to reproduce the error with the beta 2 using your test case. Thanks for letting us know. I filed Bug 31681498 to have the dev team investigate further to identify the root cause.

Sign In or Register to comment.