0

I've been trying to connect my ASP.NET MVC application to a SQL database without success.

I have in my DAL project, in App.config:

<connectionString>
    <add name="CrmContext" connectionString="..." providerName="..." />
</connectionString>

And in my WebUI project, in the Web.config file I have:

<connectionString>
    <add name="CrmContext" connectionString="..." providerName="..." />
</connectionString>

My context in the DAL project is declared like this:

public CrmContext() {
    Database.SetInitializer(new CrmDbInitializer());
}

The initial seeding of the database works fine, but then when a Controller tries to access the database it gives me the error:

SQL Network interfaces, Error: 26 - Error locating Server

Apparently it tries to create/access the database in App_data folder. I've tried many things for hours and nothing works...

I'm using EntityFramework and Code-First approach.

Rajput
  • 2,597
  • 16
  • 29
Kevin Amorim
  • 527
  • 5
  • 18
  • Can you please provide connection string? Maybe is problem with your connection string in Web project. By the way, when you start web project it will pull connection string from Web.config not from App.config. – kat1330 Dec 01 '16 at 19:28
  • @kat1330 Even being in separate projects it will get the connection string from the Web.config in the other project? I don't think it's the connection string because the error it's because it tries to access App_data. – Kevin Amorim Dec 01 '16 at 19:32
  • What is a `CrmDbInitializer`? – mason Dec 01 '16 at 19:41
  • @KevinAmorim Who tries to access to App_data? I am guessing context will try to access App_data if you specify in connection string. Maybe I am wrong. – kat1330 Dec 01 '16 at 19:48
  • From the error, I am pretty sure its somewhere in the connection string portion of the Connection String. – Kevin B Burns Dec 01 '16 at 20:15
  • @KevinBBurns Connection string: – Kevin Amorim Dec 01 '16 at 20:18
  • @mason It's the seeding class. – Kevin Amorim Dec 01 '16 at 20:19
  • There are context constructor overloads that accept the name of the database connection string or a connection string as a parameter. Perhaps you should [check it out](https://msdn.microsoft.com/en-us/library/system.data.entity.dbcontext.dbcontext(v=vs.113).aspx). – mason Dec 01 '16 at 20:25
  • @mason I've tried public CrmContext : base("connection string here") {} and I have the same problem. – Kevin Amorim Dec 01 '16 at 20:28
  • Try to go through this link, it might be useful http://stackoverflow.com/questions/13754563/sql-network-interfaces-error-26-error-locating-server-instance-specified – Rajput Dec 01 '16 at 20:41
  • @KevinAmorim, have you tried the connection string without the SQL Instance name? And are you properly sending over Windows Identity credentials? Integrated Security is for users on a domain, you might have to connect with a SQL authenticated account. I would disable MARS until you have this all squared out, especially if the connection is on the slower side. – Kevin B Burns Dec 01 '16 at 20:57
  • @KevinAmorim, what I meant for instance name is Source=sublimesw.dyndns.info\sqlloja minus the \sqlloja. – Kevin B Burns Dec 01 '16 at 20:58
  • @KevinBBurns Tried without the instance name, same error. What you mean with Integrated Security? When I login in database via SQL Management Studio I use SQL credentials. – Kevin Amorim Dec 01 '16 at 22:20
  • @KevinBBurns What I Can't understand is how it correctly seeds the database, but then the controller's can't access it. – Kevin Amorim Dec 01 '16 at 22:22

1 Answers1

1

I fixed the problem.

I kept the Connection String in the Web.config, no problems with that.

The problem was with the Authentication. I was missing

<membership defaultProvider="CrmContext"
    <providers>
        <clear />
        <add name="CrmContext" connectionStringName="DefaultConnection" />
    </providers>
</membership>

Also, I had to remove from my Web.config:

<roleManager.... />

And everything started working fine, even Authentication. My "DefaultConnection" connection string points to my database, of course.

I hope it helps someone with the same problem.

Kevin Amorim
  • 527
  • 5
  • 18