-1
一個新的DbContext獲取數據我添加了一個新的DbContext到我的項目是這樣的:從使用LINQ
[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser>().ToTable("myapp_users");
modelBuilder.Entity<IdentityRole>().ToTable("myapp_roles");
modelBuilder.Entity<IdentityUserRole>().ToTable("myapp_userroles");
modelBuilder.Entity<IdentityUserClaim>().ToTable("myapp_userclaims");
modelBuilder.Entity<IdentityUserLogin>().ToTable("myapp_userlogins");
}
}
[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class MyDbContext : DbContext
{
public MyDbContext()
: base("TestConnection")
{
}
public static MyDbContext Create()
{
return new MyDbContext();
}
}
但是當我嘗試用這個命令得到一些數據:
MyDbContext myContext = new MyDbContext();
var products = (from p in myContext.Products
select p).ToList();
我有此錯誤:
'MyDbContext' does not contain a definition for 'Products' and no extension method 'Products' accepting a first argument of type 'MyDbContext' could be found (are you missing a using directive or an assembly reference?)
對此有何幫助?如何從新的上下文的數據庫中的表中獲取數據?
'IDbSet Products'在哪裏? –
@HristoYankov我沒有它。我應該爲我在數據庫中的每個表創建新模型嗎? –
我假設你要'代碼優先',對嗎?然後是的,你需要一個'Product'類,你必須添加'public IDbSet Products {get;組; }'在你的db上下文類中。 –