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,
Related
I develop a web api project with ASP.NET. I want to establish a connection between ADO.NET and the databases I have added, but I keep encountering this error:
The ADO.NET provider with invariant name 'System.Data.SqlClient' is either not registered in the machine or application config file, or could not be loaded. See the inner exception for details.
I searched the internet for a solution but couldn't find it. The solutions in this thread didn't work: Entity Framework ADO.NET Sql.Data.Client provider not found.
What should I do?
<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="MyModel1Entities" connectionString="metadata=res://*/Models.MyModel1.csdl|res://*/Models.MyModel1.ssdl|res://*/Models.MyModel1.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=MyModel1;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="MyModel2Entities" connectionString="metadata=res://*/Models.MyModel2.csdl|res://*/Models.MyModel2.ssdl|res://*/Models.MyModel2.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=MyModel2;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</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>
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'm setting up a solution containing two projects: 1) my class library and 2) my web UI. Ultimately the website will be used to show query results from an existing mysql database.
I struggled to create the ado.net entity model for a while. But I thought I had figured it out after updating the mysql connector for visual studio and following the instructions here: https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework60.html
Now that the entity model is created I am struggling to create a simple controller with scaffolding. I've double checked my connection string and ef provider in web.config and app.config.
I've looked through these questions but have not found my answer:
Exception has been thrown by the target of an invocation- while creating the controller
Application can't scaffold items
Enity Framework With MySQL
I'm pretty sure the issue is in how I have configured it. For a while I thought that what is in app.config didn't matter because the UI project (containing web.config) was set as the startup project. Now I'm just confused. Which is why I have included both below. Any insight is greatly appreciated.
From web.config:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-MySolution.UI-20161111094519.mdf;Initial Catalog=aspnet-MySolution.UI-20161111094519;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="MySolutionEntities" connectionString="metadata=res://*/Entities.DevData.csdl|res://*/Entities.DevData.ssdl|res://*/Entities.DevData.msl;provider=MySql.Data.MySqlClient;provider connection string="server=****;user id=****;password=****;database=mydatabase""
providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
<provider invariantName="System.Data.MySqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFrameworkSqlServer" />
</providers>
</entityFramework>
From app.config:
<?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>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient"
type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
<provider invariantName="MySql.Data.SqlClient"
type="MySql.Data.Entity.SqlServer.ProviderServices, EntityFramework.SqlServer"/></providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
<connectionStrings>
<add name="MySolutionEntities" connectionString="metadata=res://*/Entities.DevData.csdl|res://*/Entities.DevData.ssdl|res://*/Entities.DevData.msl;provider=MySql.Data.MySqlClient;provider connection string="server=****;user id=****;password=****;database=mydatabase""
providerName="System.Data.MySqlClient" />
</connectionStrings>
</configuration>
Late answer but I hope it helps somebody
I had same problem, I just solve it by uninstalling and reinstalling entity-framework through the nuget package manager console.
o Run Package Manager Console :
Tools -> NuGet Package Manager -> Package Manager Console
To Remove:
UnInstall-Package EntityFramework
To Reinstall:
Install-Package EntityFramework
I hope this works for you as well
Ok I'm not exactly sure how I solved this problem but I figured that I should try to describe it. I started over my solution in the same way and created the entity model. Then instead of using the code in the providers tag from that article at dev.mysql.com (link above) I found this in my app.config:
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
</providers>
I copied the same thing over to my web.config. Then I was able to create the controller with scaffolding.
After 8 hours of struggle debugging EFPowerTools, I found at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\PrivateAssemblies was Mysq.Data and Mysql.Data.Ef6 which were a diffrent version [6.9.8] from the ones in my web config [6.10.9] i copied the versions in my bin 6.10.9 to the above PrivateAssemblies folder and every thing worked fine.
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 have a project that is been started two months ago ! i'm using [LASG]:https://layerguidance.codeplex.com/ Architecture for my Solution !
i'm working in my solution well since i have received the " No Entity Framwork provider found for the ADO.net Provider with invarient name 'System.Data.SqlClient' "
the application config file contain the full block of provider
this is 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>
<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="MASTERGESTION" connectionString="Data Source=ZZZZ\ZZZZZ;Initial Catalog=XXXXXX;User ID=YY;Password=ZZZZZZ" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
I copied the project in other computer, after building the solution, it start normally and without provider error
I rebuild the solution many times, i re-add references manually with no response!
Any help please? i loose six hours for no reason
The defaultConnectionFactory shouldn't be:
System.Data.Entity.Infrastructure.LocalDbConnectionFactory
Instead make it:
System.Data.Entity.Infrastructure.SqlConnectionFactory
I assume it worked on the second machine as that has LocalDb installed and the first one doesn't.
Try instead of
User ID=YY;Password=ZZZZZZ
to use
Integrate Security=True
also add in <context>
<context type="tablename, databasename">
<databaseInitializer type="databasename.Migrations.tablenameInitializer, databasename" />
</context>