Entity Framework connectionString with remote server (URL) - wpf

This is a WPF app which references an Entity Framework class library. In the App.config of the class library project I have 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=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
</configuration>
All very standard. What I want to do is to add a connection string which will enable me to connect to remote server via a URL to the database. Is this going to be possible?
I've tried something like this:
<connectionStrings>
<add name="LanguageEntities" connectionString="metadata=res://*/LanguageModel.csdl|res://*/LanguageModel.ssdl|res://*/LanguageModel.msl;provider=System.Data.SqlClient;provider connection string="data source=http://www.myweb.net:667;User ID=Remote;Password=cleverstring;initial catalog=LanguageModel.LanguageEntities;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
But my app seems to default to the local connection without giving me an error message.
Any suggestions gratefully received.
Many thanks!

Related

SQL connection string from ASP.NET app does not work in WPF

Several ASP.NET applications are running on the server of our company.
Connections strings in web.config look like this:
<add name="somename"
connectionString="Data Source=SERVER\INSTANCE; Initial Catalog=DbName;User Id=user; Password=*******; MultipleActiveResultSets=True; Integrated Security=true"
providerName="System.Data.SqlClient"/>
and work perfectly with no errors.
But when I copy this connection string from web.config to app.config of my WPF app, I get the following errors:
Login failed. The login is from an untrusted domain and cannot be used with Windows authentication.
Or (if I remove Integrated Security from string)
Login failed for user 'username'.
Also I cannot log in via SQL Server Management Studio (causes the same errors).
My app.config 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="somename"
connectionString="Data Source=SERVER\Instance;Initial Catalog=DbName; User Id=user;Password=******;MultipleActiveResultSets=True;Integrated Security=true"
providerName="System.Data.SqlClient"/>
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
</startup>
<entityFramework>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>
</configuration>
Both username and password are 100% correct, SQL Server uses mixed authentication.
Managed to log in with sa login of SQL Server
Edit the registry using regedit. (Start –> Run > Regedit )
Navigate to: HKLM\System\CurrentControlSet\Control\LSA
Add a DWORD value called “DisableLoopbackCheck”
Set this value to 1

Change connection string in Entity Framework from localDB to Microsoft SQL Server

I'm trying to use SQL Server 2016 instead of localdb. When I try to change it in the web.config file from mssqllocaldb to v13.0
it gives me the following error:
An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll but was not handled in user code.
Additional information: A network-related or instance-specific error
occurred while establishing a connection to SQL Server. The server was
not found or was not accessible. Verify that the instance name is
correct and that SQL
You can read about the configuration at MSDN: Entity Framework Config File Settings
An example web.config could be:
<?xml version="1.0"?>
<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=MySqlServerInstance;Initial Catalog=MyDatabase;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>
</configuration>

How to load a App.Config file into a VCL application?

I am trying the RemObject Hydra to embed a WPF module inside a VCL app.
In this WPF module, I have Grid Controls, and EntityFramework DataContext. It's connectionString, providers and everything stored in a app.config.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="ModelConnection" connectionString="character set=UTF8;data source=localhost;initial catalog=PATHTODATABASE.FDB;user id=SYSDBA;password=MASTERKEY" providerName="FirebirdSql.Data.FirebirdClient" />
</connectionStrings>
<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="FirebirdSql.Data.EntityFramework6.FbConnectionFactory, EntityFramework.Firebird" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="FirebirdSql.Data.FirebirdClient" type="FirebirdSql.Data.EntityFramework6.FbProviderServices, EntityFramework.Firebird" />
</providers>
</entityFramework>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="FirebirdSql.Data.FirebirdClient" publicKeyToken="3750abcc3150b00c" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="4.5.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.data>
<DbProviderFactories>
<remove invariant="FirebirdSql.Data.FirebirdClient" />
<add name="FirebirdClient Data Provider" invariant="FirebirdSql.Data.FirebirdClient" description=".NET Framework Data Provider for Firebird" type="FirebirdSql.Data.FirebirdClient.FirebirdClientFactory, FirebirdSql.Data.FirebirdClient" />
</DbProviderFactories>
</system.data></configuration>
The next step was to make the VCL host with delphi then load the WPF module's .dll using the HYModuleManager component. This is pretty straigtforward :
procedure TMainForm.FormCreate(Sender: TObject);
begin
HYModuleManager1.LoadModule('%path%\to\wpf\module\GridsModule.dll');
HYModuleManager1.CreateVisualPlugin('ClientGridView', fInstance, Panel1);
end;
I run the application in Delphi. But this error is thrown :
(Translation : An exception was thrown by the target of an invocation)
So with a little more research, I run this application (HydraHost.exe) using VisualStudio, and realize that this si the exception thrown by the module :
System.InvalidOperationException : 'No connection string named 'ModelConnection' could be found in the application config file.'
I then decide to write a WPF host. I set the WPF Host App.config to include the connection string and provider infos. To no surprise, everything works fine.
So, with the VCL host, the connectionString is not found. With the WPF host, it is. My conclusion is that the app.config is not loaded by the VCL host but it is loaded by the WPF host.
So what I did next is to manually copy the WPFHost.dll.config to VCLHost.exe.config as suggested by one of the answers, but same error, connectionString is not found.
So my question is : How can I "link" a .exe.config to a VCL app built with Delphi?
You should rename your app.config to YourExeName.exe.config as it's probably now YourDllName.dll.config

