DbContext 构造函数 (DbConnection, DbCompiledModel, Boolean)
[本页针对的是实体框架版本 6。最新版本以“实体框架”NuGet 包的形式提供。有关实体框架的更多信息,请参见 msdn.com/data/ef。]
通过使用现有连接来连接到数据库以构造一个新的上下文实例,并从给定模型初始化该实例。 如果 contextOwnsConnection 是 false,则释放上下文时将不会释放该连接。
命名空间: System.Data.Entity
程序集: EntityFramework(在 EntityFramework.dll 中)
语法
声明
<SuppressMessageAttribute("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")> _
<SuppressMessageAttribute("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")> _
Public Sub New ( _
existingConnection As DbConnection, _
model As DbCompiledModel, _
contextOwnsConnection As Boolean _
)
用法
Dim existingConnection As DbConnection
Dim model As DbCompiledModel
Dim contextOwnsConnection As Boolean
Dim instance As New DbContext(existingConnection, _
model, contextOwnsConnection)
[SuppressMessageAttribute("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
[SuppressMessageAttribute("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
public DbContext(
DbConnection existingConnection,
DbCompiledModel model,
bool contextOwnsConnection
)
[SuppressMessageAttribute(L"Microsoft.Usage", L"CA2214:DoNotCallOverridableMethodsInConstructors")]
[SuppressMessageAttribute(L"Microsoft.Reliability", L"CA2000:Dispose objects before losing scope")]
public:
DbContext(
DbConnection^ existingConnection,
DbCompiledModel^ model,
bool contextOwnsConnection
)
[<SuppressMessageAttribute("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")>]
[<SuppressMessageAttribute("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")>]
new :
existingConnection:DbConnection *
model:DbCompiledModel *
contextOwnsConnection:bool -> DbContext
public function DbContext(
existingConnection : DbConnection,
model : DbCompiledModel,
contextOwnsConnection : boolean
)
参数
- existingConnection
类型:System.Data.Common.DbConnection
要用于新的上下文的现有连接。
- model
类型:System.Data.Entity.Infrastructure.DbCompiledModel
支持此上下文的模型。
- contextOwnsConnection
类型:System.Boolean
如果设置为 true,则释放上下文时将释放该连接;否则调用方必须释放该连接。