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

Cosmos: Support unique keys #17305

Open
AndriySvyryd opened this issue Aug 20, 2019 · 2 comments
Open

Cosmos: Support unique keys #17305

AndriySvyryd opened this issue Aug 20, 2019 · 2 comments

Comments

@AndriySvyryd
Copy link
Member

AndriySvyryd commented Aug 20, 2019

https://docs.microsoft.com/en-us/azure/cosmos-db/unique-keys

For a unique key to be created in the container the EF model must have an alternate key on an entity type that includes the partition key and all entity types sharing the container must have the same key.

@jviau
Copy link
Contributor

jviau commented Nov 22, 2019

Will this feature also cover having the partition key be part of the lookup in DbSet<T>.Find(...)? I mean without specifically adding the partition key to be part of the complex key.

To be explicit:

public class MyModel
{
    public string Id { get; set; }

    public string PartitionKey { get; set; }
}

public class MyDbContext : DbContext 
{
    public DbSet<MyModel> MyModels { get; set; }
        
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<MyModel>()
                .HasPartitionKey(x => x.PartitionKey)
                .HasKey(x => x.Id);
    }
}


// Find model:
MyDbContext dbContext;
MyModel model = dbContext.MyModels.Find("partitionKeyValue", "idValue"); // not two separate values are passed in, but the "key" only has 1 value.

In cosmosdb, it would be preferable the PartitionKey is not part of the Id, as I believe cosmosdb would allow the same id under different partition keys already.

{
  "Id": "idValue",
  "PartitionKey": "partitionKeyValue",
   "Discriminator": "MyModel",
   "id": "MyModel|idValue"
}

Right now I am not sure how cosmos ef core would behave if I have the same id under different partition keys and try to execute Find("idValue")

@AndriySvyryd
Copy link
Member Author

@jviau This is tracked by #17310

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