我如何确定宇宙DB容器是空的并且需要播种?
我通过EF核心访问我的宇宙数据库数据库。
我可以通过EF Core Overriden进行启用,以检查基础数据库和容器是否存在。但是我不确定如何检查它是否包含任何数据?
EFCORE似乎没有任何计数,或者无法检查基础数据库是空的吗?
这是我到目前为止
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var client = Database.GetCosmosClient();
var dbResponse = client.CreateDatabaseIfNotExistsAsync("Db").Result.StatusCode;
ContainerProperties containerProperties = new ContainerProperties("forms", "/forms");
var database = client.GetDatabase("Db");
var containerResponse = database.CreateContainerIfNotExistsAsync(containerProperties).Result.StatusCode;
//Database or container was recently created, hence reseed
if (dbResponse == System.Net.HttpStatusCode.Created
|| containerResponse == System.Net.HttpStatusCode.Created)
{
Console.WriteLine("Model created but need seeding");
return;
}
var container = database.GetContainer("forms");
//How to check data is in the container?
}
根据@SvyAtoslav Danyli提供的答案来尝试的,我创建了两种方法。
using Microsoft.EntityFrameworkCore;
using System.Linq;
namespace FormsRetriever
{
public static class SeedingTools
{
public static void CheckDatabase(DbContext dbContext)
{
dbContext.Database.EnsureCreated();
var client = dbContext.Database.GetCosmosClient();
SeedData(dbContext);
}
private static void SeedData(DbContext dbContext)
{
bool a = dbContext.Set<Forms>().Any();
}
}
}
我打电话给初创公司。
作为回报给我这个错误
A host error has occurred during startup operation '50913447-b407-41a1-95bd-68918f9d3d4b'.
[2022-07-04T11:04:53.059Z] Microsoft.EntityFrameworkCore: The LINQ expression 'DbSet<Forms>()
[2022-07-04T11:04:53.059Z] .Any()' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
I am via EF core accessing my cosmos db database.
I have via Ef core overriden the OnModelCreating to check whether the underlying database and container exists. but I am not sure How I should check whether it contains any data?
EfCore does not seem to have any count, or no way to check whether the underlying db is empty?
This what I have tried so far
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var client = Database.GetCosmosClient();
var dbResponse = client.CreateDatabaseIfNotExistsAsync("Db").Result.StatusCode;
ContainerProperties containerProperties = new ContainerProperties("forms", "/forms");
var database = client.GetDatabase("Db");
var containerResponse = database.CreateContainerIfNotExistsAsync(containerProperties).Result.StatusCode;
//Database or container was recently created, hence reseed
if (dbResponse == System.Net.HttpStatusCode.Created
|| containerResponse == System.Net.HttpStatusCode.Created)
{
Console.WriteLine("Model created but need seeding");
return;
}
var container = database.GetContainer("forms");
//How to check data is in the container?
}
Based on the answer provided by @Svyatoslav Danyli i created two methods.
using Microsoft.EntityFrameworkCore;
using System.Linq;
namespace FormsRetriever
{
public static class SeedingTools
{
public static void CheckDatabase(DbContext dbContext)
{
dbContext.Database.EnsureCreated();
var client = dbContext.Database.GetCosmosClient();
SeedData(dbContext);
}
private static void SeedData(DbContext dbContext)
{
bool a = dbContext.Set<Forms>().Any();
}
}
}
Which I call in the startup.
which in return gives me this error
A host error has occurred during startup operation '50913447-b407-41a1-95bd-68918f9d3d4b'.
[2022-07-04T11:04:53.059Z] Microsoft.EntityFrameworkCore: The LINQ expression 'DbSet<Forms>()
[2022-07-04T11:04:53.059Z] .Any()' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将检查代码移至适当的功能并在正确配置EF Core时执行:
Move check code to appropriate function and execute when EF Core is properly configured: