.NET ASP Coreer容器中的Sqlite数据库为空

问题描述 投票:0回答:1

我正在尝试将.NET ASP Core Web应用程序包装在Docker容器中,但Sqlite数据库在容器中似乎是空的。

在Controller中,我执行以下查询:

using (var connection = new SQLiteConnection(_connectionString))
{
    connection.Open();
    using (var cmd = new SQLiteCommand("SELECT COUNT(*) FROM sqlite_master WHERE type='table';", connection))
    {
        result = (Int64)cmd.ExecuteScalar();
    }
}

其中_connectionString的定义如下:

private static readonly string _database = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "LocalDb\\v1\\sqlitedb.db");

Dockerfile:

FROM microsoft/dotnet:sdk AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM microsoft/dotnet:aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "MyApp.dll"]

我用docker build -t MyApp:dev .构建并与docker run -p 8080:80 -e "ASPNETCORE_ENVIRONMENT=Development" MyApp:dev一起运行

当我在Visual Studio中使用IIS运行应用程序时,我得到预期数量的表,但是在docker中它返回0.此外,当我查询表时,我得到一个SQLiteException: SQL logic error no such table错误。

当我进入容器的SSH时,我可以看到数据库文件是我期望的:在app/LocalDb/v1/

有什么问题可能是什么?

asp.net sqlite docker
1个回答
1
投票

发现了问题。该容器是基于Linux的,并且无法识别带有反斜杠的.db文件的文件路径。由于某种原因,它创建了一个名为LocalDb\v1\sqlitedb.db的新文件(不是目录,具有该名称的实际文件)。改变正斜杠的路径解决了它。

© www.soinside.com 2019 - 2024. All rights reserved.