Using stored procedure with Entity Framework or not? - entity-framework

I want to know, do I use stored procedure with Entity Framework or just user Linq-to-Entities queries?
Consider I want have a customer's orders. So one solution achieved by using stored procedure that uses joins on two table and for example return order IDs.
Another solution achieved by using Linq-to-Entities queries. This solution have a preference over first solution and that is, we can user navigation properties to easily move in customer order information. but in stored procedure since it only return ID's it's a little bit to access order information.
So which is better, considering second solution preference?

Both solutions work - and since you didn't define what "better" means to you, we cannot possibly tell you which solution is "better".
Using stored procedures with Entity Framework is definitely possible - especially with EF4. Stored procedure have their benefits - you don't have to give the user direct table access, you can possibly let a DBA tweak those stored procs for top performance, and you can do things like delete a Customer by just calling a stored proc with the CustomerID to delete (instead of having to first load the whole customer, just to delete it).
So stored procedures definitely have benefits - the downside to many folks is that they have to write those in T-SQL, and now suddenly part of your application is in your C# code, while another part is in stored procedure's T-SQL code.
So again: as vague as you asked, there's no real good answer to this. Both approaches are valid and both work - it's a bit of a personal preference, which one you want to use.

Related

Save simple information for a database within postgress

I have a multi tennant application which will use the SILO Model to save data (each tennant will get an own database).
Because tennant names could be redundand my database are with GUIDs: MyApp_[GUID].
Now I want to save simple but neccesary information for each database like a tennant name and 3 to 5 more informations.
Is there a simple way to write and get these data?
The only way I can think of is to create a special table for this with only 1 row - but it seems a bot of wasting.
If you're looking for a simpler solution than a table per database (and having to deal with the awkward constraint that it must have exactly one row), you could
use a custom configuration parameter. You can change them with ALTER DATABASE. The downside is that you can only store strings, and that the settings might be overridden per session.
use a COMMENT on the database. The downside is that you can only store a single string per databasebase; the advantage is that it is automatically shown in many lists of databases such as psql's \l+ command
add your own columns to the pg_database system table. You should not mess with that, so it's a spectacularly bad idea even if you knew what you were doing, but in a relational model it's the closest to what you were asking for so I'd mention it for completeness.
I don't really advocate any of these solutions, although they do what you were asking for there's probably a better solution to your actual problem. It might be as simple a table of databases, possibly with a foreign key to pg_database, in an extra database shared by all tenants.

Code First Join options in Entity Framework 5

I'm creating a webservice layer over a legacy SQL Server based system. It's a pretty large and complicated business application which has a large number of stored procedures that perform SELECT statements . Most of these stored procedures join a number of tables and produce a single resultset for easy consumption by the client.
In building my webservice I want to take advantage of EF, and using a Code First approach 80% of my use cases can be achieved by mapping direclty to tables. However I have a number of use cases where I need the cross tbale views of data as provided by the stored procedures. As I see it I have 3 options:
Create new POCOs that represent the stored procedure returns and link these to the existing stored procedures (let SQL do the join and re-use exisitng code)
Create new POCOs that look like the stored procedure return, but populate themselves by association with othe EF entities (let EF do the jons)
Do the joins somehow in LINQ
What do people think is best practice in situations like this, which I guess most of us are coming up against everyday?
Thanks,
Andy
many a good book out there. anything from Julie Lerman... this is more recent http://www.apress.com/9781430257882
With Navigation properties, you can do most things without old fashioned joins.
So 4) do it in Linq,and let EF do the Joins.
Use Navigation properties. you can do many logical joins and even avoid the join keyword.
this blog shows how easy and why LINQ with no join syntax results in Joins at the db level.
http://blog.staticvoid.co.nz/2012/7/17/entity_framework-navigation_property_basics_with_code_first

Is there a database administrator's guide for building stored procedures for Entity Framework?

