C# 用C添加entityFramework部分#
我们有一个WPF应用程序,可以通过内部开发的外部组件进行扩展。某些外部组件要求在安装组件期间将新节(在本例中为C# 用C添加entityFramework部分#,c#,mysql,entity-framework-6,app-config,C#,Mysql,Entity Framework 6,App Config,我们有一个WPF应用程序,可以通过内部开发的外部组件进行扩展。某些外部组件要求在安装组件期间将新节(在本例中为EntityFrameworkSection)添加到WPF应用程序的app.config。但是,EntityFrameworkSection似乎不可访问,因为它是一个内部类 我们的问题是,我们是否可以通过编程方式将EntityFrameworkSection添加到app.config?由于我碰巧使用了EF6,我最终使用了自EF6以来可用的。在我的例子中,我试图添加与MySQL相关的配置。
EntityFrameworkSection
)添加到WPF应用程序的app.config
。但是,EntityFrameworkSection
似乎不可访问,因为它是一个内部类
我们的问题是,我们是否可以通过编程方式将
EntityFrameworkSection
添加到app.config
?由于我碰巧使用了EF6,我最终使用了自EF6以来可用的。在我的例子中,我试图添加与MySQL相关的配置。基本上,我们需要做的是从DbConfiguration
派生并将其设置为EF的配置。以下是我的想法:
派生自DbConfiguration
public类CustomDbConfiguration:DbConfiguration
{
公共CustomDbConfiguration()
{
SetProviderServices(MySqlProviderInvariantName.ProviderName,新的MySqlProviderServices());
SetProviderFactory(MySqlProviderInvariantName.ProviderName,新的MySqlClientFactory());
}
}
然后像这样使用它:
类程序
{
//“DbConfiguration”应视为只读。的一个且仅一个实例
//每个AppDomain中都允许使用“DbConfiguration”。
private static readonly CustomDbConfiguration DBConfig=new CustomDbConfiguration();
静态void Main(字符串[]参数)
{
//在使用EntityFramework的任何功能之前,请显式设置配置。
SetConfiguration(DBConfig);
使用(var dbContext=new MySQLDb())
{
var dbSet=dbContext.Set();
//从数据库中读取数据。
var actors=dbSet.ToList();
}
使用(var dbContext=new SQLDb())
{
var dbSet=dbContext.Set();
//从数据库中读取数据。
var users=dbSet.ToList();
}
}
}
Myapp.config
仅包含连接字符串的信息,如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<connectionStrings>
<add name="MySQLDb" connectionString="server=localhost;port=3306;database=sakila;uid=some_id;password=some_password" providerName="MySql.Data.MySqlClient"/>
<add name="SQLDb" connectionString="data source=.\SQLEXPRESS;initial catalog=some_db;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
因为我碰巧使用了EF6,所以我最终使用了自EF6以来可用的。在我的例子中,我试图添加与MySQL相关的配置。基本上,我们需要做的是从
DbConfiguration
派生并将其设置为EF的配置。以下是我的想法:
派生自DbConfiguration
public类CustomDbConfiguration:DbConfiguration
{
公共CustomDbConfiguration()
{
SetProviderServices(MySqlProviderInvariantName.ProviderName,新的MySqlProviderServices());
SetProviderFactory(MySqlProviderInvariantName.ProviderName,新的MySqlClientFactory());
}
}
然后像这样使用它:
类程序
{
//“DbConfiguration”应视为只读。的一个且仅一个实例
//每个AppDomain中都允许使用“DbConfiguration”。
private static readonly CustomDbConfiguration DBConfig=new CustomDbConfiguration();
静态void Main(字符串[]参数)
{
//在使用EntityFramework的任何功能之前,请显式设置配置。
SetConfiguration(DBConfig);
使用(var dbContext=new MySQLDb())
{
var dbSet=dbContext.Set();
//从数据库中读取数据。
var actors=dbSet.ToList();
}
使用(var dbContext=new SQLDb())
{
var dbSet=dbContext.Set();
//从数据库中读取数据。
var users=dbSet.ToList();
}
}
}
Myapp.config
仅包含连接字符串的信息,如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<connectionStrings>
<add name="MySQLDb" connectionString="server=localhost;port=3306;database=sakila;uid=some_id;password=some_password" providerName="MySql.Data.MySqlClient"/>
<add name="SQLDb" connectionString="data source=.\SQLEXPRESS;initial catalog=some_db;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>