Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
XzaR90 opened this issue Nov 15, 2021 · 1 comment
Closed
Labels
area-cosmos area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression Servicing-approved type-bug
Milestone

Comments

@XzaR90
Copy link

XzaR90 commented Nov 15, 2021

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

@ajcvickers
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-cosmos area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression Servicing-approved type-bug
Projects
None yet
Development

No branches or pull requests

3 participants