为什么DbContext接受DbConnection而不接受IDbConnection? [英] Why does DbContext accept DbConnection and not IDbConnection?

查看:494
本文介绍了为什么DbContext接受DbConnection而不接受IDbConnection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将自定义IDbConnection实现传递给DbContext。


我不能从抽象类DbConnection继承,因为我需要SqlConnection类中的功能。 / p>

我无法从SqlConnection继承,因为该类是密封的。


我有一个通过组合在内部使用SqlConnection的类,并实现了IDbConnection接口所以它可以使用。


有人知道为什么DbContext构造函数不接受接口IDbConnection吗?


接受不是不好的做法接口可用时的具体类?


Jamie。

解决方案

嗨Jamie,


这是我们正在构建的现有EF组件的限制,它们需要DbConnection。


我们可能会解除此限制未来发布,但目前的发布是不可能的。


~Rowan


I am trying to pass in a custom IDbConnection implementation into DbContext.

I cannot inherit from the abstract class DbConnection, because I need the functionality in the SqlConnection class.

I cannot inherit from SqlConnection, because the class is sealed.

I have a class that uses an SqlConnection internally through composition, and implements the IDbConnection interface so it can be used.

Does anybody know why the DbContext constructor does not accept the interface IDbConnection?

Isn't it bad practice to accept the concrete class when an interface is available?

Jamie.

解决方案

Hi Jamie,

This is a limitation of the existing EF components that we are building on top of, they require a DbConnection.

We may lift this restriction in a future release but it wasn't possible for the current release.

~Rowan


这篇关于为什么DbContext接受DbConnection而不接受IDbConnection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