dotnet/efcore

[6.0][CosmosDB] DateTime with z suffix is not respected when reading entities.

XzaR90 opened this issue · 1 comments

File a bug

I am saving a DateTime using UTC and the document in the database get the z suffix but when I read the entity again it will have the local time, I did not have this behavior in .NET 5.

Include your code

using Microsoft.EntityFrameworkCore;

    public class AppDbContext : DbContext
    {
        public AppDbContext(DbContextOptions<AppDbContext> options)
        : base(options)
        {
        }

        public DbSet<TestEntry> TestEntries { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            var testEntryBuilder = modelBuilder.Entity<TestEntry>();
            testEntryBuilder.HasKey(e => new { e.PartitionKey, e.Id });
            testEntryBuilder.Property(e => e.ETag).IsETagConcurrency();
            testEntryBuilder.HasPartitionKey(e => e.PartitionKey);
        }
    }

    public class TestEntry
    {
        public static string CreatePartitionKey(string transactionId) => $"{transactionId}_test";
        public TestEntry() : base()
        {
            Id = Guid.NewGuid().ToString();
        }

        public string Id { get; set; }
        public string PartitionKey { get; set; }
        public string ETag { get; set; }
        public bool Confirmed { get; set; }
        public DateTime Updated { get; set; }
        public string TestValue { get; set; }
    }

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddApplicationInsightsTelemetry();
builder.Services.AddDbContext<AppDbContext>(opt =>
{
    opt.UseCosmos(builder.Configuration.GetConnectionString("DefaultConnection"), "WebTests");
});

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
}

app.UseSwagger();
app.MapGet("/", context =>
{
    context.Response.Redirect("./swagger/index.html", permanent: false);
    return Task.FromResult(0);
});

app.MapGet("/hello", () => "Hello World!");

app.MapGet("/test", async (HttpContext context, AppDbContext db) =>
{
    // Tested https://github.com/dotnet/efcore/issues/26627

    // New issue
    await db.Database.EnsureCreatedAsync();
    var dateVal = DateTime.UtcNow;
    var entry = new TestEntry()
    {
        PartitionKey = TestEntry.CreatePartitionKey("DateTest"),
        Id = "DateTest",
        Updated = dateVal,
        TestValue = dateVal.ToString()
    };
    db.Add(entry);
    await db.SaveChangesAsync();
    return JsonSerializer.Serialize(entry);
});

app.MapGet("/test-result", async (HttpContext context, AppDbContext db) =>
{
    var result = await db.TestEntries.FindAsync(TestEntry.CreatePartitionKey("DateTest"), "DateTest");
    return JsonSerializer.Serialize(result);
});

app.UseSwaggerUI();
await app.RunAsync();
<ItemGroup> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.19.0" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Cosmos" Version="6.0.0" /> </ItemGroup>

Include verbose output

{"Id":"DateTest","PartitionKey":"DateTest_test","ETag":"\u002200004429-0000-0c00-0000-61926b0b0000\u0022","Confirmed":false,"Updated":"2021-11-14T17:13:30.0612151+01:00","TestValue":"2021-11-14 16:13:30"}

Include provider and version information

EF Core version: 6.0.0
Database provider: Microsoft.EntityFrameworkCore.Cosmos
Target framework: .NET 6.0
Operating system: Windows 10
IDE: Visual Studio 2022

FYI for those impacted by this issue: EF Core 6.0.1 is now available from NuGet.