Connection String in separate config file with EF model designer - c#

How do i get the EF model designer at design time see the connection string in a separate config file without prompting for 'Choose you data connection' when i try and update model from database.
I have a separate config file for connection strings to run against different environments. In app.config i use <connectionStrings configSource="connections.config">.
I do not want to save the connection in app.config or web.config. Run time works fine just seems to be a big limitation on the designer.
To reproduce the problem simply create an new ADO.net Entity Data Model. Store the connection string in the app.config. you will get an app fonfig like 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.5" />
</startup>
<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>
<connectionStrings>
<add name="Entities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=myserver;initial catalog=Devdb;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
Now copy the connectionstring and put it in a new file 'myconnections.config'
<?xml version="1.0" encoding="utf-8" ?>
<connectionStrings>
<add name="Entities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=myserver;initial catalog=Devdb;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
Edit the app.config and change the connection string settings to
<connectionStrings configSource="myconnections.config">
The model designer will now not know about the connection. If you click 'Update Model from Database' it will prompt you for the connection and want to save a connection back to the app.config. Really frustrating.

Well I know this question is 5 years old, but I actually found the solution. I am using VS2019 and when I add a new data connection in Server Explorer, EF will see my new connection. While this is not actually the perfect solution because we have to add connection string separately, it still works.

Related

Deploy C# app with Entity framework+SQL Server Database

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

Entity Framework 6 Code First to RDS trying to Create Database

I have an existing database and also some existing code that I'd like to push to that database using the Entity Framework (v6.0.0.0).
I am having no problem connecting to the DB, and I have examined the context and migrations files, all look good to me.
When I run the Update-Database -Verbose command, I'm getting the following error:
CREATE DATABASE permission denied in database 'master'.
I'm not clear why it's even trying to create a database, b/c one already exists, and I'm specifying the DB in the settings.
Here is my 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=xxxxxxxxxxxxx" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="Server=[xxxxxxxxxx].rds.amazonaws.com,1433;Database=[xxxxxxxxxx];UID=[xxxxxxxxxx];PWD=[xxxxxxxxxx]" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings>
<add name="DataModel" connectionString="Server=[xxxxxxxxxx].rds.amazonaws.com,1433;Database=[xxxxxxxxxx];UID=[xxxxxxxxxx];PWD=[xxxxxxxxxx]"/>
</connectionStrings>
</configuration>
Any reason it would be ignoring my DB name and trying to create a new one from scratch?
So I figured it out...
The solution (in my case at least) was that I had two connection strings in my app.config file.
I had one in the <connectionsStrings> section and another in the <parameters> section (see above).
At one point I even commented out the one in <connectionStrings> because the other one seemed to look more like an Entity Framework version.
As it turns out, I followed the advice I found in a few other answers regarding setting up an empty class in the DbContext file and hard-code it to the name listed in the <connectionStrings> section.
So like this:
public class YourContext : DbContext
{
public YourContext() : base("name=DefaultConnection")
{
}
public DbSet<aaaa> Aaaas { get; set; }
}
After I did that, I got the following error:
The connection string 'DataModel' in the application's configuration file does not contain the required providerName attribute."
So, I changed my connectionStrings to look like this:
<connectionStrings>
<add name="DataModel" connectionString="Server=[xxxxxxxxxx].rds.amazonaws.com,1433;Database=[xxxxxxxxxx];UID=[xxxxxxxxxx];PWD=[xxxxxxxxxx]" providerName="System.Data.SqlClient" />
</connectionStrings>
And after that, I got the following error:
CREATE TABLE permission denied in database '[xxxxxxxxx]'.
So I went back to the DB and added the db_ddladmin Database Role for the DB in question.
After that, I ran Update-Database and it worked!
Of course, I went back in and removed the db__ddladmin permission and also the View any database server permission.

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.

Database not showing in SQL Server Explorer

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,

Entity framework, dll, excel

I have written dll in c# which is used by Excel (the dll is COM registered). I have no problem connecting with Excel. The dll retrieves data from a SQL Server database using Entity Framework 5. If I run the dll through a console app, the dll works fine. But when I run it through Excel (through VBA) I get a an InvalidOperationException. The error message is "No connection string named "MegaDailyEntities' could be found in the application config file. This occurs the first time I try to retrieve data from the database.
I ran into this problem with the console app, but then I include the following in my App.config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
<connectionStrings>
<add name="MegaDailyEntities" connectionString="metadata=res://*/MegaDaily.csdl|res://*/MegaDaily.ssdl|res://*/MegaDaily.msl;provider=System.Data.SqlClient;provider connection string="data source=CQI-Laptop1;initial catalog=MegaDaily;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
and it worked fine.
So my question is, how do I get Excel to use this connection? Is there a config file for Excel?
Thanks
The reason why connection string is not found is because .config files exist only for applications (.exe) but not for additional libraries (.dll)
Excel process tries to load connection string from Excel.exe.config, instead of loading from yourlibrary.dll.config
Working solution for this is to edit your *.context.tt file (EntityFramework classes generator) and update contructor for you DbContext.
You will find something like:
public <#=code.Escape(container)#>()
: base("name=<#=container.Name#>")
{
...
}
It should be possible to change it into
public <#=code.Escape(container)#>()
: base("your EF connection string")
{
...
}
Or take a look how to set connection string from code

Categories

Resources