.NET6无法识别Azure Data Studio中的数据库

nhhxz33t  于 2023-05-19  发布在  .NET
关注(0)|答案(1)|浏览(164)

我正在尝试连接到. NET6中的应用程序的数据库。我的连接字符串如下

"ConnectionStrings": {
        "Default": "Server=localhost;Database=Tim;User Id=SA;Password=*****;TrustServerCertificate=true"

    }

它在appsettings.json中定义。
在azure data studio中,我有一个名为Tim的数据库,我正在尝试访问它

但是当我运行我的应用程序时,我得到了以下错误

SqlException: Invalid object name 'Tim'.
Microsoft.Data.SqlClient.SqlCommand+<>c.<ExecuteDbDataReaderAsync>b__209_0(Task<SqlDataReader> result)
System.Threading.Tasks.ContinuationResultTaskFromResultTask<TAntecedentResult, TResult>.InnerInvoke()
System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, object state)
System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, object state)
System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref Task currentTaskSlot, Thread threadPoolThread)
Dapper.SqlMapper.QueryAsync<T>(IDbConnection cnn, Type effectiveType, CommandDefinition command) in SqlMapper.Async.cs
DataLibrary.DB.SqlDb.LoadData<T, U>(string storedProcedure, U parameters, string connectionStringName)
RPDemoApp.Pages.Orders.CreateModel.OnGet() in Create.cshtml.cs
+
            var food = await _foodData.GetFood();
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.ExecutorFactory+NonGenericTaskHandlerMethod.Execute(object receiver, object[] arguments)
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeHandlerMethodAsync()
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeNextPageFilterAsync()
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Rethrow(PageHandlerExecutedContext context)
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

我犯了什么错?
编辑:这是我的服务收集器代码

builder.Services.AddSingleton(new ConnectionStringData
{
    SqlConnectionName= "DefaultConnection"
});

builder.Services.AddSingleton<IDataAccess, SqlDb>();
builder.Services.AddSingleton<IFoodData, FoodData>();
builder.Services.AddSingleton<IOrderData, OrderData>();

这里是GetFood方法,它使用一个存储过程从数据库中获取数据。

public Task<List<FoodModel>> GetFood()
        {
            return _dataAccess.LoadData<FoodModel, dynamic>("dbo.spFood_All",
                                                            new { },
                                                            _connectionString.SqlConnectionName);

        }
    ```
nfg76nw0

nfg76nw01#

在设置文件中,将Connection更改为DefaultConnection,将Source更改为Data Source,将Database更改为Initial Catalog,如下所示:

"ConnectionStrings": {
    "DefaultConnection": "Data Source=localhost;Initial Catalog=Tim;User Id=sa;Password=Password;TrustServerCertificate=true;"
  },

更新:

该错误与存储过程代码100%相关。选择引用的是数据库而不是表。请将存储过程select语句更改为使用表名。你也可以使用[DBNAME].[SCHEMA].[TABLENAME]这样的东西:

GO CREATE PROCEDURE [dbo].[spFood_All] AS BEGIN
set nocount on;
select [ID], [Title], [Descrip], [Price] from Tim.dbo.[TABLENAME]; 
END GO

相关问题