.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);

}










留言

這個網誌中的熱門文章

🛠【ASP.NET Core + Oracle】解決 ORA-00904 "FALSE": 無效的 ID 錯誤與資料欄位動態插入顯示問題

🛠【實戰排除教學】從 VS Code 的 _logger 錯誤,到 PowerShell 找不到 npm/serve,再到 Oracle ORA-03135 連線中斷——一次搞懂!

🔎如何在 Oracle PL/SQL 儲存過程中為文字欄位加入換行符號(CHR(10))——以 Updlcmremark 為例