0

Entity Framework code-first deployment to company SQL Server issues.

I have an ASP.NET MVVM app that uses EF. It runs perfectly on my development laptop. However when I move the database to the company SQL Server, I'm having issues.

The connection string I’m using in my web.config is

<add name="ApplicationInventoryContext" 
     connectionString="Data Source=COMPANY_SQLSERVER; Initial Catalog=ApplicationInventory; Integrated Security=True;Connect Timeout=15;" 
     providerName="System.Data.SqlClient" />

The connection string should be OK as I’m able to connect and query the database using this connection from:

  • Visual Studio SQL Server Explorer
  • Microsoft SQL Server Management Studio

This is my DbContext:

public class ApplicationInventoryContext:DbContext
{
     public DbSet<Application> Applications { get; set; }

     public ApplicationInventoryContext() : 
         base("name=ApplicationInventoryContext")
     {
         Database.SetInitializer<ApplicationInventoryContext>(null);
     }
 }

This is the repository where I query the database:

public class DisconnectedRepository
{
     public List<Application> GetApplicationsList()
     {
         using (var context = new ApplicationInventoryContext())
         {
            return context.Applications.AsNoTracking().ToList();
         }
     }
     .....................
}

When I run the application I got the following error:

Entity Framework Exception SqlException: Cannot open database requested by the login
SqlException: Cannot open database requested by the login .
The login failed for user ' domain\userid’

How could this happens if the error shows the same login I'm using in the two connections above that are Ok?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
MasterOfTheHouse
  • 1,164
  • 1
  • 14
  • 34
  • 1
    Try to specify the connectionstring keyname in the creation of the DbContext. See this QA https://stackoverflow.com/questions/4805094/pass-connection-string-to-code-first-dbcontext – Steve Nov 30 '17 at 21:20
  • 3
    Your web app doesn't run as you. It will normally run as a secured anonymous user for security reasons. "Integrated Security" will only work if the user owning the process has access to the DB – Eris Nov 30 '17 at 21:31
  • @Steve : Steve Thanks so much for send me in the right direction.I do really appreciate your help as I spent hours debugging and trying to find the solution to the issue .Indeed I had to specify the connectionString in the creation of the DbContext – MasterOfTheHouse Dec 01 '17 at 03:11

2 Answers2

4

Your connection string is specifying Integrated Security=True. That means it is trying to use the identity of the process running the application to access the database. Because it's a web application you're probably using IISExpress locally, which means it's running as the identity of the logged in user (i.e. you). Do you have permission to log into the company SQL server using the credentials you use to log into your computer? If not, there's your problem.

What seems unusual to me is that you're trying to hit the corporate SQL Server from your development machine. Most organizations don't allow developers to access production, so it's hard to tell if you're violating policy trying to do what you're doing or if that's just normal in your environment.

There are two options here:

  1. Give your domain account a login on the corporate SQL Server. This isn't a particularly good option because if you're not the only developer then every developer is going to need to have the same thing done.

  2. Create a SQL Server login and change your connection string to use UserId and Password rather than integrated security. That's still not a great option because now every developer will know the login credentials to the corporate SQL Server.

Craig W.
  • 17,838
  • 6
  • 49
  • 82
0

I will post the solution I found thanks to Steve . The response I found was here stackoverflow.com/questions/4805094/

So what I did was just modify the constructor of the DBContext as under:

>

public class ApplicationInventoryContext:DbContext { public DbSet Applications { get; set; }

public ApplicationInventoryContext() : 
                                     base("Data Source=COMPANY_SQLSERVER;                                           >                                          Initial Catalog=ApplicationInventory; 
                                      Integrated Security=True;      
                                       Connect >Timeout=15;")
{
    Database.SetInitializer<ApplicationInventoryContext>(null);
}

}

And it worked perfectly now . Please look at the above link where you can find other approaches

MasterOfTheHouse
  • 1,164
  • 1
  • 14
  • 34