Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

entity framework - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...