public ActionResult Index()
{
IRepository<Group> groupRepo = new Repository<Group>();
var k = groupRepo.GetAll().ToList();
return View("Groups");
}
使用简单的GetAll()
我正在尝试从表中获取所有数据
public class Repository<T> : IRepository<T> where T : class
{
public Repository()
{
DbContext dbContext = new MusicoDatabaseContext();
if (dbContext == null)
throw new ArgumentNullException("Null DbContext");
DbContext = dbContext;
DbSet = DbContext.Set<T>();
}
protected DbContext DbContext { get; set; }
protected DbSet<T> DbSet { get; set; }
但我仍然遇到运行时错误:
An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct.
答案 0 :(得分:1)
看起来你的连接字符串不正确。顺便说一句,连接字符串有点复杂的EF,所以这里有一个适合我(在关闭configuration
标签之前将其添加到web.config):
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="Data Source=(local);Initial Catalog=my_project_db;Integrated Security=True;MultipleActiveResultSets=True;LANGUAGE=English" />
</parameters>
</defaultConnectionFactory>
<!-- set up migrations -->
<contexts>
<context type="MyProject.MyProjectContext, MyProject">
<databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[MyProject.MyProjectContext, MyProject], [MyProject.Migrations.Configuration, MyProject]], EntityFramework" />
</context>
</contexts>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>