C#, Entity Framework 6, 繼承DbContext

  1. using System;
  2. using System.Data.Entity;
  3.  
  4. public class MyDbContext : DbContext
  5. {
  6. public MyDbContext() : base("YourConnectionString")
  7. {
  8. // 在建構子中指定資料庫連線字串,替換 "YourConnectionString" 為你的實際連線字串
  9. // 禁用自動遷移初始化 __MigrationHistory 表格
  10. Database.SetInitializerMyDbContext>(null);
  11. }
  12.  
  13. public DbSet<Student> Students { get; set; } // 這是資料庫中的一個表格
  14.  
  15. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  16. {
  17. // 在這裡可以進行資料庫模型的配置,例如設定索引、關聯等等
  18. base.OnModelCreating(modelBuilder);
  19. }
  20. }
  21.  

留言

這個網誌中的熱門文章

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

Oracle 工作排程 DBMS_JOB 筆記

Oracle 例外控制(Exception Control)