Entity Framework database not working (Code First, Web Deploy) - entity-framework

I'm using Asp.Net MVC4 with Entity Framework Code First.
Everything works locally, but when I publish, pages without database access work (on the internet), and any page with it, loads for an age, then gives me a server error.
I have enabled migrations, I have made an InitialCreate migration that is using the current model. I have ran the "Update-Database" command without error.
Any ideas ?
EDIT, thanks to the comment below, here is some detailed information on the error.
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
Error in stack trace...
[SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)]
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +5064458
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +234
System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo,
SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout,
Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean
integratedSecurity) +341
Also I'm using .sdf database's. Connection strings here...
<add name="DefaultConnection" connectionString="Data Source=|DataDirectory|Data.sdf" providerName="System.Data.SqlServerCe.4.0" />
<add name="ZDataContext" connectionString="Data Source=|DataDirectory|ZData.sdf" providerName="System.Data.SqlServerCe.4.0" />
I repeat, it all works locally.
Just uploaded all files manually, got this error...
Access to the database file is not allowed. [ 1884,File name = d:\virtualservers[username]\website.co.uk\wwwroot\App_Data\ZData.sdf,SeCreateFile ]
Again that works locally, and also works on none database pages, but databases pages give me the above error.

Related

SQL Server 2016 Express and LocalDb connection issues

I'm having a difficult time getting localdb running under IIS in the connection strings. I am running:
Windows 10
SQL Server 2016 Express and LocalDb installed
I added the following to my applicationHost.config, which I would not like to do if possible, but it changed the original error I was getting about not connecting:
<applicationPools>
<add name="myappPool"
autoStart="true" managedRuntimeVersion="v4.0">
// Added this <processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="true" />**
</add>
<applicationPoolDefaults managedRuntimeVersion="v4.0">
<processModel identityType="ApplicationPoolIdentity"
loadUserProfile="true" setProfileEnvironment="false" />
</applicationPoolDefaults>
</applicationPools>
The myAppPool is running under ApplicationPoolIdentity but changing this to another level of like LocalService doesn't help.
I am now getting this error:
An attempt to attach an auto-named database for file
C:\_sites\mySite\Databases\MyDb.ldf failed. A
database with the same name exists, or specified file cannot be
opened, or it is located on UNC share.
Here is my connection string:
<add name="core"
connectionString="Server=(localdb)\mssqllocaldb;Integrated Security=true; AttachDbFileName=C:\\_sites\mySite\Databases\MyDb.ldf;" />
I can verify that I can connect to (localdb)\mssqllocaldb if Management Studio. I was assuming that localdb meant I did not have to attach the db like I regularly do with sql if using the connection string above. I even added anonymous logon to the file security of the files just to see what would happen.
Any ideas?

Entity framework with db mode cannot connect to server

I am trying to connect to by db instance using db first, I created a connection
<add name="Entities"
connectionString="metadata=res://*/Models.ModelCmarket.csdl|res://*/Models.ModelCmarket.ssdl|res://*/Models.ModelCmarket.msl;provider=System.Data.SqlClient;provider connection string="data source=(localdb)\v12.0;initial catalog=Cevaheer;integrated security=True;trustservercertificate=False;multisubnetfailover=True;MultipleActiveResultSets=True;multipleactiveresultsets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
And I always get an error -
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. The specified LocalDB instance does not exist.)
But if i try
<add name="CevhermarketEntities"
connectionString="Data source=(localdb)\v12.0;initial catalog=Cevaheer;integrated security=True;trustservercertificate=False;multisubnetfailover=True;MultipleActiveResultSets=True;user id=dbuser;password=flexsin#123!;multipleactiveresultsets=True;"
providerName="System.Data.SqlClient" />
I can connect, and also can connect from VS sql server explorer and SSMS.
To begin - there are 4 issues that could be causing the common LocalDb SqlExpress Sql Server connectivity errors SQL Network Interfaces, error: 50 - Local Database Runtime error occurred, before you begin you need to rename the v11 or v12 to (localdb)\mssqllocaldb
You dont have the services running
You don't have the firelwall ports here
configured
Your install has and issue/corrupt (the steps below help give you a nice clean start)
You did not rename the V11 or 12 to mssqllocaldb
I found that the simplest is to do the below - I have attached the pics and steps for help.
First verify which instance you have installed, you can do this by checking the registry and by running cmd
cmd> Sqllocaldb.exe i
cmd> Sqllocaldb.exe s "whicheverVersionYouWantFromListBefore"
if this step fails, you can delete with option d cmd> Sqllocaldb.exe d "someDb"
cmd> Sqllocaldb.exe c "createSomeNewDbIfyouWantDb" create - the pic is error
cmd> Sqllocaldb.exe start "createSomeNewDbIfyouWantDb"

