问题描述
我已经看到一些,当创建新的DbContext时,将实体框架连接字符串作为构造函数参数。但是当我将一个新的ADO.NET实体数据模型添加到一个项目(数据库中)时,DbContext只有一个无参数的构造函数。
I have seen some code sample that put an entity framework connection string as a constructor argument when creating a new DbContext. But when I added a new ADO.NET entity data model into a project (database first), the DbContext only has one parameterless constructor.
我错过了一步吗?我应该怎么做才能获得该构造函数?
Did I miss a step? What should I do to get that constructor?
Visual Studio 2012定位.net框架4.5实体框架5。
Visual Studio 2012 targeting .net framework 4.5 entity framework 5.
推荐答案
根据Arthur Vickers的建议,我将部分类扩展为接受连接字符串的构造函数。在C#(非常类似于hege的答案):
As per Arthur Vickers' suggestion, I am extending the partial class to have a constructor that accepts connection string. In C# (very similar to hege's answer):
public partial class MyEFEntities
{
public MyEFEntities(string connectionstring)
: base(connectionstring)
{
}
}
或在VB.Net中:
Partial Public Class MyEFEntities
Public Sub New(ConnectionString As String)
MyBase.New(ConnectionString)
End Sub
End Class
这篇关于EF连接字符串为DbContext构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!