Entity Framework opening connections to database and not using them - entity-framework-6

I'm using Entity Framework 6 DB first for a project. The database is owned by a DBA. He is (and so am I) wondering why EF opens up so many connections the the database that it never uses.
See the below picture, EF has opened up a lot of connections to the database, all from the same host (the hosting IIS server) and for many of them it never even makes a query.
Is this some sort of "there might be a query so I'm gonna make a connection" just in case type of thing?

Related

Will MVC timeout if EF Code First takes a while updating the DB due to indexing

Environment:
I have an MVC application that is using the Entity Framework 6.1. The database I am working with is extremely large and will contain many indexed tables and views.
Concern
When the application starts, due to it being code first, it'll update the database as we've asked it to (Not the concern). My concern is, with it being a web application, the application will time out, not finish updating the database, and create a corrupted DB.
Question
So when I go to add or update views that contain indexes, will I run into an internet timeout issue? So say if, because of the indexes, it takes 15 to 25 minutes to run, is that a cause for concern, or does MVC and EF know how to handle that?
Should we, when we update a client, manually go on their server and run a separate sql file that updates and adds the indexing?
If this is really 15-25 minutes then I would not risk letting users access the application while the database is indexing. I would switch off the web app, reindex the database offline and turn on the app.
And no, neither MVC not EF know how to handle that automatically.
Technically, you could possibly increase the command timeout for the database migrator so that the migration would not timeout but the database would not operate normally during reindexing anyway.
http://msdn.microsoft.com/en-us/library/system.data.entity.migrations.dbmigrationsconfiguration.commandtimeout(v=vs.113).aspx

Will using Entity Framework in ASP.NET MVC allow my web project to work on different database vendors

I have been using asp.net MVC web application and Entity Framework with SQL Server 2008 R2. But now one of our clients requires us to use Oracle 10g database. So my question is what will happen if I do the following:-
I create the same tables that are already inside sql server , inside a new Oracle DB.
I create a new ADO.net Entity data model , and define the Oracle database instead of SQL Server 2008 R2. And map the oracle tables.
So I am assuming that Entity Framework 6 will generate the same model.edmx class regardless of the database vendor . And in this case can I re-use my application and run it on Oracle database ?
Is this the case, or there are more steps to follow , to be able to have my asp.net mvc 5 web application working on both SQL Server & Oracle databases without worrying about the database vendor ?
bearing in mind that i am not using stored procedures at all, and all my business logic is implemented inside my Controller and Model classes.
Thanks
In theory, yes - Entity Framework "abstracts" away the underlying database - to a certain degree.
But in reality, different databases from different vendors are just different - often to the point where trying to support both (or multiple) simultaneously isn't practical.
My approach here would be:
create the same table structure in Oracle as you have in SQL Server
encapsulate the EDMX model and the database-specific features into a separate class library - one for SQL Server, one for Oracle
put a database-independent layer on top of this (e.g. the "Repository Pattern" or other approaches)
make sure no database-specific features "leak out" of this encapsulation layer
configure your app so that either the SQL Server or the Oracle database core assembly will be loaded at startup
With such an approach, you should be able to handle both databases - not exactly switching at runtime, but you can configure one or the other to be used.
I once used Oracle 11 in place of SQL Server and found that Oracle 11 does not have a provision for Identity column and I had to implement the same using a trigger and I had to auto generate Identity while inserting as EF was not picking up Id value generated by the trigger. So ideally there shouldn't be any change but practically there are a lot of subtle changes required.
Also take the example of boolean field which is present in SQL Server but not in Oracle.
--Revised--
Further Reading-
Map multiple databases to EF
Changes in EF required for working with Oracle
ODP.NET provider

ASP.net MVC, I want one database, not multiple

