Working on a project without a connection string - c#

I am trying to publish my website on the intranet server. From websites and suggestion I understood that i had to change the connection string to point to the database in the intranet server. While trying to do that i understand that i do not have a connectionstring in my web.config. Or maybe I am not having something similar to what that has been mentioned in the websites. Should I have added the connection string explicitly before I started working on the project? But my project works fine in my local machine with the local database.
So is it possible that a project can work without a connection string when working with local database or am I missing something really important? Now when I need to publish the project i could add a connection string in the web.Config file? I have a connection string portion commented out in my web.release. Should i uncomment the connection string in web.release or i add a new connection string in web.config.
Web.Config
<configuration>
<configSections>
<section name="entityFramework" requirePermission="false" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<authentication mode="Windows" />
<authorization>
<deny users="?" />
</authorization>
</system.web>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<!-- So many dependent assembly -->
</assemblyBinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
Web.Release.config
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!--
<connectionStrings>
<add connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" name="MyDB" xdt:Locator="Match(name)" xdt:Transform="SetAttributes" />
</connectionStrings>
-->
</configuration>
I am using Visual Studio Express 2013 for web. Devloped application is a MVC5 web application. Database used Local DB. But i have copied the database into the intranet server using SSMS.
Update : added config details
Based on comments and answer i have added the following to my web.config
<entityFramework>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings>
<add
name="TEDALS_Ver01.DAL.TedalsContext"
connectionString="Server=FE0VMC0643; Database=TeDaLSdev; "
providerName="System.Data.SqlClient" />
</connectionStrings>
And this is the connection string that i have in the properties of the database
Data Source=FE0VMC0643;Initial Catalog=TeDaLSdev;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False

So is it possible that a project can work without a connection string when working with local database or am I missing something really important?
If you're using Entity Framework (I don't know about other ORMs) and you don't specify a connection string it will internally create one pointing to LocalDB or SQLExpress by using the full name of your DbContext.
It's explained in Entity Framework > Get Started > Connections and Models:
If you have not done any other configuration in your application, then calling the parameterless constructor on DbContext will cause DbContext to run in Code First mode with a database connection created by convention. […] uses the namespace qualified name of your derived context class […] as the database name and creates a connection string for this database using either SQL Express or LocalDb. If both are installed, SQL Express will be used.
If your database is not following the convention established by Entity Framework, then you either modify your database to have it following the convention or add a connection string for Entity Framework to use.
If your DbContext is
namespace MySolution.MyApplication.Contexts
{
public class MyContext : DbContext
{
[…]
}
}
either you have a database named MySolution.MyApplication.Contexts.MyContext in your (localdb)\MSSQLLocalDB / localhost\SQLExpress instance or add the following connection string to your App.config / Web.config:
<add
name="MySolution.MyApplication.Contexts.MyContext"
connectionString="Server=myServerAddress; Database=myDataBase; User Id=myUsername; Password=myPassword;"
providerName="System.Data.SqlClient" />

Related

Database not showing in SQL Server Explorer

I'm trying to use Entity Framework to update-database.
It's running fine with no errors. When I use -Verbose it's showing
Target database is: 'PokemonAppCore.PkmnContext' (DataSource: .\SQLEXPRESS, Provider: System.Data.SqlClient, Origin: Convention)
But in the app.config I've pointed it to a local db, like so. Nothing is showing up in SQL Server Explorer under LocalDb.
Why is it using .\SQLEXPRESS instead of localdb like it's specified in app.config?
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false" />
</configSections>
<connectionStrings>
<add name="PkmnConnectionString"
connectionString="Data Source=(localdb)\v11.0;Initial Catalog=PkmnDatabase;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient"
type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
At first and as mentionned by Mohammad Akbari, it's important to pass your connectionstring name on DbContext constructor.
I'd like to add one more thing, the startup project (in VS solution) should be the one where EF is used, the update-database command is seeking for web.config (or app.config) of startup project.
Regards,

Code-first EF database not auto-generating on startup

I have a new ASP.NET web application I am calling "Smartifyer" that was empty to start with. I added EntityFramework with Nuget and have created a model called WordModel and the following context:
public class SmartifyerContext : DbContext
{
public SmartifyerContext()
: base("SmartifyerDatabase")
{
}
public DbSet<WordModel> Words { get; set; }
}
Within Web.config in the root directory I have the following configurations:
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="SmartifyerDatabase"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\SmartifyerDb.mdf;Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
When running a test on the context, the Find method takes about 30 seconds until it times out with the following error:
System.Data.SqlClient.SqlException: 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 have a pretty poor understanding of setting up SQL connections and what all the configurations within Web.config do. I would like the .mdf database file to be auto-generated if it is not present but I am unsure how to modify my configurations to do that.
EDIT WITH SOLUTION:
All of my connection config and database stuff was setup correctly. My problem was that I was running a unit test which was using the test project's app.config file rather than the main project's web.config. Copy and pasting my web.config into the test project's app.config fixed the issue!
The error you're getting is because the EF is not able to locate the database instance service. The EF have default connection string that connects to developer database known as LocalDB\V11.0. Here your application is trying to connect to LocalDB\V11.0 as specified in ConnectionString.
<add name="SmartifyerDatabase" connectionString="Data Source=(LocalDB)\v11.0; ......
You must ensure that LocalDB instance is installed on you machine. see this answer. If it's not there install it and try to connect manually to the LocalDB instance then try with the application. Hope this will resolve your issue.
For information about the SQL LocalDB see this MSDN article.
You can also use your SqlExpress or other Sql database if it's installed on your machine. Just change the DataSource property in the connectionstring and you will be good to go.

