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);
- }
- }
留言
張貼留言