[英]SQLite Entity Framework without App.config
It is necessary to use the SQLite Entity Framework Database-first approach for the 3d-party application plugin. 对于3d-party应用程序插件,必须使用SQLite实体框架数据库优先方法。 I searched all the Internet, including Add a DbProviderFactory without an App.Config , Problems using Entity Framework 6 and SQLite and many other.
我搜索了所有的互联网,包括添加没有App.Config的DbProviderFactory , 使用Entity Framework 6和SQLite的问题等等。 I have tried to use them all in different ways and combinations, but nothing helps:
我试图以不同的方式和组合使用它们,但没有任何帮助:
"An unhandled exception of type 'System.Data.Entity.Core.MetadataException' occurred in mscorlib.dll. Additional information: Schema specified is not valid. Errors: AutosuggestModel.ssdl (2,2): error 0152: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SQLite.EF6'. Make sure the provider is registered in the 'entityFramework' section of the application config file."
“mscorlib.dll中发生了'System.Data.Entity.Core.MetadataException'类型的未处理异常。附加信息:指定的模式无效。错误:AutosuggestModel.ssdl(2,2):错误0152:没有实体框架提供程序在ADO.NET提供程序中找到了名为'System.Data.SQLite.EF6'的不变名。确保提供程序已在应用程序配置文件的'entityFramework'部分中注册。“
There is a test console application in the solution. 解决方案中有一个测试控制台应用程序。 With this minimal App.config it works:
使用这个最小的App.config它可以工作:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<providers>
<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
</DbProviderFactories>
</system.data>
</configuration>
The connection string has already implemented in the code. 连接字符串已在代码中实现。 Used packages are:
二手包是:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.1.3" targetFramework="net452" />
<package id="System.Data.SQLite" version="1.0.98.1" targetFramework="net452" />
<package id="System.Data.SQLite.Core" version="1.0.98.1" targetFramework="net452" />
<package id="System.Data.SQLite.EF6" version="1.0.98.1" targetFramework="net452" />
<package id="System.Data.SQLite.Linq" version="1.0.98.1" targetFramework="net452" />
</packages>
Please, give all the required code and specify where to insert it. 请提供所有必需的代码并指定插入位置。 Thanks in advance.
提前致谢。
Here is a sample code that illustrates how to achieve the goal. 这是一个示例代码,说明了如何实现目标。
namespace SqliteEFNoConfig
{
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Core.Common;
using System.Data.SQLite;
using System.Data.SQLite.EF6;
using System.Linq;
internal class Program
{
private static void Main()
{
// EF manages the connection via the DbContext instantiation
// connection string is set in config
// Use this code if you want to use a config file
// with only the connection string
//using (var model = new Model1())
//{
// var dbSetProperty = model.dbSetProperty.ToList();
//}
// Alternative method:
// Use this code if you don't want to use a config file
// You will also need to use the override constructor shown below,
// in your EF Model class
var connectionString = @"data source = {PathToSqliteDB}";
using (var connection = new SQLiteConnection(connectionString))
{
using (var model = new Model1(connection))
{
var dbSetProperty = model.dbSetProperty.ToList();
}
}
}
}
class SqliteDbConfiguration : DbConfiguration
{
public SqliteDbConfiguration()
{
string assemblyName = typeof (SQLiteProviderFactory).Assembly.GetName().Name;
RegisterDbProviderFactories(assemblyName );
SetProviderFactory(assemblyName, SQLiteFactory.Instance);
SetProviderFactory(assemblyName, SQLiteProviderFactory.Instance);
SetProviderServices(assemblyName,
(DbProviderServices) SQLiteProviderFactory.Instance.GetService(
typeof (DbProviderServices)));
}
static void RegisterDbProviderFactories(string assemblyName)
{
var dataSet = ConfigurationManager.GetSection("system.data") as DataSet;
if (dataSet != null)
{
var dbProviderFactoriesDataTable = dataSet.Tables.OfType<DataTable>()
.First(x => x.TableName == typeof (DbProviderFactories).Name);
var dataRow = dbProviderFactoriesDataTable.Rows.OfType<DataRow>()
.FirstOrDefault(x => x.ItemArray[2].ToString() == assemblyName);
if (dataRow != null)
dbProviderFactoriesDataTable.Rows.Remove(dataRow);
dbProviderFactoriesDataTable.Rows.Add(
"SQLite Data Provider (Entity Framework 6)",
".NET Framework Data Provider for SQLite (Entity Framework 6)",
assemblyName,
typeof (SQLiteProviderFactory).AssemblyQualifiedName
);
}
}
}
}
In case you decide to not add a connection string in the config file then you need to add the following constructor in the EF model. 如果您决定不在配置文件中添加连接字符串,则需要在EF模型中添加以下构造函数。
public Model1(DbConnection connection)
: base(connection, true)
{
}
Notice: The above code is just a sample on how to achieve the goal and you will have to adjust it accordingly to your needs. 注意:以上代码只是如何实现目标的示例,您必须根据需要进行调整。 The above code is provided assuming you are using EF Code First approach.
假设您使用的是EF Code First方法,则提供上述代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.