我如何确定宇宙DB容器是空的并且需要播种?

发布于 2025-02-10 23:49:31 字数 2215 浏览 3 评论 0原文

我通过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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

睫毛上残留的泪 2025-02-17 23:49:31

将检查代码移至适当的功能并在正确配置EF Core时执行:

public static class DatabaseTools
{
    public static void CheckDatabase(DbContext context)
    {
        // EF Core will create database and containters
        context.Database.EnsureCreated();

        // Cosmos provider do not supports Any
        if (dbContext.Set<Form>().FirstOrDefault() == null)
        {
            // seed data
            context.Set<Form>().AddRange(
                new []
                {
                    new Form{},
                    new Form{},
                    new Form{},
                }
            );

            context.SaveChanges();
        }
    }
}

Move check code to appropriate function and execute when EF Core is properly configured:

public static class DatabaseTools
{
    public static void CheckDatabase(DbContext context)
    {
        // EF Core will create database and containters
        context.Database.EnsureCreated();

        // Cosmos provider do not supports Any
        if (dbContext.Set<Form>().FirstOrDefault() == null)
        {
            // seed data
            context.Set<Form>().AddRange(
                new []
                {
                    new Form{},
                    new Form{},
                    new Form{},
                }
            );

            context.SaveChanges();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文