Why is the SPA application not creating a database? - asp.net

I have followed a microsoft video trying to make the single page application and it doesnt create or connect to my sql instance even though I have other applications using code first. For example the connection string was...
<add name="DefaultConnection" connectionString="Data Source=Localhost;Initial Catalog=aspnet-MvcApplication2-201265113324;Integrated Security=True" providerName="System.Data.SqlClient" />
But I get an error saying
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)
Its really strange because my other application with the connectionstring...
<add name="PteDotNetContext" providerName="System.Data.SqlClient" connectionString="Server=Localhost;Database=PteDotNet;Trusted_Connection=true;" />
Works fine and will connect no problem...

If you do not provide a connection string or connection string name to the DbContext constructor, it will try to find a connection name that matches the (inherited) DbContext class name.
Hence you can either change the name of the connection string
<add name="MyDbContext" connectionString="Data Source=Localhost;Initial Catalog=aspnet-MvcApplication2-201265113324;Integrated Security=True" providerName="System.Data.SqlClient" />
Or provide the name of the connection string in the constructor
public class MyDbContext : DbContext
{
public MyDbContext() : base("DefaultConnection") {}
}

Related

ASP.NET MVC identification tables creation

So I have a project with my custom, local DB (created by model-first approach). I need to create there identification tables (like asp_users, asp_roles, I don't remember how exactly they are called, but I hope you got the idea). As far as I know, they should be created on first registration, however it doesn't happen:
I can't see those tables in SQL Management Studio either.
My connection string (created automatically with DB):
<!--<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-WebApplication1-20170504122316.mdf;Initial Catalog=aspnet-WebApplication1-20170504122316;Integrated Security=True" providerName="System.Data.SqlClient" />-->
<add name="DefaultConnection" connectionString="metadata=res://*/DBModel.csdl|res://*/DBModel.ssdl|res://*/DBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=VADIM-PC;initial catalog=PIT_3_Project_DB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
The first connection string is truly default, the second is mine.
The fun part is - authentication works fine, I can register and login, and I don't see where my code cane use with the default local DB.
UPD: Sorry, I haven't changed this row:
public ApplicationDbContext()
: base("DBModelContainer", throwIfV1Schema: false)
{
}
I have changed DBModelContainer to DefaultConnection and now I am getting this error (when I am trying to register)
The entity type ApplicationUser is not part of the model for the current context.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
You should create these tables with code first migrations.
First step:
Uncomment first default connection string,rename it for example DefaultAuthConnection and modify it like this (for local DB).
<add name="DefaultAuthConnection" connectionString="data source=.;initial catalog=PIT_3_Project_DB;persist security info=True;" providerName="System.Data.SqlClient" />
<add name="DefaultConnection" connectionString="metadata=res://*/DBModel.csdl|res://*/DBModel.ssdl|res://*/DBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=VADIM-PC;initial catalog=PIT_3_Project_DB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Second Step: Open your IdentityModels.cs and modify ApplicationDBContext like this:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultAuthConnection", throwIfV1Schema: false)
{
}
}
Third Step: Open Package Manager Console (View->Other Windows->Package Manager Console) and write follow steps:
Enable-Migrations -ContextTypeName
WebApplication1.ApplicationDbContext
Add-Migration Init
Update-Database
From now on, you can see authentication tables in your local database.

Unable to complete operation. The supplied SqlConnection does not specify an initial catalog or AttachDBFileName

I am trying to create an App Service web app in the Azure Portal and to connect the web app to my local on-premises SQL Server database using the new Hybrid Connection feature.
Created a simple ASP.NET application using Visual Studio 2015 and trying to connect to the SQL server Database which is on on-prem. And modified the connection String as follows
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=PRAVEEN,1433; User ID=sa; Password=my_password;"
providerName="System.Data.SqlClient" />
</connectionStrings>
When the application is started its running without any errors as shown in below screenshot1.
But when I try register (enter any record) it throws me with an error as specified in the below Screenshot3....
"https://azure.microsoft.com/en-us/documentation/articles/web-sites-hybrid-connection-connect-on-premises-sql-server/" - this is the documentation to which I'm referring to..
So, can anyone from the other end help me out please...
Microsoft SQL Server Management Studio screenshot....
Thank you..
#Praveen,
It is what it says - meaning Initial Catalog is missing in your connection string. It requires the Db name to connect to. You have the servername, SQLUserName, SQLPassword, but missing the DatabaseName it requires. Please change your connection string to include the the DbName as follows
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=YourServerName,1433;Database="YourDbName" UserID=YourId;Password=YourPswd;"
providerName="System.Data.SqlClient" />
</connectionStrings>
Look at this site to configure your connection string appropriately: http://www.connectionstrings.com/store-connection-string-in-webconfig/

