Reputation: 1875
I'm using Ef Core with CosmosDb provider and I want to use strongly typed Id for my Entities but my code does not work correctly. detail:
Domain:
public class Customer : Entity
{
public CustomerId Id { get; set; }
public string PartitionKey { get; set; }
public string FirstName { get; set; }
public string DocumentType { get; set; }
}
public class CustomerId : TypedId<string>
{
public CustomerId(string value) : base(value)
{
}
}
public class TypedId<TKey>
{
public TKey Value { get; }
protected TypedId(TKey value)
{
Value = value;
}
}
Config:
class CustomerConfiguration : IEntityTypeConfiguration<Customer>
{
public void Configure(EntityTypeBuilder<Customer> builder)
{
builder.HasPartitionKey(x => x.PartitionKey);
builder.Property(x => x.Id).ToJsonProperty("id");
builder.Property(x => x.Id).HasConversion(x => x.Value, x => new CustomerId((x)));
builder.HasKey(x => x.Id);
builder.Property(x => x.PartitionKey).ToJsonProperty("partitionKey");
builder.Property(x => x.FirstName).ToJsonProperty("firstName");
builder.Property(x => x.DocumentType).ToJsonProperty("documentType");
builder.HasDiscriminator(x => x.DocumentType);
}
}
after saving an entity with
id = new CustomerId(someGuid.ToString())
this is the result, but I expect a GUID as id:
{
"id": "Customer|Domain.Customers.CustomerId",
"documentType": "CustomerProfile",
"firstName": "Moji",
"partitionKey": "100-2",
"_rid": "8L5mAKVMiToMAAAAAAAAAA==",
"_self": "dbs/8L5mAA==/colls/8L5mAKVMiTo=/docs/8L5mAKVMiToMAAAAAAAAAA==/",
"_etag": "\"00000000-0000-0000-b5ac-bcf438d801d6\"",
"_attachments": "attachments/",
"_ts": 1604825656
}
does anyone have an idea where I'm wrong?
Upvotes: 0
Views: 804
Reputation: 4870
Convert datatype
of Id
from CustomerId
to string
and save the document without adding any value for Id:
public string Id { get; set; }
Cosmos DB will automatically create a GUID for you.
Upvotes: 2