0

I have built a mvc 4 intranet application that uses a local db to store info. Now that i want to deploy it on a server entries are not saving to the database. I have tried everything i can think of with no luck.

Is there a way to tell the application to use a sql database instead of a local instance in iis?

This is the connection string in my web.config

-- edit: formatted code below so it would show up in the question. --

<add name="MyCalendarConnectionString" connectionString="Data Source=DST09119\SQLEXPRESS;Initial Catalog=MyCalendar;Integrated Security=True"
      providerName="System.Data.SqlClient" />
    <add name="SampleConnectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Sample.mdf;Integrated Security=True"
      providerName="System.Data.SqlClient" />
ps2goat
  • 8,067
  • 1
  • 35
  • 68
Ernie
  • 49
  • 1
  • 9

1 Answers1

0

Two ways to go about this:

  1. name your connection string after your derived DbContext class (i.e.; if your DbContext is XyzEntities, then name your conn string XyzEntities <add name="XyzEntities" connectionString"..... />

  2. Pass the name of the connection string into the base constructor for DbContext. You want to use a constructor like below:

    public class XyzEntities : DbContext
    {
        public XyzEntities( string nameOrConnectionString ) 
            : base( nameOrConnectionString )
        {
        }
    }
    
    // usage
    var dbContext = new DerivedDbContext( "MyCalendarConnectionString" );
    

Update:

If you just can't connect to the SQL Server express instance, it's due to using Integrated Security=True - unless you set the identity of the app pool to a user that has access to the database, the connection will be denied.

Moho
  • 15,457
  • 1
  • 30
  • 31
  • Currently my dbContext files contains base(global::System.Configuration.ConfigurationManager.ConnectionStrings["SampleConnectionString"].ConnectionString, mappingSource). – Ernie Nov 08 '13 at 08:39
  • Well, the call to `ConfigurationManager.ConnectionsStrings` is unnecessary - simply pass in `"SampleConnectionString"` as the first parameter to the base constructor. – Moho Nov 08 '13 at 08:42
  • You're using integrated security w/ an asp.net app pool identity - your app pool must use an identity that has access to the database – Moho Nov 08 '13 at 08:59
  • How will be going about in doing that? – Ernie Nov 08 '13 at 09:03
  • open IIS Manager (run: inetmgr), click on `Application Pools`, find your application's app pool (if you don't know what it is, look at advanced properties of your application in IIS Manager to find it), right click and select advanced settings, set the `Identity` property to an identity that has access to your SQL Server. Now remember, any of the server's web applications that use that application pool will now use that identity. If you use your identity, for example, all the web apps running under that app pool will have access to your network resources, etc. – Moho Nov 08 '13 at 09:07
  • Still no luck Application Pool identity set to use local account. Database connection still seems to be denied – Ernie Nov 08 '13 at 09:27
  • change it to your account briefly to test – Moho Nov 08 '13 at 09:29
  • Currently set to my local account yet no go. Im trying to run this on a remote server 2012 with sql 2012 and iis 8. Gave my local account full sql rights and running the application pool with that account. But still no go – Ernie Nov 08 '13 at 09:34
  • what do you mean by "local account" – Moho Nov 08 '13 at 09:34
  • The account i use to log on to the server – Ernie Nov 08 '13 at 09:36