Issues when installing AspxCommerce onto GoDaddy Servers

I have been up all night trying to figure it out and when I try and install AspxCommerce onto my GoDaddy server. I have ran across multiple issues when trying to install this, all of the answers have been found after long research although for This last issue I am getting Configuration Error saying that I do not have any connection to 'SageFrameConnectionString'. Does anybody have any thoughts on how I should go about doing this including the necessary steps to get there?
PS: Everything works great when installed locally..
web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<roleManager enabled="true" defaultProvider="SageFrameSqlRoleProvider">
<providers>
<clear />
<add connectionStringName="SageFrameConnectionString" applicationName="SageFrame" name="SageFrameSqlRoleProvider" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<add applicationName="SageFrame" name="SageFrameWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</providers>
</roleManager>
</system.web>
</configuration>
connectionstring.config:
<?xml version="1.0"?>
<connectionStrings>
<clear />
<add name="SageFrameConnectionString" connectionString="Data Source=;Initial Catalog=;Integrated Security=False;Persist Security Info=False;User ID=;Password=;Connect Timeout=120" providerName="System.Data.SqlClient" />
</connectionStrings>
Actually it is not a AspxCommerce problem. It's a GoDaddy server problem.
GoDaddy doesnt allow roles unless you use LocalSqlServer as your membership provider connection string.For GoDaddy you have couple of change in AspxCommerce .
In connectionstring.config file :
<connectionStrings>
<clear />
<add name="LocalSqlServer" connectionString="Data Source=.;Initial Catalog=YourCatlog;Integrated Security=False;Persist Security Info=False;User ID="ID";Password="PSWD";Connect Timeout=120" providerName="System.Data.SqlClient" />
</connectionStrings>
And In library go to SageFrame.Common\Setting\SystemSetting.cs on Declaration section :
replace
public static string SageFrameConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["SageFrameConnectionString"].ToString();
with
public static string SageFrameConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString();
Note : Don't forget to rebuild library.

SQL Server Object Explorer doesn't show my database

I started a MVC project on VS2012, created my models, my controllers and my views. I'm using Entity Framework code-first, so I've enabled migrations and I'm updating the database accordingly to the changes in the code.
The pages run just fine, the database is working properly, the data is being retrieved as it should, so functionally, everything is fine.
The problem is that I see no connection string in my web.config:
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=301880
-->
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
Nor do the database shows up in the SQL Server Object Explorer...
I've already checked this answer, but the server on my Object Explorer was already the correct.
I think you are connected using Default Connection Factory, it allows the program to locate the database ONLY when there are no connection strings specified in the config file.
As in your web.config, I've found
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
Your program may be using this default connection to connect the database.
The Default Connection may have been set up when you installed the EF Nuget Package.
For more about Default Connection Factory, take a look at http://msdn.microsoft.com/en-au/data/jj556606.aspx#Factory

Connection String in separate config file with EF model designer

How do i get the EF model designer at design time see the connection string in a separate config file without prompting for 'Choose you data connection' when i try and update model from database.
I have a separate config file for connection strings to run against different environments. In app.config i use <connectionStrings configSource="connections.config">.
I do not want to save the connection in app.config or web.config. Run time works fine just seems to be a big limitation on the designer.
To reproduce the problem simply create an new ADO.net Entity Data Model. Store the connection string in the app.config. you will get an app fonfig like below
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings>
<add name="Entities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=myserver;initial catalog=Devdb;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
Now copy the connectionstring and put it in a new file 'myconnections.config'
<?xml version="1.0" encoding="utf-8" ?>
<connectionStrings>
<add name="Entities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=myserver;initial catalog=Devdb;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
Edit the app.config and change the connection string settings to
<connectionStrings configSource="myconnections.config">
The model designer will now not know about the connection. If you click 'Update Model from Database' it will prompt you for the connection and want to save a connection back to the app.config. Really frustrating.
Well I know this question is 5 years old, but I actually found the solution. I am using VS2019 and when I add a new data connection in Server Explorer, EF will see my new connection. While this is not actually the perfect solution because we have to add connection string separately, it still works.

Categories

Resources