Is there a way to insert data using LinqPad and the entity framework?
You need a "Context" of some kind to do an Add or AddObject. I can't find how to get that reference.
I tried making one but then I go this error:
ArgumentException: The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
Any one know a cool way to insert/update in LinqPad with Entity Framework?
In order to use Entity Framework from LINQPad, you would need an existing data context since LINQPad can only generate LINQ-to-SQL data contexts (if you don't already have a project with such a data context, create one and build it)
Click "Add Connection" on the left side of LINQPad.
Select "Use a typed data context from your own assembly".
Select "Entity Framework" from the list.
Click "Next >".
In "Path to Custom Assembly" enter the path to the DLL/EXE file containing the EF data context.
In "Full Name of Typed ObjectContext", click "Choose" to find the EF data context, and the same for "Path to Entity Data Model".
Configure the database connection settings.
Click "Test" to verify everything works.
Click OK - you're ready to go.
What I was missing was the connection string.
I had to copy the connection string from my App.config file (replacing the " with ') and put it in the constructor of my ObjectContext.
After I did that it all worked fine.
if you are using a C# program type, this.Connection.ConnectionString will give you the connectionstring which you can then pass into the ctor of context.
Related
I have defined 2 diffrent Connections (named like "Connection1" and "Connection2") using EntityFramework (ObjectContext) in LinqPad.
When i now define a Query (Statement or Programm) in LinqPad, i have to choose a Connection from the ComboBox that i would like to use for the Query.
Lets say I select the Connection "Connection1" i can now write queries against the Context and execute them, but i also need to use an ObjectContext that used the "Connection2".
Is it possible to use the Connections that i specify in LinqPad to use as ConnectionString to instantiate a new ObjectContext or is there a factory in LinqPad to create a EntityConnection.
Example:
Connection2Container context = new Connection2Container("Connection2");
// or
EntityConnection connection = LinqPadFactory.CreateEntityConnection("Connection2");
Connection2Container context = new Connection2Container(connection);
Yes, this is totally possible.
There are 2 "standard" approaches to this.
Choose the connection you want to use as a primary connection. Then hold down the ctrl key while dragging the second/third/n-th connection from the schema overview on the left into the query window.
After this you'll have to use a slight different syntax to access your entities
Connection1.Persons.Where(x=> ....)
Connction2.OrderDetails.Join(...)
Click add new Linq-To-Sql Connection. Click "Include additional databases" Go.
As far as I am aware, this is only available on Linqpad Premium (maybe Pro, too). I am using the Premium version, so I can't give you a hands-on check on the pro version.
Kind regards.
You can add the assemblies that contain your ObjectContexts as references. Open the Query Properties (press F4), and click Browse... button when viewing the Additional References tab to pull in your assemblies. Now you can use your ObjectContexts like you would from any .NET project.
I have a problem when I got the lasted .edmx.
I get a message that says
Error 11007: Entity type 'pl_Micro' s not mapped.
Looking at the designer view, I do see that this table does indeed exist.
How can I overcome this meesage?
From MSDN:
Error 11007: Entity Type Is Not Mapped
This error occurs when an entity type in the conceptual model is not mapped to the data source. To resolve this error, map the entity type to the data source. Look at the MSDN link
How to: Map Entities to Database Tables
I get this error when I move a database table to a different schema or delete a database table. In this case Entity Framework doesn't seem to update the edmx file correctly when you Update Model from Database....
The way I resolved this was to do a text search for the name of the offending entity. I then removed all references to this in the xml view of the edmx file, deleted the corresponding cs file that defines the entity, and any other references in the project.
This error was passed to me by the SQL Server Authentication server did not have the necessary permissions and could not see the entities.
Confirm that your sql user has the necessary permissions in the database.
This happens also when you import tables via diagram in edmx and then you point in app.config to different instance of database (switching from dev db to test db for example)
I got this error because I had copy pasted an entity and modified it. Restarting visual studio resolved this.
Correct, close visual study, enter the * .edmx file with a notepad ++, sublime text, notepad, etc., search the table, search the column and modify the column size (MaxLength), in all matches, in My case was 2, but they can be more. Start visual studio and the change is already accepted.
I got this error because I had to update the table column type.
Refresh table in SQL server and re-add table in the entity framework for resolving this.
I've got an existing EF4.1 project which is working just fine. I've added a new SP to the DB which returns a new kind of an entity (an existing entity with some additional fields).
The problem is when I try to import the function to the EF - it won't create my complex type. the wizard writes that "no database connection has been configured for this model". Which is strange - because it does see the new SP and everything.
I've tried creating my own, new complex type, but it won't map the fields as needed (some type conversion issues)
Any ideas what could be done?
Thanks!
To build on Malako's answer, I have a simlar situation. My problem was that my connection strings are in an external file
<connectionStrings configSource="some_other_file.config">
The "Update Models" wizard has a bug where it will not look in the external file for connection strings, and I cannot leave a connection string in the connectionStrings tag since it must be empty or it will give an error when building.
The workaround for me is to use the Update Wizard, check the box that says "Save entity connection string in Web.config", and then add all my function imports. When I'm done, I comment out the connectionString so my project will build. Next time, uncomment that entity connection string before updating models.
Annoying, but at least there's a way to get it to work.
For me, how I got it to work in my model project. There was other connection strings I left in there that had "server=" in there (non-EDMX related). I just deleted those other connection strings and the EDMX update wizard works as advertised.
Error message seen: Unable to update the App.Config file because of the following exception: 'The 'server' keyword is not supported.'
I assume the ConnectionString property of the entity model is empty. Check in model browser.
The easiest way to fix this is to remove all the connection strings in the .config files. Do a search for
connectionstring="
Delete the line(s) entirely or comment it/them with
Delete your edmx and recreate it. Make sure 'Save entity connection settings in Web.config as:' is checked.
Now the ConnectionString should be set and you will be able to generate complex models via function import.
if it still not works for you then remove other connection strings. and re add new.
the same problem is solved by this. removing all the connections string and added the Entity framework's connection string first then added the others but changed the name.
My solution to this same problem (VS2013) was this:
Go to Model Browser
Right click YourModel.edmx
Update Model from Database
Then it prompted me to select the connection. I selected the connection I wanted and proceeded to the next screen.
Add/Refresh/Delete what you need.
Select Finish and voila, the connection is here.
You have to add the connection string again every-time you add a stored procedure. Otherwise it will be no map when you try to get the columns you get a message on the "Edit Function import" you will See a message saying "No database connection has been configured for this model".
So my workaround is to re install the stored procedure again but what happens when I have 20 or more then every time I will have to re do all stored procedures. Or create a new model, this is the cons of this.
Here is the link of my solution there is a video and everything but I'm trying to figure How to do it with out deleting the model or adding the connection string VIDEO LINK OR stack overflow solution since there is not way yet to map store procedure I use a class my company has to retrieve has table from the store procedure and then send the data as JSON String if you want that solution let me know and then I will tray to do the code with the explanation (its done with a .dsn conection string and ODBC "Open Database Connectivity").
I did (pretty much) everything correct in a new EF project, but I forgot to use the named connection string in the EF context class, so, it used the default.
It created a new database inside the SQL Express default data directory, and it worked perfectly.
When I realised my mistake (After wondering for ages why no files were showing up in the app_data folder), I renamed the class to use the named connection string and then I kept getting the following error:
Unable to complete operation. The
supplied SqlConnection does not
specify an initial catalog.
I know how to fix this, but, EF is like magic to me! I can't believe it works as well as it does and I am just curious as to what it uses by default / is there a list anywhere of "assumptions" that EF uses on your behalf if you specify nothing?
By default it uses database with the same name as your context but once you specify custom named connection string you must provide the name of used database either by Initial Catalog or Database parameter.
I had the same problem but I figured it out by changing the web.conf. You need to specify an initial catalog and Integrated security and make a user instance.
Here is working web.conf:
add name="MovieDBContext"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|Movies.mdf;Initial Catalog=Movies;Integrated Security=SSPI;User Instance=true"
providerName="System.Data.SqlClient"
I'm using the entity framework that comes with 3.5sp1, and I'm trying to add a custom entity that I will propulate with searchresults from a stored procedure. But I have problems with the designer....... when I pick "Add + Entity" and give it a name, and hit save, the whole diagram stops working...... blah entities namespace could not be found..... all the other entities have lost its reference or summet. Works when I add a "table" to it that exists in database. But when custom it screws everything up.
How should I fix this? I've imported the function, I just need to add an entity for it...
I've read http://blogs.microsoft.co.il/blogs/gilf/archive/2009/03/13/mapping-stored-procedure-results-to-a-custom-entity-in-entity-framework.aspx but I cant get to step 3 since of all the errors when saving on step 2.
/M
Apparently I must have the primary key option checked when adding entity.