Cannot connect to mysql database in .NET MVC3

I've having some trouble in connecting mvc 3 application in .net. I know that mysql database instance has to be created before using web.config file to connect the it. My
connectionStrings as below:
<connectionStrings>
<add name="epmsDb"
connectionString="Server=localhost;Database=database1;
Uid=root;Pwd=mypassword"
providerName="System.Data.SqlClient" />
</connectionStrings>
and I've got a database name call epmsDb created in Server explorer. Then I tried to get the connectionString in the code using this :
string connectionString =
ConfigurationManager.ConnectionStrings["epmsDb"].ConnectionString;
context = new DataContext(connectionString);
usersTable = context.GetTable<UserObj>();
Every time I tried to access the database, I threw this exception:
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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
I've been trying to overcome this for a week now. Does anyone have ideas why this occurres?
Your example tries to connect to an MS SQL database. To connect to a MySQL DB you might want to use the MySQL connector
Then change the ProviderName property:
<connectionStrings>
<add name="epmsDb"
connectionString="Server=localhost;Database=database1;
Uid=root;Pwd=mypassword"
providerName="MySql.Data.MySqlClient" />
</connectionStrings>

connection string issue after hosting website

Please help me with this!
I've put my website on somee.com, they gave this connection string
Connection string: workstation id=GWAPDataBase.mssql.somee.com;packet size=4096;user id=xxxx;pwd=xxxxx;data source=GWAPDataBase.mssql.somee.com;persist security info=False;initial catalog=GWAPDataBase
and my auto generated connection string when i was on local host was
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
<add name="GWAP_Entities" connectionString="metadata=res://*/App_Code.GWAPModel.csdl|res://*/App_Code.GWAPModel.ssdl|res://*/App_Code.GWAPModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\aspnetdb.mdf;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
I tried to modify it, but I get an error that says
The specified named connection is either not found in the
configuration, not intended to be used with the EntityClient provider,
or not valid.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.
Exception Details: System.ArgumentException: The specified named
connection is either not found in the configuration, not intended to
be used with the EntityClient provider, or not valid.
Source Error:
Line 19: #region Constructors Line 20: Line 21: public
GWAP_Entities() Line 22: : base(ConnectionString,
ContainerName) Line 23: {
Please help me how should I modify the connection string?
It sounds like your connection string name and the name that the Entity Framework expects do not match. Without more information about how your code looks, I am assuming that you generated your models from the database using the wizard in Visual Studio. When you do that, you enter a name for the connection string which then gets hard-coded in your designer file. An example:
public FooEntity() : base("name=FooEntity", "FooEntity")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
The string "FooEntity" in this case would need to be the name of the connection string. If they are different, a connection string won't be found. Make sure those values match.

problem connecting to access database in asp.net 2.0

hi im trying to get a dataset from an access database
im using this connection string:
<connectionStrings>
<add name="SiteConnString" connectionString="Data Source=c:\inetpub\vhosts\db\mainDB.mdb"
providerName="Microsoft.Jet.OLEDB.4.0" />
</connectionStrings>
and this is my call to SqlHelper:
myDataSet = SqlHelper.ExecuteDataset(connString, CommandType.Text, strSQL);
and the error im getting is this:
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)
The providerName attribute expects an ADO.NET provider class name. Try changing your connection like so:
<connectionStrings>
<add name="SiteConnString"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\inetpub\vhosts\db\mainDB.mdb"
providerName="System.Data.OleDb" />
</connectionStrings>
A very handy reference for connection string formats is http://connectionstrings.com

Resources