1

Seems an easy thing to do, but I cannot find how.. We have an EF EDMX model, DB first. Our DbContext's name is let's say MyEntities. This generates a context file

// <auto-generated>
public partial class MyEntities: DbContext
    {
        public MyEntities()
            : base("name=MyEntities")
        {
        }
    }

This means that connection string in config is also "MyEntities". Now we want to change the name to sth more suitable.

When I say 'Update from Db' in update dialog there is an option "Save entity connection settings in App.config as", but the name I see there is "MyEntities" and I cannot edit it. I checked Properties for EDMX, .tt and whatever files, this is nowhere to find.

Editing it directly in MyEntities.Context.cs will not do, as the file is autogenerated.

So where can I change this name?

2 Answers 2

2

You can add a partial MyEntities class with a custom constructor:

public partial class MyEntities : DbContext
{
    public MyEntities(string connectionString) : base(connectionString)
    {
    }
}

This allows you to pass in a custom connection string:

new MyEntities("name=Foo");

If you want to do this design time: On the designer surface of your Entity Framework Model, do right click and go to properties. You can change the name from there.

Sign up to request clarification or add additional context in comments.

3 Comments

Good idea. It worked of course, thx, although I really don't get why one cannot (easily / in a straightforward way) just rename my DbContext. As I understood the connection string name is just by default the same as the name of my DbContext class.
On the designer surface of your Entity Framework Model, do right click and go to properties. You can change the name from there.
Perfect! That was the place I was looking for!
0

Change the name of connection string in web.config or app.config like this:

<connectionStrings>
    <add name="HereGoesName" connectionString="Data Source=..."
        providerName="System.Data.SqlClient" />
</connectionStrings>

and you shoule be able to freerly edit it as app.config is editable.

1 Comment

I can edit name in App.config, true, but my DbContext looks in App.config for connection string that is called MyEntities, that's the problem. That piece of code in the question is autogenerated and there comes this nae in the constructor

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.