Entity Framework in Windows Service - c#

I have solution with 3 projects:
1) - library
2) - test (windows forms application)
3) - windows service
Library uses Entity Framework. When i link it to test application it works. My App.config (for test application) looks like this:
<?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="ITSBPEntities1" connectionString="metadata=res://*/DBModel.csdl|res://*/DBModel.ssdl|res://*/DBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=192.168.0.131;initial catalog=ITSBP;persist security info=True;user id=user;password=pass;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
I've referenced to library in service also. I installed it and run, but when it try to use Entity Framework some exception is thrown, there is log:
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. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.
App.config in service project is copy-pasted from test application. EntityFramework.dll exists in debug folder.

Related

ConnectionString with DataDirectory (c#, sqlserver)

I created an application in c# with wpf using EntityFramework.
When I create the setup it works fine on my computer but when I install it on another computer I get this error
A file activation error occured. The physical file name \Data\myDb.mdf may be incorrect. Diagnose and correct additionnal errors, and retry the operation. CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
Here is my 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="myConnectionString" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;
AttachDbFileName='|DataDirectory|\Data\myDb.mdf';
Initial Catalog=myDb.mdf; Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<entityFramework>
<defaultConnectionFactory type="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 could try to set the value for |DataDirectory| during runtime:
AppDomain.CurrentDomain.SetData("DataDirectory", #"C:\Program Files (x86)\Company\AppFolder\")
Or replace |DataDirectory| with an absolute path in your configuration file.

Changing the database in app configuration doesn't work

I have created my c# application using entity framework. The app config generated when creating data model looks like following. I want to change the name of the database and run application with a different database by running all the query changes I made when making the application. I created a new database with a different name and added all the query changes to it. And when I run the application it shows that table names doesn't exist though they exist already in the database.
My app config looks like this.
the changed config line with new database name is in the comments.
<?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>
<connectionStrings>
<add name="RIT_Allocation_Entities" connectionString="metadata=res://*/DataControllers.JobAllocationDataModel.csdl|res://*/DataControllers.JobAllocationDataModel.ssdl|res://*/DataControllers.JobAllocationDataModel.msl;provider=System.Data.SqlClient;provider connection string='data source=.;initial catalog="RIT_POS_DB ALUTECH HO 12112018";integrated security=True;MultipleActiveResultSets=True;App=EntityFramework'" providerName="System.Data.EntityClient" />
<!-- <add name="RIT_Allocation_Entities" connectionString="metadata=res://*/DataControllers.JobAllocationDataModel.csdl|res://*/DataControllers.JobAllocationDataModel.ssdl|res://*/DataControllers.JobAllocationDataModel.msl;provider=System.Data.SqlClient;provider connection string='data source=.;initial catalog="RIT_POS_DATABASE";integrated security=True;MultipleActiveResultSets=True;App=EntityFramework'" providerName="System.Data.EntityClient" /> -->
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
How to change my database and add the table changes to it and run my application? Or do I need to change the database name somewhere else in the application?
I'm working with one project creating the application.

DESIGN TIME: No connection string named could be found in the application config file

I was using WPF Windows and ADO.NET EF 6x, VS 2015 update 3.
The error "No connection string named could be found in the application config file" only when the design time.
The image at design time
But when I run it it was compiled without error and work perfectly.
The image at run time
How to fix this?
My 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="knightitEntities" connectionString="metadata=res://*/KnightModel.csdl|res://*/KnightModel.ssdl|res://*/KnightModel.msl;provider=System.Data.SqlClient;provider connection string="data source=DESKTOP-ELNOS23;initial catalog=knightit;user id=user;password=password;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<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>
</configuration>

Unhandled exception running an application on Windows XP but works fine on Windows 7

We have an application that is going to be deployed on Windows XP that uses Entity Framework 6 and SQL Server Express.
Here's how the application's app config file looks :
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
<connectionStrings>
<add name="Rust" connectionString="Data Source=(ip here).\SQLEXPRESS;Initial Catalog=Rust;Integrated Security=True" 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>
Whenever we change the connection string to connect to an actual server and the application is being ran on Windows 7 it works fine. But if it is ran on Windows XP there occurs an unhandled exception which we cannot trace:
It happens when we try to retrieve some information from the database
Task.Factory.StartNew(() =>
{
//here
departments = new ObservableCollection<Department>(this.GetAllDepartments());
});
The SQL Server to which we connect is an SQL Server 2012 Express. Here's how the App config on the data access layer looks like:
<?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>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
</configuration>
Here's what the problem was :
We have been using a VM named VirtualBox which had Windows XP installed on it. We tried installing Visual Studio 2010 and debugging the application from there but that wasn't successful. After two days of banging our heads about why this doesn't work we tried using the VMWare workstation and it it worked fine!! There must be some kind of problem with the VirtualBox software.
Thank you guys for the suggestions!

Upgraded EF5 to EF6 - getting No Entity Framework provider found

I have updated my project from EF5 to EF6, using NuGet, but am getting an error now as soon as my application tried to query the database.
error 0152: No Entity Framework provider found for the ADO.NET
provider with invariant name 'System.Data.SqlClient'. Make sure the
provider is registered in the 'entityFramework' section of the
application config file. See
http://go.microsoft.com/fwlink/?LinkId=260882 for more information.
I think the issue is with my data project's App.Config, which resembles this:
<?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="AccuFinanceEntities" connectionString="metadata=res://*/AccuFinanceEntities.csdl|res://*/AccuFinanceEntities.ssdl|res://*/AccuFinanceEntities.msl;provider=System.Data.SqlClient;provider connection string="data source=laptop;initial catalog=mydb;Integrated Security=True;MultipleActiveResultSets=True;App=AccuFinance"" 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>
</configuration>
My web.config only has a connection string applied:
<connectionStrings>
<!--SERVER -->
<!--LOCAL-->
<add name="AccuFinanceEntities" connectionString="metadata=res://*/AccuFinanceEntities.csdl|res://*/AccuFinanceEntities.ssdl|res://*/AccuFinanceEntities.msl;provider=System.Data.SqlClient;provider connection string="data source=laptop;initial catalog=mydb;Integrated Security=True;MultipleActiveResultSets=True;App=AccuFinance"" providerName="System.Data.EntityClient" />
</connectionStrings>
I think I need to add or remove something from one of these, but not sure which. It worked fine under EF5. Project compiles and builds, and I can edit my EDMX file (I can update model from database).
What am I doing wrong?

Categories