C#, Entity Framework 6, 繼承DbContext

using System;
using System.Data.Entity;

public class MyDbContext : DbContext
{
    public MyDbContext() : base("YourConnectionString")
    {
        // 在建構子中指定資料庫連線字串,替換 "YourConnectionString" 為你的實際連線字串
        
        // 禁用自動遷移初始化 __MigrationHistory 表格
        Database.SetInitializer<MyDbContext>(null);
    }

    public DbSet<Student> Students { get; set; } // 這是資料庫中的一個表格

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // 在這裡可以進行資料庫模型的配置,例如設定索引、關聯等等
        base.OnModelCreating(modelBuilder);
    }
}

留言

這個網誌中的熱門文章

ORA-12514: TNS: 監聽器目前不知道連線描述區中要求的服務

Oracle 例外控制(Exception Control)

Oracle 工作排程 DBMS_JOB 筆記