首先在哪里为 EF6 代码设置 WCF 服务中的 DbConfiguration?
Where to set the DbConfiguration in a WCF service for EF6 code first?
尝试首先将 EF 6 代码与 WCF 服务一起使用,但 运行 出现以下 运行 时间错误:
The default DbConfiguration instance was used by the Entity Framework
before the 'MyConfiguration' type was discovered. An instance of
'MyConfiguration' must be set at application start before using
any Entity Framework features or must be registered in the
application's config file. See
http://go.microsoft.com/fwlink/?LinkId=260883 for more information.
尝试在服务调用中实例化以下 DbContext 时抛出此错误:
[DbConfigurationType(typeof(MyConfiguration))]
public partial class MyContext : DbContext
{
public MyContext()
: base("name=MyContext")
{
}
}
public class MyConfiguration : DbConfiguration
{
public MyConfiguration()
{
SetExecutionStrategy(MySqlProviderInvariantName.ProviderName, () => new MySqlExecutionStrategy());
SetDefaultConnectionFactory(new MySqlConnectionFactory());
AddDependencyResolver(new MySqlDependencyResolver());
}
}
此 WCF 服务有其他 DbContext,甚至在此 class 被访问之前使用,因此错误消息非常有意义。问题是应该在哪里设置配置?
按如下方式在 MyContext 上创建构造函数:
public MyContext(DbConnection dbConnection, bool contextOwnsConnection)
: base(dbConnection, contextOwnsConnection)
{
}
然后手动提供 MySqlConnection:
var connection = new MySqlConnection(connectionString);
connection.Open();
var context = new MyContext(connection, true);
尝试首先将 EF 6 代码与 WCF 服务一起使用,但 运行 出现以下 运行 时间错误:
The default DbConfiguration instance was used by the Entity Framework before the 'MyConfiguration' type was discovered. An instance of 'MyConfiguration' must be set at application start before using any Entity Framework features or must be registered in the application's config file. See http://go.microsoft.com/fwlink/?LinkId=260883 for more information.
尝试在服务调用中实例化以下 DbContext 时抛出此错误:
[DbConfigurationType(typeof(MyConfiguration))]
public partial class MyContext : DbContext
{
public MyContext()
: base("name=MyContext")
{
}
}
public class MyConfiguration : DbConfiguration
{
public MyConfiguration()
{
SetExecutionStrategy(MySqlProviderInvariantName.ProviderName, () => new MySqlExecutionStrategy());
SetDefaultConnectionFactory(new MySqlConnectionFactory());
AddDependencyResolver(new MySqlDependencyResolver());
}
}
此 WCF 服务有其他 DbContext,甚至在此 class 被访问之前使用,因此错误消息非常有意义。问题是应该在哪里设置配置?
按如下方式在 MyContext 上创建构造函数:
public MyContext(DbConnection dbConnection, bool contextOwnsConnection)
: base(dbConnection, contextOwnsConnection)
{
}
然后手动提供 MySqlConnection:
var connection = new MySqlConnection(connectionString);
connection.Open();
var context = new MyContext(connection, true);