gpt4 book ai didi

mysql - 首先在 WCF 服务中的何处为 EF6 代码设置 DbConfiguration?

转载 作者:行者123 更新时间:2023-11-29 18:31:35 25 4
gpt4 key购买 nike

尝试首先将 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,因此错误消息非常有意义。问题是配置应该在哪里设置?

最佳答案

在 MyContext 上创建一个构造函数,如下所示:

public MyContext(DbConnection dbConnection, bool contextOwnsConnection)
: base(dbConnection, contextOwnsConnection)
{
}

然后手动提供 MySqlConnection:

var connection = new MySqlConnection(connectionString);
connection.Open();
var context = new MyContext(connection, true);

关于mysql - 首先在 WCF 服务中的何处为 EF6 代码设置 DbConfiguration?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45644407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com