![](/img/trans.png)
[英]dotnet ef migrations: Value cannot be null. Parameter name: contentRootPath
[英]Entity Framework Core and EF6 - dotnet ef migrations add InitialCreate - Value cannot be null. (Parameter 'connectionString')
我正在嘗試將 Entity Framework Core 與 .NET 6 一起使用。
我的 Visual Studio 解決方案包含以下項目:
這個想法是代碼優先和遷移的所有數據庫相關類都存儲在Data
項目中,因此該項目安裝了 EF Core。
另一方面,我的API
項目沒有對 EF Core 的引用。
我的DbContext
類(在Data
中)配置如下:
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
namespace X
{
public class MyDbContext: DbContext
{
protected readonly IConfiguration Configuration;
public MyDbContext()
{
}
public MyDbContext(IConfiguration configuration)
{
Configuration = configuration;
}
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
// connect to SQL Server with connection string from app settings
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
}
}
}
我的 API Program
類配置如下:
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
var services = builder.Services;
var env = builder.Environment;
services.AddDbContext<MyDbContext>();
services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
services.AddEndpointsApiExplorer();
services.AddSwaggerGen();
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
services.AddCors();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseCors(x => x
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run("http://localhost:4000");
我的appsettings.json
(在 API 項目中找到)和appsettings.development.json
如下:
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=localhost;Initial Catalog=dotnet-rpg;Integrated Security=SSPI; MultipleActiveResultSets=true;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
然后我打開開發者控制台並在數據項目中運行:
dotnet ef migrations add InitialCreate
我收到此錯誤:
值不能為空。 (參數“連接字符串”)
我嘗試進行研究,大多數時候它要么是連接字符串部分位於日志下方,要么是錯誤地寫入了名稱連接字符串,所以我仍然迷路了。
有任何想法嗎?
我不完全確定,但我認為這可能會有所幫助:
你可以考慮使用這個例子
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
/*
Here you check if the database exists and if it does it does nothing because you
don't want your database to be recreated every time you update your migration list
*/
if (!optionsBuilder.IsConfigured)
{
/*
Instad of 'YourSqlInstace' you must use your sql instance
Instead of 'DatabaseName' you must write the name of your dataabse
'Trusted_Connection' is used so that your code won't have to use logins
when trying to retrieve, select, update, delete... data from the database
*/
optionsBuilder.UseSqlServer("Server=YourSqlInstace;Database=DatabaseName;Trusted_Connection=True;MultipleActiveResultSets=true");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.