EntityFramework and update-database stopped working

I had everything working nicely using EF and code first until I changed the configuration class to add some extra seed data and then tried
Update-Database
Now I get the below error each time.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database. System.Data.SqlClient.SqlException
(0x80131904): A network-related or instance-specific error occurred
while establishing a connection to SQL Server. The server was not
found or was not accessible. Verify that the instance name is correct
and that SQL Server is configured to allow remote connections.
(provider: SQL Network Interfaces, error: 26 - Error Locating
Server/Instance Specified)
I can connect and view the database in the sql object explorer and even view the tables and data. I'm at a loss and about to give up code first and goto db first or maybe even stored procedures soon.
My connection string looks like this
<add name="MyDbContext" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MyDbContext.mdf;Initial Catalog=MyDb;Integrated Security=True" providerName="System.Data.SqlClient" />
Damn, I set the project as the startup project and then ran the command and it looks like its all worked now! Grrrr. I assumed selecting the project from Default project: selection dropdown did this but obviously not.
You can specify connection string via ConnectionString parameter:
Update-Database -ConnectionString "data source=server_name;initial catalog=db_name;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" -ConnectionProviderName "System.Data.SqlClient" -Verbose
Also you need to use this parameter with the same value for Add-Migration command:
Add-Migration Version_Name -ConnectionString "data source=server_name;initial catalog=db_name;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" -ConnectionProviderName "System.Data.SqlClient" -Verbose

Why won't my connection string work?

Could someone tell me why my connection string isn't working.. i'm currently using database first, but don't know why it wont connect. Whenever i try i get the error
'The network path was not found'
connection string is:
<add name="FactsAndFiguresEntities1" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string='data source=SERVERNAME User Id=****; Password = **** catalog=FactsAndFigures;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework'" providerName="System.Data.EntityClient" />
The error network path not found, could refer to how you are referencing the metadata files. When you use "res://" EF tries to load the metadata from assemblies in the bin catalogue of the application. Do you have a bin catalogue?
Also I notice that you use both Integrated Security=True and Username/password. The system will use one or the other not both.
For details on EF connection strings see: https://msdn.microsoft.com/en-us/library/vstudio/cc716756(v=vs.100).aspx

Entity framework - The provider did not return a ProviderManifestToken string error

I am using SQL Server 2005 Express with SQL Servre authentication and remote connections enabled although I am working locally.
My web.config connection string part:
<add name="HelpDesk2connect" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string='Data Source=SATURN;Initial Catalog=HelpDesk2;Integrated Security=False;user id=sa;password=*******;multipleactiveresultsets=true'" providerName="System.Data.EntityClient" />
I am logging in as sa
In the controller I get these errors (in this statement: `return View(db.Tickets.ToList());)
An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct.
The provider did not return a ProviderManifestToken string.
In the page I get this error:
[SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 52 - Unable to locate a LocalDB installation. Verify that SQL Server Express is properly installed and that the LocalDB feature is enabled.)]
Can you help me spot what am I doing wrong?
Thanx in advance