Changing the database in app configuration doesn't work - c#

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.

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.

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>

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?

How can I connect to database my application is using & explore into it?

I have created a simple application as explained here on MSDN, this is simple Code First workflow example, this works fine, it stores data and displays stored data as well . But I don't know to how can I access database it is using? And how can I connect to that database to explore into entered data?
Note: I am using EF6 with VS 2013 ultimate
App.config is:
<?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.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>
In VS 2013 ultimate
View->SQL Server Object Explorer
and there you will find your databases.
Check the connection string in the config file.
Now that you added the config try:
(local)\mssqllocaldb or .\mssqllocaldb

Entity Framework in Windows Service

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.

Categories