Application with no connection string - Where is the DB hidden?

So, I have created a console application, added the entity framework nuget package, created a dbcontext and enabled migrations for my project.
However, I don't see the database anywhere. The database exists since I can query it when I run the application but since I haven't entered a connection string I don't see it anywhere in my SQL Management Studio.
My question is, where do Visual Studio "hide" this database?
Btw, I'm using EF6 Code First.
EDIT: Here 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>
<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>
Add this to constructor of the context to see the default connection string used.
public class MyContext : DbContext
{
public MyContext()
{
Console.WriteLine(this.Database.Connection.ConnectionString);
}
}
Usually it creates the database in v11.0 or mssqllocaldb(in you app.config it is mssqllocaldb) instance of sqllocaldb.
The database file can be located at Users folder(Windows 8.1):
C:\Users\{UserName}\ApplicationName.ContextName.mdf

How to specify Entity Framework Code First connection string in external Enterprise Library config file

I am creating a WPF application, having classical architecture: UI layer, business logic layer and infrastructure layer. I decided to split configuration in two files: app.config file, containing common app configuration, and dll.config, containing connection string to use in DbContext for domain model storage. The second .config file should be sticked to business logic DLL, while first file sticked to corresponding UI executable (there will be one more UI with it's own configuration).
app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<enterpriseLibrary.ConfigurationSource selectedSource="Winter DAL Configuration">
<sources>
<add name="Winter DAL Configuration" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
filePath="dll.config" />
</sources>
</enterpriseLibrary.ConfigurationSource>
</configuration>
dll.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<dataConfiguration defaultDatabase="WinterContext" />
<connectionStrings>
<add name="WinterContext" connectionString="Data Source=Winter.sdf"
providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>
</configuration>
Now, when I'm starting the app, DbContext throws an exception saying it cannot find connection string with specified name. If I move connection string from dll.config to app.config - all working fine.
Maybe I must explicitly load configuration somehow? Or?.. What do I do wrong?
Thx in advance!
Based on the config files supplied, Entity Framework went looking in app.config of root in start project and didnt find the EF content. Especially the Connection string.
When you first used UF it would have created such entries, perhaps in the model project.
IM using EF5.0 so careful with cut and paste on Entity Section .
<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=5.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="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
<connectionStrings>
<add name="CONTEXT_NAME_HERE"
providerName="System.Data.SqlClient"
connectionString="Data Source=YOURDB_SERVER;Initial Catalog=YOUR_DBNAME;Integrated Security=True;MultipleActiveResultSets=True;App=EntityFramework" />
</connectionStrings>
</configuration>
I hope I understood your question, you want to structure your app.configs as a hierarchy, which is valid, in web applications that is very common, the easiest way to let .net help you with the merging is specifiying the regions of app.config in all those configurations that need to be bounded, and in the inner configurations you can override the values until you have the last one, for example:
ROOT APPCONFIG
<configuration>
<commonCollection>
<add key="first" value="1" />
<add key="second" value="2" />
<add key="third" value="3" />
</commonCollection>
</configuration>
INNER APPCONFIG
<configuration>
<commonCollection>
<remove key="first" />
<add key="first" value="10" />
</commonCollection>
</configuration>
Results:
ROOT APPCONFIG
first : 1
second: 2
third: 3
INNER APPCONFIG
first: 10
second: 2
third: 3
EDIT
for your EF context, to set the connection string in the constructor :
public MyContextDB() :base("name=DefaultConnection")
{
//other initializers
}
hope it helps,

Resources