读sqlite 速度慢的问题
我的环境 vs2010 + sqlite.net + ef4.3 + dbcontext generator
我在程序中第一次调用数据库,不管是增改删查 都很慢(要等1-2秒), 然后的数据库操作 速度就很正常了
我找了很久 发现
http://www.cnblogs.com/osiris/articles/2013622.html
这篇文章说是
“如果你为了图省事不想建立SQLiteConnection,直接建立DataContext”的原因
如是我修改了 DbContext的创建 代码如下
1.修改一
C# code
public partial class CodeLibraryEntities : DbContext
{
private static DbCompiledModel GetDbCompiledModel()
{
string connstr = ConfigurationManager.ConnectionStrings["CodeLibraryEntities"].ConnectionString;
connstr = connstr.Remove(0, connstr.IndexOf("provider connection string=\"") + "provider connection string=\"".Length );
connstr = connstr.Remove(connstr.IndexOf("\""));
DbModelBuilder modelBuilder = new DbModelBuilder();
var conn = new System.Data.SQLite.SQLiteConnection(connstr);
DbModel model = modelBuilder.Build(conn);
return model.Compile();
}
public CodeLibraryEntities()
: base("Name=CodeLibraryEntities", GetDbCompiledModel())
{
}
。。。。。。。。。。。。。。。。。
}
报
“System.InvalidOperationException: Cannot initialize a DbContext from an entity connection string or an EntityConnection instance together with a DbCompiledModel. If an entity connection string or EntityConnection instance is used, then the model will be created from the metadata in the connection. If a DbCompiledModel is used, then the connection supplied should be a standard database connection (for example, a SqlConnection instance) rather than an entity connection.” 错误
2.修改二
C# code
public partial class CodeLibraryEntities : DbContext
{
public static System.Data.SQLite.SQLiteConnection GetSQLiteConnection()
{
string connstr = ConfigurationManager.ConnectionStrings["CodeLibraryEntities"].ConnectionString;
connstr = connstr.Remove(0, connstr.IndexOf("provider connection string=\"") + "provider connection string=\"".Length);
connstr = connstr.Remove(connstr.IndexOf("\""));
return new System.Data.SQLite.SQLiteConnection(connstr);
}
public CodeLibraryEntities()
: base(GetSQLiteConnection(), true)
{
}
。。。。。。。。。。。。。。。。。。。。。。。
}
报错
"System.Data.Entity.Infrastructure.UnintentionalCodeFirstException: Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception."
问题1. 我应该怎么改动
问题2. 这样的改动能提高操作sqlite数据库的速度吗?