I am following the guide on the asp.net site for learning asp.net mvc4.
Link : www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/
Thing is, I want to put my movies table in the Default connection. Because I want all of the data to be in ONE database and not two.
I mean. I am confused as to why I can't just have one database, with separate tables. Surely multiple databases will introduce latency, and also scalability issues to my project.
How can I get around this?
That Default connection points to your local db and is what is used for the forms authentication stuff. That db has all of the asp.net role provider schema stuff in it and since it's an "internet" project this is where all the login stuff goes. if you want to have your entities and models hook into that same database when you add your entity framework model point it at that db and your good to go.
After a little further investigation:
It looks like http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-model is where you define your connection string for your entities. I noticed AttachDbFilename=|DataDirectory|\Movies.mdf in there, and after verifying my thoughts http://social.msdn.microsoft.com/Forums/en-US/sqldataaccess/thread/f21c0728-935d-492a-baaf-ff2704e3683b/ it looks like that is what is spinning up a second database in the app's data directory called movies.mdf. If you want to keep your 1 default database, change the connection string information in your MovieDBContext string to that of the default connection and and it should create your new movie structure within that same database.

Using two different databases simultaneously using Entity Framework

I'm using Entity Framework 4 in an MVC3 project. I'm trying to access two different databases (A and B):
Databases A and B are completely different and unrelated.
Database A is a 'code first' SQL compact database.
Database B is a 'database first' SQL Server database.
I have straightforward queries on either database, no linking or cross-database joins.
Either database connection works if I remove the other DbContext from the project.
Now, when I try to query from database A, I'm getting an Exception "Could not find the conceptual model type for ".
I can't imagine that EF doesn't let you use different databases, so my question is: do I need to do something extra when creating a new instance of the appropriate DbContext, in order for this to work?
I stumbled across this thread while trying to resolve the "Could not find the conceptual model type for" exception.
I am using EF 4.2 with multiple models. I implemented DbContext with one of my models to "try it out". When I compiled and ran, I received the exception above for an entity in a completely different model on a completely different database!
After much hassle I finally implemented DbContext to the other two models and everything ran fine.
I know this doesn't necessarily answer the question, but I wanted to leave this here for anyone else that has the same issue and stumbles across this post.
Make sure you don't use the same entities name for both edmx files. You can change it manully opening a edmx with xml editor right clicking the file and open with.
Note that for some security reasons you can't nest the contexts. They can only be used separated.

EF4 cross database relationships

I was wondering if EF4 support cross-databse relationships? For instance:
db1
Author
Id
Name
db2
Posts
Id
Content
db1.Author.Id
What ideally I need to do to get this relation in my ef4 model?
Do you guys have any idea?
Thanks
I've found this entry in Microsoft Connect that answers the question about the support given at this moment by EF (actually it is not supported yet).
Also found a thread in Social MSDN about this concern.
Other links on Stack Overflow:
ADO.Net Entity Framework across multiple databases
Entity framework 4 and multiple database
In summary, the only given alternatives are:
Using views in EF
Use NHibernate instead
If your database supports Synonyms, you can trick EF to span multiple databases. I wrote up how to do it here.
Basically you end up with an edmx file per database, and a script which merges them into a single edmx file. Synonyms are used to reference one database from another by using the actual table name, so EF doesn't throw a fit when you try to access database2.table from database1. You still have to setup links between the two databases manually in the EF model, but once setup they'll stay even if you re-run the merge script.
Scripts to setup Synonyms and to merge the edmx files are posted in the link
I recently began a project that uses entity framework with two databases, one Oracle and one SQL Server. I could not find any information regarding cross-database or multiple database support in the entity framework.
Most posts from the MS Entity framework team are a couple of years old and indicate that including two databases in a single model is not a feature that will be included soon. I would be interested in having a concrete answer on whether it was included in 2010 myself although I suspect the answer is no.
Currently out project gets around this limitation by having a separate entity model for each database. This has solved the problem for the majority of the scenarios we've encountered thus far in the project.
In cases where we've needed to query the data from the two databases at the same time, we simply created a view in one or the other databases. Since we're using Oracle and SQL Server, this view would utilize either a Linked Server (SQL) or a DBLink (Oracle).
The disadvantage of views in the entity framework is we've had to spent more time than I expected getting the primary keys working.
Hope this helps.

Resources