Ethan
Ethan

Reputation: 11

C# - .NET- Cross partition query is required but disabled when I Get

I'm building a web API with .net core and the CosmosDb SQL API. My post method works, but when I try to get data from the database I get an error "Cross partition query is required but disabled".

I am using the entity framework core for cosmosdb sql and I found a "solution" using feedoptions to enable Cross partition, but I am not using a query the same way they did in the "solution" I found. So I have no idea where to insert the feedoptions, or if that is even the right solution for me.

Click here for the "solution" I found.

Get method in SensorController.cs:

    [HttpGet]
    public ActionResult<IEnumerable<Sensor>> Get()

    {

        var bookmarks = _sensorContext.Sensors.ToList();

        return Ok(bookmarks);

    }

SensorContext.cs:

    public SensorContext(DbContextOptions options) : base(options)
    {
    }


    public DbSet<Sensor> Sensors { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Sensor>();
        var sensors = modelBuilder.Entity<Sensor>().Metadata;
        sensors.CosmosSql().CollectionName = "Sensors";
    }

I expected to get a list of all the sensors in my database but I just get the Cross origin error instead.

Upvotes: 0

Views: 1061

Answers (2)

DSpirit
DSpirit

Reputation: 2280

Seems that you're using an old version of the CosmosDB Entity Framework. Make sure to download the latest NuGet package, which is currently:

dotnet add package Microsoft.EntityFrameworkCore.Cosmos -v 3.0.0-preview.18572.1

Afterwards, you may try out following walkthrough: Announcing Entity Framework Core 2.2 Preview 3

I managed to create and retrieve documents by executing the steps shown in the link above.

Upvotes: 1

Alex
Alex

Reputation: 18526

If your sensors data is so small that you can return all of it within one HTTP request, your probably don't need a partition key on the collection.

It is currently not possible to specify the partition key when using EntityFramework to access CosmosDB. It looks like they are considering it for Version 3 - see this Github Issue

3.0 Preview 3

  • [..]
  • API to configure document collection facets (for partition keys, RU, size)
  • Handle partition key in queries

Open issues

  • [..]
    • How should we handle queries that require feed options to execute across partitions?

Upvotes: 0

Related Questions