-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
EF9 Cosmos provider does not map a property with HasKey()
to the db id
property
#35325
Comments
I can't reproduce the problem. Please see the below minimal console program which works for me for this scenario. Please tweak it to show it failing, or submit a similar code sample which illustrates the problem. Attempted reproawait using var context = new BlogContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
context.PlanningMovements.Add(new PlanningMovement
{
MovementId = new Guid("a456bf06-89c3-458d-93d8-ce21eeb1790b"),
Name = "foo",
});
await context.SaveChangesAsync();
_ = await context.PlanningMovements.ToListAsync();
public class BlogContext : DbContext
{
public DbSet<PlanningMovement> PlanningMovements { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseCosmos(
"https://192.168.64.6:8081",
"C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
"Test",
o => o.HttpClientFactory(() => new HttpClient(
new HttpClientHandler
{
ServerCertificateCustomValidationCallback =
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
}))
.ConnectionMode(ConnectionMode.Gateway))
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PlanningMovement>(builder =>
{
builder.ToContainer("test")
.HasNoDiscriminator()
.HasPartitionKey(m => m.MovementId)
.HasKey(m => m.MovementId);
builder.UseETagConcurrency();
});
}
}
public class PlanningMovement
{
public Guid MovementId { get; set; }
public string Name { get; set; }
} |
This is strange behavior (for me) coming from EF8. Here's the repro that breaks. Only changed your example to add a Reprousing Microsoft.Azure.Cosmos;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System.Text.Json;
await using var context = new BlogContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
context.PlanningMovements.Add(new PlanningMovement
{
MovementId = new Guid("a456bf06-89c3-458d-93d8-ce21eeb1790b"),
Name = "foo",
});
await context.SaveChangesAsync();
var movements = await context.PlanningMovements.ToListAsync();
Console.WriteLine(JsonSerializer.Serialize(movements));
public class BlogContext : DbContext
{
public DbSet<PlanningMovement> PlanningMovements { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseCosmos(
"https://localhost:8081",
"C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
"Test",
o => o.HttpClientFactory(() => new HttpClient(
new HttpClientHandler
{
ServerCertificateCustomValidationCallback =
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
}))
.ConnectionMode(ConnectionMode.Gateway))
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PlanningMovement>(builder =>
{
builder.ToContainer("test")
.HasNoDiscriminator()
.HasPartitionKey(m => m.MovementId)
.HasKey(m => m.MovementId);
builder.UseETagConcurrency();
builder.Property(m => m.MovementId)
.ToJsonProperty("movementId");
});
}
}
public class PlanningMovement
{
public Guid MovementId { get; set; }
public string Name { get; set; }
} From your example, here's what the saved entity looks like. There's no It looks OK when serialized however, but doesn't work with our existing data model. Incidentally we did manage to workaround the issue yesterday by applying the In our model, |
@ErikPilsits-RJW Add modelBuilder.Entity<PlanningMovement>(builder =>
{
builder.ToContainer(PlanningMovementContainerName)
.HasNoDiscriminator()
.HasPartitionKey(m => m.MovementId)
.HasKey(m => m.MovementId);
builder.UseETagConcurrency();
builder.HasShadowId();
builder.Property(m => m.MovementId)
.ToJsonProperty("movementId");
}); |
File a bug
Prior to EF9, the Cosmos provider would map an entity property that was set with
.HasKey()
to the cosmos document'sid
property, as long as anotherId
or<entity>Id
property did not exist. In EF9 this has changed and we're getting an exception for entities that don't have an explicitId
property.The entity type 'PlanningMovement' does not have a property mapped to the 'id' property in the database. Add a property mapped to 'id'.
Include your code
The entity in question does not have an explicit
Id
property. Here is the db context configuration we've been using prior to EF9. Our partition key is configured as/movementId
which is the reason for the json name mapping.Include stack traces
Include provider and version information
EF Core version: 9.0.0
Database provider: Microsoft.EntityFrameworkCore.Cosmos
Target framework: .NET 8.0
Operating system: Windows 10
IDE: Visual Studio 2022 17.12.3
The text was updated successfully, but these errors were encountered: