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

EF Core for Cosmos DB does not support ETag for derived entities #35336

Open
valosharp opened this issue Dec 16, 2024 · 1 comment
Open

EF Core for Cosmos DB does not support ETag for derived entities #35336

valosharp opened this issue Dec 16, 2024 · 1 comment

Comments

@valosharp
Copy link

valosharp commented Dec 16, 2024

EF Core for Cosmos DB does not support ETag for derived entities.

I defined the model:

    public class Address
    {
        public string AddressId { get; set; }
        public string State { get; set; }
        public string City { get; set; }
        public string Street { get; set; }
        public string HouseNumber { get; set; }

        public string CustomETag { get; set; }
    }

    public class ExtendedAddress : Address
    {
        public string PostalCode { get; set; }
    }

I configured DbContext:

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

        public DbSet<Address> Addresses { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Address>()
              .ToContainer(nameof(Address))
              .HasPartitionKey(address => address.State)
              .HasKey(address => address.AddressId);

            modelBuilder.Entity<Address>()
              .Property(address => address.CustomETag)
              .IsETagConcurrency();

            modelBuilder.Entity<ExtendedAddress>()
                      .HasBaseType<Address>()
              .ToContainer(nameof(Address))
                      .HasPartitionKey(address => address.State);

            modelBuilder.Entity<ExtendedAddress>()
              .Property(address => address.CustomETag)
              .IsETagConcurrency();
        }
    }

I added a record to the db:

using var context = await contextFactory.CreateDbContextAsync();

context.Add(
  new ExtendedAddress
  {
      AddressId = $"{nameof(Address)}-1",
      City = "Salt Lake City",
      State = "Utah",
      Street = "Course Road",
      HouseNumber = "1234"
  });
 await context.SaveChangesAsync();

Then in a separate method, I do:

 using var context = await contextFactory.CreateDbContextAsync();
var address = await context.Addresses.FindAsync($"{nameof(Address)}-1"); 
var addressEntry = context.Entry(address); 
var addressETagPropertyName = addressEntry.Metadata.GetETagPropertyName(); 
// we expect a value, but addressETagPropertyName is null.
var addressETagProperty = addressEntry.Metadata.GetETagProperty(); 
// we expect a value, but addressETagProperty is null
address.Street = "Street from context 1";
await context1.SaveChangesAsync();

At this point I expect that address.CustomETag contains an updated value from db, but the value is not updated.
If I do the same test but with Address entity, everything works as expected and described in the documentation.

Provider and version information

EF Core version: 8.0.10
Database provider: Microsoft.EntityFrameworkCore.Cosmos 8.0.10
Target framework: .NET 8.0
Operating system:
IDE: (e.g. Visual Studio 2022 17.4)

I also tested this using .NET 6.0 and EF Core 6, and also using >NET 8.0 EF Core 9.0

@rezaghadimim
Copy link

Would it be okay if I work on this issue, @AndriySvyryd ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants