Entity Framework DefaultConnectionFactory being ignored

雨燕双飞 提交于 2019-12-01 00:39:35
dksh

I know it is not ideal but this worked for me:

public class DBBase : DbContext
{
    public DBBase(string nameOrConnectionString)
        : base(Database.DefaultConnectionFactory.CreateConnection(nameOrConnectionString), true)
    {
    }
    // ...
}
JabberwockyDecompiler

You need to get the connection that you built for each call that you are wanting to use. For example using the following code.

private static void UsingCustomConnection()
{
    using (var conn = Database.DefaultConnectionFactory.CreateConnection("YourDbName"))
    {
        using (var context = new YourContext(conn))
        {
            context.Destinations.Add(new Destination {Name = "Colorado"});
            context.SaveChanges();
        }
    }
}

You will need to setup this in YourContext

public YourContext(DbConnection connection)
    : base(connection, contextOwnsConnection: false)
{

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!