EntityFramework and update-database stopped working - entity-framework

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

Related

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"

Entity Framework code first - update database can't find .mdf in project

I am creating a small app with EF code first and I want to call the update database command from the package manager console and target the database in my app.config which is a local .mdf.
I have this connection string in my app.config
<connectionStrings>
<add name="EventsConnectionString"
connectionString="Data Source=(localdb)\v11.0;Database=EventsApp;Trusted_Connection=Yes;" />
</connectionStrings>
RegistrantContext class with connection string in constructor
using System.Data.Entity;
using EventsApp.Entities;
namespace EventsApp.Contexts.DataContexts
{
public class RegistrantDb :DbContext
{
public RegistrantDb()
: base("EventsConnectionString")
{
}
public DbSet<Registrant> Registrants { get; set; }
}
}
My file structure
And the command to update the database:
Update-Database -ConfigurationTypeName EventsApp.Contexts.DataContexts.RegistrantMigrations.Configuration -verbose
Error message
Error Number:-1,State:0,Class:20
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 believe my settings are wrong so it can't find Events.mdf. Does anyone know what I need to change?
You should always check if you use the correct project to apply updates to (either select the correct project from the "Default Project" dropdown at the top of the package manager console or you can add -ProjectName to your Update-Database command:
Update-Database -ProjectName EventsApp.Contexts -YourOtherOptions

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

can't change connection string (ODP, Oracle, Linq to Entities)

I have a App.config connection string that connects me to my database just fine:
<add name="SFEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=Oracle.DataAccess.Client;provider connection string="DATA SOURCE=micahs_poc_dev_server.kci;PASSWORD=kierkegaard;PERSIST SECURITY INFO=True;USER ID=KIERKEGAARD"" providerName="System.Data.EntityClient" />
I am using an ODP reference in conjunction with an ADO.NET / Linq to Entities approach.
The trouble comes when I want to connect to a different database (with the same schema) on our network.
<add name="SFEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=Oracle.DataAccess.Client;provider connection string="DATA SOURCE=QA_ETL_TEST_SERVER.COM;PASSWORD=bigshotpassword;PERSIST SECURITY INFO=True;USER ID=CoolidgeCalvin"" providerName="System.Data.EntityClient" />
When I just change the name of the server I get this error:
ORA-00942: table or view does not exist
I can actually run several lines of code before it throws an exception:
using (SFEntities ctx1 = new SFEntities())
{
var ds = ctx1.Connection.DataSource; // debugger: QA_ETL_TEST_SERVER.COM
var db = ctx1.Connection.Database;
var dstate = ctx1.Connection.State;
var dsite = ctx1.Connection.Site;
ctx1.Connection.Open();
SF_CHANGE_ORDER cotest = new SF_CHANGE_ORDER();
cotest.DELETE_FLAG = "D";
var result = (from cp in ctx1.SF_CLIENT_PROJECT
select cp).ToList(); // <--- error here!
I also tried changing the format to this:
<add name="SFEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=Oracle.DataAccess.Client;provider connection string="DATA SOURCE=(DESCRIPTION =(ADDRESS=(PROTOCOL=TCP)(HOST=155.32.75.11)(PORT=65550))(ADDRESS=(PROTOCOL=TCP)(HOST=155.32.75.11)(PORT=65550))(CONNECT_DATA=(SERVICE_NAME=QA_ETL_TEST_SERVER.COM)(FAILOVER_MODE=(TYPE=SELECT)(METHOD=BASIC)(RETRIES=180)(DELAY=5))));PASSWORD=bigshotpassword;PERSIST SECURITY INFO=True;USER ID=CoolidgeCalvin"" providerName="System.Data.EntityClient" />
Which basically mirrors the TNS entry I have for this server. I still get the 'does not exist' error.
If I make the server name something goofy I get a TNS error saying that server isn't recognized. So I am pretty sure it is picking up the server name I want and checking to see it is there.
I don't think it is a table permissions/creation issue because I can query them just fine on both databases from the same machine with the same credentials (and same TNS file).
[note: I changed the server/user/pw in the strings]
[edit: tried bringing up fiddler, but I didn't see any messages pass through]
Ah ... VisualStudio tucks away the schema name in the Model .edmx file. To make it work you have to somehow open the model file (use notepad or search all documents in the project) and manually change the schema name. Boo!

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

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.