0

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.

Ethan
  • 11
  • 5

2 Answers2

1

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.

DSpirit
  • 2,062
  • 16
  • 22
0

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?
Alex AIT
  • 17,361
  • 3
  • 36
  • 73