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.
Related
I have to create a new project and (as usual) is with an existing SQL Server database.
I used to use EF Code First connecting with my database, opening my EDMX model designer and then right click --> Add Code Generation Item. (http://weblogs.asp.net/jgalloway/archive/2011/02/24/generating-ef-code-first-model-classes-from-an-existing-database.aspx) Easy.
But now I've discovered there's something called EF Power Tools that allows me to do Reverse Engineer Code First (cool name!) and get the same (http://msdn.microsoft.com/en-us/data/jj200620)
Do you know the difference between the two options? Which one is better?
Thanks in advance.
(Sorry if this question was previously asked but I didn't find it.)
The difference is that the edmx approach is not code first, but database first with DbContext API. You will always use the database as the source of model changes.
EF Power Tools produce a truly code first model with DbContext: from then on you will change the class model first and modify the database accordingly (e.g. by EF migrations).
Neither is "better". DbContext API is easier to work with than ObjectContext, but both approaches use the former. It's up to you to choose whether you want to work database first or code first. It's a matter of personal preference and it may depend on who maintains the database structure. With database first it is easier to respond to changes someone else imposes on the database structure.
As far as workflow goes for database first, adding to what #Gert-Arnold said:
With database first it is easier to respond to changes someone else imposes on the database structure.
If someone else is managing the database changes, I'm finding it far easier to use the EF Designer. You get an updated database, then just right-click in the EF Designer and update the model from the database. You can use source control to easily view what has changed.
Also, if you only need a subset of tables from the database, reverse engineering causes alot of work having to go back and remove classes and properties from the context.
I found re-reverse engineering via code-first to an existing database to be just too much of a pain trying to figure out what changed and how I needed to update code that used the context.
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.
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").
As the title says, how do I view the SQL generated by Entity Framework from within my code? I'm running into an error where the EF is crashing because a field is generated by the database (a DateTime field), and I thought I set it to know that the store is generating it via StoreGeneratedPattern, but it's still crashing, so I would like to see what exactly it's trying to push up to the database.
P.S. I've only been using EF for about an hour now... Switching from L2S.
Since you don't have Sql Profiler, your best choice would be LINQPad. You can use your existing assembly.
Click Add connection -> Use a typed data context from your own assembly -> Entity framework and select your dll.
You can write queries directly against your model (or copy-paste from your code). Select the SQL 'tab' under the query window to view the generated SQL code.
You can use the Entity Framework Profiler (EFProf). It's not free, but there's a 30-day trial available. It does a lot more neat stuff besides showing you the SQL statements.
Generally, you should always use SQL Profiler to see the SQL statements that being submitted by EF into your database.
Also, I think you misunderstood about what StoreGeneratedPattern is. If you look at its possible values inside the model, you'll see that it has identity meaning that the value will be generated (by the database) when the row is inserted and will not otherwise change. The other options are Computed, which specifies that the value will be generated on inserts and updates, and None, which is the default.
So EF will not generate that DateTime field on the fly for you, you need to manually create it and then update your model from database so that EF will generate appropriate metadata to work with it at runtime.
The free AnjLab Sql Profiler will work if real SQL Profiler is not available because you're using SQL Server Express: http://anjlab.com/en/projects/opensource/sqlprofiler. It's not quite as nice as the real thing but it gets the job done well enough.
One solution would be to capture the network traffic and have a look at the data on that level. Microsoft Network Monitor does a good job of this.
Of course, that only works if you're using a separate DB server, and the connection is not encrypted.
I am creating a project using Entity Frameworks 4 and self tracking entities. I want to be able to either get the data from a sql server 2008 database or from sql server compact database (with the switch being in the config file). I am using the repository pattern and I will have the self tracking entities sitting in a separate assembly.
Do I need two edmx files? If so, how do I generate only one set of STE's in the separate assembly? Also do I need to generate two context classes as well? I am unsure of the plumbing for all this. Can anyone help?
Darren
I forgot to add that the two databases will be identical and that the compact version is for offline usage.
Just to follow up on this. In the end I had to maintain two separate edmx files, one for sql server and one for compact. The main reason being that compact 3.5 does not support auto identities (as mentioned above by Zeeshan). This in turn led to two context classes. In the context class for sql server compact I had to put code to check for insertions, query the database for the latest id and increment it manually before saving.
Thankfully with the release of compact 4.0 this no longer applies as it supports auto id and you can indeed use just one edmx file.
Darren
You do need the edmx file as long as the schema is exactly the same. just change the connectionstring and everything would work seamlessly. Though i am not sure how u are saying that schema is same when compact edition does not support identity concept and full blown sql server does. So if you are using features specific to sql server that's not available in compact, then you would get runtime errors.