I'm working on a green-field application that has a corporate mandate that Stored Procedures are used for all database interaction.
I'd like to use Entity Framework and leverage Stored Procedure Mapping to gain the benefits of the ORM.
Since we will be developing the database and .NET application in parallel, I'm looking for information to help the database developer/administrator. Does anyone know of a consolidated guide on how to design tables and stored procedures so they can be best integrated with the Entity Framework?
A couple tips I've collected are:
Update Stored Procedures require exactly 1 parameter per table column
There must be an insert, update, and delete Stored Procedure for every table
I want to know as much about how the database should be designed for easy use with Entity Framework because the database is very difficult to change later in our environment.
I wrote a blog post describing the limitations of using mapping in this way after working on this for several months:
The Pitfalls of Mapping the Entity Framework to Stored Procedures
If you want to use Stored Procedures are used for all database interaction, I just don't see the need to use Entity Framework. One good reason of EF is to save time to write T-SQL, and if you don't take advantage of this, why even use EF?

What advantages my BO deves gets in case of using EF with SQL Server having all relationships maintained?

I am a DBA. I want to know what advantages my Business Objects developers will get when using EF with SQL Server DB which is fully managed using Foreign keys and Primarkey as and when require. As this is our new project and we have to use EF with SQL Server 2008 R2. We have a plan to use Database First Approach. Can anyone tell me what difference my Business Object developer will experience in case If I define all foreign Key relationships in my DB?
Assuming it's setup correctly, when your developers actually create their objects from the database structure, they'll be able to access any related tables rather easily.
It should also make creation of new objects (rows in the tables) easy, as it then shouldn't be possible to create new items that would break the foreign key relationship.
It's also just plain good practice to correctly setup any foreign keys in the database; I'm not sure of any benefit not to.
As a developer that's had to work with data sources that haven't been setup correctly, I can tell you a correctly setup database structure is an amazing experience for a developer.
(As an aside, as a DBA, you may want to take a look at EF. Also take a look at LINQ, one of the items that they'll be using. In particular, Why LINQ beats SQL may help you get a basic understanding, even if you don't agree with the article title :) )

MVC 3 and LINQ to SQL or Entity Framework

I'm trying to display the results of a sproc in my MVC 3 web app.
However, the sproc calls into 4 tables on one database and joins them with 5 views (single table views only, thank goodness) on another database. Each (SQL Server) db is on a separate server but that shouldn't matter.
I've read this: http://blogs.msdn.com/b/swiss_dpe_team/archive/2008/02/04/linq-to-sql-returning-multiple-result-sets.aspx
and this:
http://www.codeproject.com/KB/dotnet/linqToSql5.aspx
and still cannot determine whether I should use the dataContext classes or just embed the straight SQL.
Perhaps there is a better way to return my results than LINQ to SQL (15 columns, 3 different data types)? I need to update the tables as well. The user will have the ability to update each value if they choose. Is this a task best suited for the entity framework classes?
I plan on using the repository pattern so I can change data access technology if I must but would rather make the correct decision the 1st go 'round.
I was hoping for a resource that was more up-to-date than say, NerdDinner and more robust than the movie apps for MVC3 that abound, particularly implementing the sproc results inside a view. Any suggestions would surely be appreciated. Thanks.
Once you plan to "update" data then you are going to handle it all through stored procedures. Both Linq-to-sql or Entity framework will not help you with this because they are not able to persist changes to something created from arbitrary query. You should very carefully check if you are even able to track the data back to the correct record in the correct table. Generally result of a stored procedure is mostly for viewing the data but once you want to modify the data you must work with each table directly or again use some stored procedure which will do the task. Working with tables from multiple databases can be pretty complex in entity framework (EF doesn't support objects from multiple databases in one entity model).
Also what you mean by 15 columns, 3 different data types? Stored procedures support in both Linq-to-sql and Entity framework will return enumeration of one flattened data type containing 15 properties.
I'm not aware of anything that linq-to-sql can do that Entity Framework can't really, so EF seems to be a better solution in this case. You can add a stored procedure to your Entity Framework model as well, so you can just have it call the procedure and deal with whatever comes back.
Since the end goal will involve accessing the same Databases with either technology and they will be using sql to retrive the data either way its really a subjective anwser.
I would use whatever technology you are most comfortable and focus more on the implementation. Both data access platforms are based off of ado.net technologies and are for the most part equally powerful.
Regardless of the technology I would evaluate how the data is accessed and make implementation decisions based on that.