Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF connection string as DbContext constructor argument

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 targeting .net framework 4.5 entity framework 5.

like image 811
Endy Tjahjono Avatar asked Jan 28 '13 12:01

Endy Tjahjono


1 Answers

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)
    {
    }
}

Or in VB.Net:

Partial Public Class MyEFEntities
    Public Sub New(ConnectionString As String)
        MyBase.New(ConnectionString)
    End Sub
End Class
like image 87
Endy Tjahjono Avatar answered Sep 22 '22 10:09

Endy Tjahjono