.Net Core C# Program.cs問題
1. 確保正確引用命名空間
在Program.cs檔案的頂部,加入ProjectVersion類別所在命名空間的參考。根據你提供的程式碼,ProjectVersion位於Net.Model命名空間:
using Net.Model;
2. 確保有一個繼承自DbContext的類
using Microsoft.EntityFrameworkCore;
using Net.Model; // 確保引入ProjectVersion的命名空間
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<ProjectVersion> ProjectVersions { get; set; }
}
3.更新Program.cs
中的DbContext
配置
在Program.cs中,將引用ProjectVersion的地方改為引用新建立的ApplicationDbContext:
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseOracle(builder.Configuration.GetConnectionString("OracleDB")));
這樣,你就正確配置了DbContext和實體類別。完整的修改後的Program.cs檔案看起來應該是這樣:
using Microsoft.EntityFrameworkCore;
using Net.Model; // 引入命名空間,包含實體類別和DbContext
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<ApplicationDbContext>(options => // 使用正確的DbContext
options.UseOracle(builder.Configuration.GetConnectionString("OracleDB")));
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
app.MapGet("/weatherforecast", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast");
app.Run();
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
留言
張貼留言