0

I got this idea from here. It converts an old style connection string to a modelFirst Connection string:

public static class ConnectionStringExtensions
{
    public static string GetEntityConnectionString(this string connectionString, Type contextType)
    {
        var prefix = contextType.Namespace.Replace(contextType.Assembly.GetName().Name, "");

        if (prefix.Length > 0
            && prefix.StartsWith("."))
        {
            prefix = prefix.Substring(1, prefix.Length - 1);
        }

        if (prefix.Length > 1 && !prefix.EndsWith("."))
        {
            prefix += ".";
        }


        var csBuilder = new EntityConnectionStringBuilder
        {
            Provider = "System.Data.SqlClient",
            ProviderConnectionString = connectionString,
            Metadata = String.Format("res://{0}/{1}.csdl|"
                                     + "res://{0}/{1}.ssdl|"
                                     + "res://{0}/{1}.msl"
                , contextType.Assembly.FullName
                , prefix + contextType.Name)
        };

        return csBuilder.ToString();
    }
}

This works great. My question is when I call it:

string connString =   ConfigurationMananager.ConnectionStrings["name"].ConnectionString;
var dbContext = new MyDbContext(connString.ToEntityConnectionString(typeof(MyDbContext);

The call looks redundant with the instantiation and then passing the type into the extension method. Is there a way to get that programatically based on what constructor it was passed into

Community
  • 1
  • 1
Robert
  • 4,306
  • 11
  • 45
  • 95
  • Why create an extension method at all? Why not put that code into a private method of `MyDbContext` and then call that method inside the constructor? Then your calling code only needs to be `var dbContext = new MyDbContext(connString);` – Chris Dunaway Jul 29 '14 at 20:23
  • @ChrisDunaway I have multiple contexts to deal with and I am planning to create a nuget package out of this and use it in other applications. – Robert Jul 29 '14 at 20:27
  • since it is an extension method, so, your call can be var dbContext = connString.GetEntityConnectionString(this.GetType()). – Eric Lemes Jul 29 '14 at 21:12
  • @EricLemes I was hoping to call the method without passing anything into the method. I like that idea better than passing in an explicit type. But I didn't know if I could infer the type in the extension method based on the call – Robert Jul 30 '14 at 12:05
  • I'm afraid it isn't possible. You'll need to send the DbContext type to the method some way, or turn it into an constant inside your class. I don't know how to get the type of the caller from inside a method, and I think it's a bad ideal. That will be a collateral effect inside the method. If the DbContext is needed for the processing, it should be in the method signature. If you'll never change it, it should be a constant. – Eric Lemes Jul 30 '14 at 17:28
  • @EricLemes Thanks for the input. Put you're comment about using this.GetType() and I'll accept it as an answer – Robert Jul 30 '14 at 17:30

1 Answers1

1

I'm afraid it isn't possible.

You'll need to send the DbContext type to the method some way, or turn it into an constant inside your class.

I don't know how to get the type of the caller from inside a method, and I think it's a bad idea. That will be a collateral effect inside the method. If the DbContext is needed for the processing, it should be in the method signature. If you'll never change it, it should be a constant.

You can try to send the type using "this.GetType()" or var dbContext = connString.GetEntityConnectionString(this.GetType()).

Eric Lemes
  • 561
  • 3
  • 10