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
Related
Sorry for that if it's repeating question but I could not find anything helpful.
I've developed a C# Winforms Application (Hostel Management System) using VS 2017 + MS SQL Server 2017
with EF 6.x. It is fully functional on my system (on which it's being developed). But now I wanna deploy it to my client. When I install it on any client machine and run, it works fine until any Database operation not happens. As soon as DB operation occurs, Application stops working and shows the error that it can't get connected to EF like this.
App.config is like shown 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.6.1" />
</startup>
<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>
<connectionStrings>
<add name="DBEntities" connectionString="metadata=res://*/DBModels.csdl|res://*/DBModels.ssdl|res://*/DBModels.msl;provider=System.Data.SqlClient;provider connection string="data source=(localdb)\ProjectsV13;initial catalog=HMSDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
Anyone here who can help me out to deploy my app successfully
I am very new to Entity Framework so apologies if this question is basic.
I am working through a Code First text book, and have created a small class library (c#) and a console app. The text book indicates that when I run it Entity Framework will build the database in SQL server automatically. It doesn't and the reason I believe is because I have a named instance rather than the default instance \SQLExpress. The text book indicated I did not need a connection string when using the default instance, but since I am not using a default instance I am guessing I do need a connectionm string in App.Config. I have tried putting a standard connection string in this file but it does not work.
Here are the entire contents of my App.Config file
<?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.2" />
</startup>
<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>
Could someone please advise where the connection string goes, or tell me if I am going about this the wrong way.
UPDATE
Thanks to the help received so far, my App.Config file now looks as follows -
<?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>
<connectionStrings>
<add name="Model" connectionString="Server=OFFICE-ACER\SQLE;Database=BreakAway;Trusted_Connection=True;" providerName="System.Data.EntityClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<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>
However, nothing happens i.e. no database is created as before.
Under your <configSections> tag, you can place this
<connectionStrings>
<add name="myConn" connectionString="theConnString" providerName="System.Data.SqlClient" />
</connectionStrings>
And then replace the values with what you need
Also although it may not help you in this situation, to avoid having to manually do this in future, the EntityFramework NuGet package works wonders, you just input the database's url and your login - then it creates the entire connection string for you in your config file, creates your edmx and allows you to import the data of your choice
This should do it:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="putYourEntityName" connectionString="putYourConnectionStringHere" providerName="System.Data.EntityClient"/>
</connectionStrings>
</configuration>
<configuration>
<configSections>
...
</configSections>
<connectionStrings>
<add name="Name" connectionString="Data Source=servername; Initial Catalog = yourDbName ; Intgrated Security ="According to your requirement" providerName="System.Data.SqlClient" />
</connectionStrings>
...
First thing that You need to know. If You are using a Class Library (.dll), the Entity (.edmx) file created inside the .dll, and You are invoking this Method, from an MVC Application (That have a web.config). The connection string inside of the App.config will never be used.
So You can have the Connection string mapped on the Web.Config (MVC Project), and even delete from the App.Config. It will always use the web.config.
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,
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" />
Trying to access asp.net configuration and get this '(provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)'
already tried these solutions: http://forums.asp.net/t/1396682.aspx?Could+not+establish+a+connection+to+the+database+for+asp+net+configuration+
http://forums.iis.net/t/1163032.aspx?
Provider+Management+error+of+asp+net+configaration+in+asp+net
Asp.Net membership via ASP.NET Website Administrator Tool
UPDATE: I took a look in the SQL Server Configuration Manager and selected SQL server Service and it had a error "The remote procedure call failed.[0x800706be]" What I don't get is what caused this, was it the web.config or the sql server itself. To my knowledge the SQL server file is still in one place and that's it, I made a Connection string in VS to check if everything is OK and the server contained all the tables needed and everything was alright so I don't get the catch here.
here is 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=169433
-->
<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>
<system.web>
<roleManager enabled="true" />
<authentication mode="Forms" />
<compilation debug="true" targetFramework="4.5">
<assemblies>
<add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
<add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
</assemblies>
</compilation>
<httpRuntime targetFramework="4.5" />
</system.web>
<connectionStrings>
<add name="ConnectionStringTest" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Project.mdf;Integrated Security=True;MultipleActiveResultSets=True;Connect Timeout=30;Application Name=EntityFramework"
providerName="System.Data.SqlClient" />
<add name="ASPNETDB" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\ASPNETDB.mdf;Integrated Security=True;MultipleActiveResultSets=True;Connect Timeout=30;Application Name=EntityFramework"
providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>
</appSettings>
<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>