Deploy C# app with Entity framework+SQL Server Database - c#

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

Related

LocalDatabase- Setup installation problem

I have c# winfrm project with Local database. I created setup File with attaching database.
But I get following error.
A file activation error occurred. The physical file name 'InventoryDb.mdf' may be incorrect. Diagnose and correct additional errors, and retry the operation.
It's working fine in the same computer as I have my project, but not in othet Windows 7 whe nI install it.
This is my connection string in App.config page
<?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="Db" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\InventoryDb.mdf;Integrated Security=True;Connect Timeout=30; MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
I wonder what is I'am missing? Do I have wrong connectionstring? I would appricate for your help.

Entity Framework Connection String in App.Config

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.

EF6 with localdb v13 underlying connection failed to open

I have a simple console app with EF6, my PC has localdb v13 and when I try to connect to the EF database, I get this error
{"Cannot open database \"NinjaDomain.DataModel.NinjaContext\" requested by the login. The login failed.\r\nLogin failed for user 'xx\\xx.yy'."}
this is the 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>
<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>
It is does not matter Console app might also have an App.config, normally if you have installed EF from Package Manager Console like that:
install-package entityframework
The app.config will be automatically added to your project file.
This is the default connection string, if the do not have it the app.config (related to EF v 6.1.3)
"Data Source=(localdb)\\mssqllocaldb;Initial Catalog=MyConsoleApplication.Program+MyDbContext;Integrated Security=True;MultipleActiveResultSets=True"
Below, I have changed the app.config (If you do not have you can just add it to your project) and I have provided the connection string
<?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>
<connectionStrings>
<add name="DatabaseConnectionString"
connectionString="Integrated Security=SSPI; Initial Catalog=UsersDatabase5; Data Source=.\;" providerName="System.Data.SqlClient" />
</connectionStrings>
<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>
You should pass the connection string to the DbContext constructor with 'name' keyword like that:
using (var myDbContext = new MyDbContext("name=" + "DatabaseConnectionString"))
{
....
I have used the normal SQL Server in my app.config example:
Data Source=.\;
You can change it:
Data Source=(localdb)\\mssqllocaldb;
or just check your SQL Server instance name (use SSM or command line).

Error accessing the entity model in a seperate project

I have two projects one of which contains the entity model and acts as the DAL and the other one consumes that model and implements the application logic.
The model is a class library project and the application is a windows forms projects.
Having the following app.config files of the projects, and having referenced System.Data.Entity in the forms project, when I run the WinForms app, I get the following error:
Additional information: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application
Where you think the problem might be?
DAL Project's 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>
<connectionStrings>
<add name="ProviderInterfaceContext" connectionString="metadata=res://*/ProviderInterfaceModel.csdl|res://*/ProviderInterfaceModel.ssdl|res://*/ProviderInterfaceModel.msl;provider=System.Data.SqlClient;provider connection string="data source=192.168.2.12;initial catalog=ProviderInterface;user id=--;password=---;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<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>
<system.serviceModel>
<client>
<endpoint address=""
binding="basicHttpBinding"
contract="PrizeServiceSoap" name="PrizeServiceSoap" />
</client>
</system.serviceModel>
</configuration>
The WinForms 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>
<connectionStrings>
<add name="ProviderInterfaceContext" connectionString="metadata=res://*/ProviderInterfaceModel.csdl|res://*/ProviderInterfaceModel.ssdl|res://*/ProviderInterfaceModel.msl;provider=System.Data.SqlClient;provider connection string="data source=192.168.2.12;initial catalog=ProviderInterface;user id=---;password=----;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<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>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
Fixed.
Added reference to EntityFramework 6.0 and EntityFramework.SqlServer and everything is working now.

No Entity Framework Provider Found

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>

Categories

Resources