0

I am launching my asp.net application and am having issues with some databases that were created locally. I need to change their connection string for the program to point to my new online SQL Server database but I can't find any way to do that. I created these databases locally using the Entity Framework code-first scaffolding. This is my code for the db context:

public class SubjectDbContext : DbContext
{
    public DbSet<Subject> SubjectDatabase { get; set; }
}

And I made it into a .mdf using this method:

enter image description here

enter image description here

Which automatically generates the database using constructors (according to https://msdn.microsoft.com/en-us/library/jj653752(v=vs.110).aspx#localdbtosse). That's what I was able to figure out.

I really need to find these connection strings so I can change them because otherwise I don't know how to get my published application online to access its respective databases.

Remaking these databases is not really an option. So I need to find AND change the connection strings for them. Now the connection strings are not in the web.config file. In fact deleting all the connection strings in the web.config file does not stop these databases from working locally. So they must be stored somewhere else.

How can I change these connection strings. If I can't my application won't work online.

The closest post I could find to this question was: Code first: Where's the connection string & the Database?

but it doesn't answer the question. Thanks for any help or links.

4
  • If you search your entire solution for the MDF filename do you find anything? Its annoying when you search, get a hit but then find the question isn't answered hey! Commented Apr 13, 2017 at 0:55
  • Maybe that question is saying that if you add that entry it will automatically be used? Commented Apr 13, 2017 at 0:56
  • searching the whole solution only yeilds the file in the app data folder. I mean this must be an issue people commonly run into because how else would you be able to use these custom db contexts in a deployed web app, if you scaffolded them with default settings?. either that or ive done something really wrong, or something is really wrong in my app. Commented Apr 13, 2017 at 2:31
  • and I tried adding an entry with the connection name switched to the dbcontext name, but sadly it didnt work either. I'm starting to feel like remaking these databases might be the only way to get these connection string to change Commented Apr 13, 2017 at 2:43

1 Answer 1

1

The default Connection string gets added in Web.config by the name "DefaultConnection".

And the database files gets created under App_Data folder of your application.(Click 'Show All files' to see the databases file or browse to the App_Data folder path in explorer).

This is how you do migrations after having the DefaultConnection set to your new server:

  1. Change SubjectDbContext to as below, adding constructor:

    public class SubjectDbContext : System.Data.Entity.DbContext
    {
        public SubjectDbContext() : base("DefaultConnection")
        {
        }
    
        public DbSet<Subject> SubjectDatabase { get; set; }
    }
    
  2. Run command in Package Manager Console:

    Enable-Migrations -ContextTypeName SubjectDbContext -MigrationsDirectory Migrations\SubjectDbContext
    

    It will create Configuration.cs in Migrations/SubjectDbContext folder

  3. Set AutomaticMigrationsEnabled = true; in Configuration constructor. Can add seed code if required.

  4. Run the below command in Package Manager Console, to create the database and its tables:

    Update-Database -ConfigurationTypeName [Replacewith namespace].SubjectDbContext.Configuration –Verbose
    
Sign up to request clarification or add additional context in comments.

8 Comments

Nope. Ive been using the webconfig file in the root folder. It has a default connection string. But changing this connection string to my server has not resulted in these other databases working online in the published app, and in fact deleting the default connection string does not stop the local db (apart from login) from working locally either for these other db.
I'll post my connection string, maybe that will help
It used to be: <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=&quot;|DataDirectory|\aspnet-BA Portal-20170128030249.mdf&quot;;Initial Catalog=&quot;aspnet-BA Portal-20170128030249&quot;;Integrated Security=True" providerName="System.Data.SqlClient" />
and now its: <add name="DefaultConnection" connectionString="Data Source=184.163.194.53;Integrated Security=False;database=my_banaturalportal;User ID=my_banaturalportal_user;Password=Temp**!;Connect Timeout=1500;" providerName="System.Data.SqlClient" />
I have edited the answer on how to create DBs/Tables in the server.
|

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.