2016-09-29 36 views
0

這是我的代碼(Model);使用MVC控制器實體框架創建控制器時的「不支持的上下文類型」。 MVC4

public class XpsEntity 
    { 
    public DbSet<AModel> A { get; set; } 
    public DbSet<TModel> T { get; set; } 

    public class SDbContext : DbContext 
    { 
     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
      modelBuilder.Entity<XpsEntity>().ToTable("Table1"); 
      modelBuilder.Entity<XpsEntity>().ToTable("Table2"); 

     } 
    } 

我的編碼錯了嗎?因爲每次我使用MVC Controller Entity Framework創建一個控制器。我總是得到「不支持的上下文類型」錯誤。

Here is the screenshot for adding the controller.

+0

那些dbset屬性需要嵌套在派生類中 – Nkosi

+0

究竟你是如何「創造一個控制器」一的DbContext? – Mats

回答

0

DbSet屬性需要嵌套在一個DbContext派生類

public class SDbContext : DbContext { 
    protected override void OnModelCreating(DbModelBuilder modelBuilder) { 
     modelBuilder.Entity<AModel>().ToTable("Table1"); 
     modelBuilder.Entity<TModel>().ToTable("Table2"); 
    } 

    public DbSet<AModel> A { get; set; } 
    public DbSet<TModel> T { get; set; } 
} 
+0

仍爲「不支持的上下文類型」錯誤。 – Nosbig

+0

更新您的問題,更詳細地瞭解您如何獲取上下文和模型設置 – Nkosi

+0

當我創建一個控制器時,這些是參數;模型類:AModel(MvcApplication4.Models),數據上下文類:XpsEntity(MvcApplication4.Models),視圖:Razor。 – Nosbig

相關問題