Querying Ef context using LinqPad: Argument Exception - entity-framework

I have added Ef connection in linqpad like following
As you can see connection is successfully added but when i want to query against this connection i get the following exception
The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
it seems to be connected with connection string but how it would be solved, i have no idea. The context is defined in asp.net mvc project (no separate project for data access) and connection string is in web.Config file as usual

I had this same problem, I was able to solve it by following the instructions here
1) Find the path that LINQPad uses for its configuration file by excuting this: AppDomain.CurrentDomain.SetupInformation.ConfigurationFile.Dump()
This returned for me the following: C:\Program Files\LINQPad4\LINQPad.config
I was suprised this returned LINQPad.config instead of LINQPad.exe.config
which is what you would typically expect since most .NET applications name the > file the same as the executable.
Take your App.config and copy it to the location above naming the config
file whatever yours returned. In my case it was LINQPad.config
Close LINQPad or the TAB that you have opened to execute your assembly and reopen to get LINQPad to read the configuration file.
http://coding.infoconex.com/post/2012/06/01/Getting-LINQPad-to-read-your-applications-AppConfig-settings.aspx

Recent versions of LINQPad support direct connections to projects with a DbContext or ObjectContext. With that connection type, you can specify your app.config / web.config when setting up the connection.
http://www.kevinlabranche.com/blog/ConnectingLinqPadToEntityFrameworkCodeFirst.aspx

Related

Migration.exe -connectionString arguments does not work doing EF migration on CI server

I am trying to do database update of code first migration on a build server.
I read about using migration.exe from EF 6 tools, and passing misc. context and connection settings in as arguments to the migrate.exe call.
I want to be able to specify the connection string, default catalog and security myself directly as arguments.
The problem is that when I have specified my connection string, etc. like:
migrate.exe Ef.Data.DLL /ConnectionString:"Data Source=myserver;Initial Catalog=MyCatalog;Integrated Security=true" /connectionProviderName:System.Data.SqlClient /verbose
Then migrate.exe will throw an error:
System.InvalidOperationException: No connection string named 'MyContext' could be found in the application config file
My context is defined in code like:
public MyContext(): base("name=MyContext")
So it expects a MyContext connection string like it was still trying to use an App.Config or web.config for this, but it should not, since I'm passing this information in as arguments.
If I try to specify a ConnectionStringName as argument (-connectionStringName:MyContext) along the other args. the I get:
ERROR: Only one of /connectionStringName or /connectionString can be specified.
So I'm pretty stuck here. Cant seem to solve this one. Any ideas are highly appreciated.
I had the same issue today. For me, it was sorted by changing my context constructor, from this:
public RootContext() : base("name=MyContext")
to this:
public RootContext() : this("MyContext")
Having the name= in front of the context name forces the value to be found in the config otherwise it throws an error. This is great when you're only deploying from Visual Studio because it'll helpfully throw errors if your strings don't match, but not so great when trying to automate migrations in different environments from the command line.
So, if you make this change, be careful when running your migrations in VS - if your strings don't match it'll happily continue and create a new database in LocalDB named "MyContext" without telling you that's what it did!

Web app with EF + database first not working on Azure

I have a ASP.NET MVC application which uses EF (v6) as data access layer. My application works fine on IIS Express and also when deployed to the server running IIS 7.5.
The problem is that I'm getting the following exception when I deploy it to Azure (Web Sites).
Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception.
I've searched the web and I made sure my connection string starts with "metadata=" + checked my db context class' constructor to be sure it contains the correct name (in my case it's "name=PsDataEntities"))
My connection string looks like this: <add name="PsDataEntities" connectionString="metadata=res://*/PsDataModel.csdl|res://*/PsDataModel.ssdl|res://*/PsDataModel.msl;
provider=System.Data.SqlClient;
provider connection string="data source=SERVER_NAME;initial catalog=DB_NAME;user id=UID;password=PWD;
MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Anybody had the same problems?
Any help is appreciated
I did a few more tests (like changing metadata in the connection string to use fully qualified names instead of '*') without success, then I decided to set my metadata (regarding .csdl/.ssdl/.msl) programatically with the help from this post. Application now works correctly when deployed to Azure.

Database Connection Error with ef-code-first

I'm new to ef code first and have just used the reverse engineer code first to create a model of an existing database on Microsoft SQL Server 2008.
The problem I'm having is that even though I'm providing User ID and Password in the connection string, it's giving me an authentication error while complaining about my computer name as if I were using Integrated Security (which I'm not.)
The error I get is this:
Cannot open database \"edmTestDBContext\" requested by the login. The login failed.\r\nLogin failed for user 'jwelty-thinkpad\jwelty'.
My connectionString is this:
Data Source=srv-123;Initial Catalog=edmTestDB;Persist Security Info=True;User ID=user;Password=userpass;MultipleActiveResultSets=True
It seams to me like it's ignoring my User ID and using my machine name instead.
It's interesting that the connection string was auto generated by the Entity Framework tool and it worked for building the model but not for actually connecting the model back to the source database.
Any thoughts on what's going on?
I do have full permissions with my username/password as this is what I use with Sql Server Management Studio and that's also how I created the database in the first place.
I tried adding "Integrated Security=False;" and that was no help.
It looks like EF isn't finding your connection string. Make sure that it is in the config file being used (you might need to copy it from the class library config to the application config) and that it either has the same name as the context class or that you provide DbContext with the name by calling the appropriate base constructor. For example:
public EdmTestDBContext()
: base("name=MyConnectionStringName")
{
}
There are some built-in conventions in EF Code-first such as using the name of derived context class from DbContext to find the related connection string in the .config file.
So if your context class is named BlogContext, it will look for the following connectionString first:
<connectionStrings>
<clear />
<add
name="BlogContext"
...

Entity Framework Cannot find connection string

I have a stange problem.
In my solution, I have a "Model" directory. In that directory, I have created an entity model. When I create the model (using the wizard), it creates a new entry for connection string in my app.config (which is at the solution level).
However when I try to access the entity object constructor using the default constructor, I get the error: The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
I have a test application, where I have added a link to this app.config. From here I can access the entity object without any problem!
Thanks
Make sure the connection string is in the config file of the executing assembly. If you for instance use EF from af web site, you can copy the connection string from the app.config in the EF assembly to the web.config of the website assembly. Copy all of the connectionStrings-block:
<connectionStrings>
...
</connectionStrings>

Connection to Entity Framework from SSIS-package script task

From a script task in a SSIS-package I am calling a method in a DLL that uses Entity Framework. And when I´m debugging it stops in the DLL.
Error message: "The specified named connection is either not found in the configuration,
not intended to be used with the EntityClient provider, or not valid"
I had this error before, when I used the DLL from a website, but then I copied the Entity Framework Model Connection to the web.config, and it worked just fine.
What do I have to do in my script task in SSIS? Is this possible?
Your entity model assumes an application configuration file (such as web.config) exists, but because this is an SSIS package, the configuration model is different.
You need to add an SSIS configuration, put your entity connection string in there, map this into a variable in the package and then have the script task supply the connection string as an argument in the constructor of the data model. So something like:
EntityModel MyModel = new EntityModel(this.Dts.Variables["EntityConnectionString"]);
...and you should be good to go.