我正在做一个关于。net 6的udemy课程项目。无论如何,我想到的是,当我运行项目"迁移添加"时,一切都很顺利。但是,在执行删除或数据库更新时,会出现以下异常。System.InvalidOperationException: The ConnectionString property has not been initialized.
都没起作用。我将离开GitHub和一些项目代码,以防它有帮助。我还将使用——verbose保留迁移命令。GitHub上的项目。另一个细节是,我在同一个提交中下载了教授的项目,并且他的命令可以工作。我用一个比较网站检查了所有东西,项目是一样的。我也测试了使用相同版本的依赖项,但什么也没有。
使用<<p>CLI/strong>dotnet ef migrations remove --verbose
ESTIWantAppsrc" --verbose
Using assembly 'IWantApp'.
Using startup assembly 'IWantApp'.
Using application base 'C:UsersVOTOGAMESOneDriveDocumentosworkspacews curso csharp API RESTIWantAppsrcbinDebugnet6.0'.
Using working directory 'C:UsersVOTOGAMESOneDriveDocumentosworkspacews curso csharp API RESTIWantAppsrc'.
Using root namespace 'IWantApp'.
Using project directory 'C:UsersVOTOGAMESOneDriveDocumentosworkspacews curso csharp API RESTIWantAppsrc'.
Remaining arguments: .
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider in assembly 'IWantApp'...
Finding Microsoft.Extensions.Hosting service provider...
Using environment 'Development'.
Using application service provider from Microsoft.Extensions.Hosting.
Found DbContext 'ApplicationDbContext'.
Finding DbContext classes in the project...
Using context 'ApplicationDbContext'.
Finding design-time services referenced by assembly 'IWantApp'...
Finding design-time services referenced by assembly 'IWantApp'...
No referenced design-time services were found.
Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'...
Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'.
Finding IDesignTimeServices implementations in assembly 'IWantApp'...
No design-time services were found.
System.InvalidOperationException: The ConnectionString property has not been initialized.
at Microsoft.Data.SqlClient.SqlConnection.PermissionDemand()
at Microsoft.Data.SqlClient.SqlConnectionFactory.PermissionDemand(DbConnection outerConnection)
at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry, SqlConnectionOverrides overrides)
at Microsoft.Data.SqlClient.SqlConnection.Open(SqlConnectionOverrides overrides)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerConnection.OpenDbConnection(Boolean errorsExpected)
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternal(Boolean errorsExpected)
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.<>c__DisplayClass18_0.<Exists>b__0(DateTime giveUp)
at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.<>c__DisplayClass12_0`2.<Execute>b__0(DbContext _, TState s)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, TState state, Func`2 operation, Func`2 verifySucceeded)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.Exists(Boolean retryOnNotExists)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.Exists()
at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.GetAppliedMigrations()
at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.RemoveMigration(String projectDir, String rootNamespace, Boolean force, String language)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.RemoveMigration(String contextType, Boolean force)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.RemoveMigrationImpl(String contextType, Boolean force)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.RemoveMigration.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
The ConnectionString property has not been initialized.
ApplicationDbContext
using IWantApp.Domain.Products;
using Microsoft.EntityFrameworkCore;
namespace IWantApp.Infra.Data;
public class ApplicationDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<Category> Categories { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Product>()
.Property(p => p.Name).IsRequired();
builder.Entity<Product>()
.Property(p => p.Description).HasMaxLength(255);
builder.Entity<Category>()
.Property(c => c.Name).IsRequired();
}
protected override void ConfigureConventions(ModelConfigurationBuilder configuration)
{
configuration.Properties<string>()
.HaveMaxLength(100);
}
}
appsettings.Development.json
{
"Database": {
"IWantDb": "Server=localhost;Database=IWantDb;User Id=sa;Password=123456;MultipleActiveResultSets=true;Encrypt=YES;TrustServerCertificate=YES"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
Program.cs
using IWantApp.Endpoints.Categories;
using IWantApp.Infra.Data;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSqlServer<ApplicationDbContext>(builder.Configuration["ConnectionString:IWantDb"]);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.MapMethods(CategoryPost.Template, CategoryPost.Methods, CategoryPost.Handle);
app.MapMethods(CategoryGetAll.Template, CategoryGetAll.Methods, CategoryGetAll.Handle);
app.MapMethods(CategoryPut.Template, CategoryPut.Methods, CategoryPut.Handle);
app.Run();
我已经更新了所有的项目依赖关系,并遵循了我在这里找到的一些解决方案,像这样和文档中的其他解决方案。
我已经检查了你的github你在做这个
builder.Services.AddSqlServer<ApplicationDbContext>(builder.Configuration["ConnectionString:IWantDb"]);
但是你的连接字符串实际上有不同的路径
"Database": {
"IWantDb": "Server=localhost;Database=IWantDb;User Id=sa;Password=123456;MultipleActiveResultSets=true;Encrypt=YES;TrustServerCertificate=YES"
},
你应该把ConnectionString:IWantDb
改为Database:IWantDb