Entity framework, dll, excel - c#

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

Related

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.

Using the ADO.NET Entity Framework with the Advantage Database Server

I'm creating and WPF application using the MVVM in VS 2013; first implementation was with SQL server and it worked like a charm.
Second phase is to have support for Advantage Sybase. For this I have downloaded Advantage Data Provier to have the connection in connection drop down list ( http://www.codeguru.com/csharp/.net/article.php/c17027/Using-the-ADONET-Entity-Framework-with-the-Advantage-Database-Server.htm ).
For VS 2013 there is a problem with this and the workaround is to manually edit the registry to have this provider (http://blog.nwoolls.com/2012/07/25/registering-missing-data-providers-with-visual-studio-2012/).
Now I have the provider in the drop down, I can select the provider, but when I try to generate the script for data base generation I have a weird error:
ERROR:
"
Could not find the appropriate DbProviderManifest to generate the SSDL. The supplied provider Manifest token '2008' is not valid.
"
Any ideas on how to use the DB Provider correctly?
First, VS 2013 is not yet officially supported by Advantage Database Server. I believe official support may be available once ADS 12.0 is released.
But.... I did get a chance to try it out and it is working.
Be sure to that you are using the 11.1 ADS .Net dataprovider. It includes support for Entity Framework 5 (As far as In know, nothing from ADS includes support for EF6 at this time)
Export the 4 keys mentioned in your second article from Nate Wools. In my case I exported from VS 2012 (11.0 in the registry path). Full find/replace on 11.0 -> 12.0 including the assembly version for Microsoft.VisualStudio.Data.Framework
(Disclaimer, I've not had a chance to try MVVM, just a plain Windows Form app, but it worked well)
App.Config that was automatically created and updated. Maybe check against yours?
<?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=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
</startup>
<connectionStrings>
<add name="Entities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=Advantage.Data.Provider;provider connection string="Data Source=E:\ADS\School\School.add;User ID=adssys"" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
</configuration>

Connection String in separate config file with EF model designer

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.

connectionStrings configSource in App.config not working

I'm trying to separate my connection string from my App.config, and as you can't do transformations like with Web.config, I thought may I could use the configSource attribute to point to another config file with the connection string in, but it doesn't seem to be working.
This works, App.config:
<?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=*snip*" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
<connectionStrings>
<add name="DefaultConnection"
providerName="System.Data.SqlClient"
connectionString="Server=*snip*" />
</connectionStrings>
</configuration>
But this doesn't, App.config:
<?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=*snip*" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
<connectionStrings configSource="connections.config" />
</configuration>
connections.config:
<connectionStrings>
<add name="DefaultConnection"
providerName="System.Data.SqlClient"
connectionString="*snip*" />
</connectionStrings>
I'm looking for the simplest of solutions.
Any ideas?
If you added the file yourself, the build action (in the file properties) may not have been set correctly.
The Copy to Output Directory option needs to be Copy if newer or Copy Always to ensure that the .config file ends up in the bin directory, otherwise it will not be there and trying to load the configuration will fail.
Right Click on file and then Click properties
Change to "Copy always" or "Copy if newer"
I had the same issue and Oded solution works for me. But I will just precise that to find out how to change the file "Copy to Output Directory option" to be "copy if newer or copy always", you have to
rigth click on the file
select properties
go to advance
then will see copy to output directory and choise copy if newer or copy always
This helped me, I hope it will help you too

Categories

Resources