'CosmosDB database first approach with Entity Framework Core
I have recently switched to Entity Framework Core with CosmosDB. Created context and tried to pull the data from existing cosmos container.
My context looks like this:
public class CosmosContext : DbContext
{
public string ConnectionString { get; set; }
public string DbName { get; set; }
public DbSet<ActivityLog> ActivityLog { get; set; }
public CosmosContext(
DbContextOptions options,
string connectionString,
string dbName) : base(options)
{
ConnectionString = connectionString;
DbName = dbName;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseCosmos(ConnectionString, databaseName: DbName);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ActivityLog>().ToContainer("ActivityLog");
}
}
Activity log like that:
public class ActivityLog
{
public string PartitionKey { get; set; }
public string CreatedDate { get; set; }
public string EntityID { get; set; }
public string ModifiedBy { get; set; }
public string CreatedBy { get; set; }
public string Action { get; set; }
public string Type { get; set; }
public string Object { get; set; }
public string id { get; set; }
}
Data in Azure currently is:
But when I try to query the data it's always null:
var ana = new CosmosContext(new DbContextOptions<CosmosContext>(),
_connectionString,
_databaseName);
var b = await ana.ActivityLog.FirstAsync();
I can create new containers just fine, however I need to work with the existing data. I'm pretty sure it's not recognized by the model binder but I cannot figure out why.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

