I'm looking at using ADO.NET Data Services with silverlight. I've been looking at the
Data Model and Data Service Implementations (ADO.NET Data Services/Silverlight) topic on msdn. In their example, they build a data model generated from a database, and query entities in the datamodel. In my case I want to use stored procedures, so I"ve created the data model and added stored procedures instead of tables & views, but now I'm not sure how to execute them. Does anyone have a sample?
Just specify the stored procedures when you build your Entity Framework model. There's lots of documentation about this. You can also see a screencast.
As a point of clarification, I don't think there is anything Data-Services specific about your question. It's more of an Entity Framework question.
Related
I want to start using Dapper because I think it's easier to use with Stored procedures than to map every stored procedure with Entity Framework to replace CRUD operations in general.
I would like to know how can I create the POCO classes with dapper, and if it is not possible, could I create the model from the Database using Entity framework and then use the classes created with dapper?
Thanks!
There are a number of projects where I have used dapper alongside Entity Framework / nHibernate.
There is a distinction between the POCO classes created for Dapper and the domain entities used by code first Entity Framework and you should create both.
Stored procedures will work with a shaped view of the data and do not have the responsibility of representing the entire domain.
Following single responsiblity practises the POCO classes you use should only contain those properties used by the stored procedures to are interfacing with.
If you are not using Entity Framework for data access and want to replace it completely with Dapper and stored procedures then you are failing to realise any of the benefits of an ORM and should simply hand craft the schema and the POCO classes it uses.
You can generate POCOS from a schema and tools exist to do this, given the overhead of creating your own classes which can be better tailored to the application layer I woudl suggest there would be no benefit.
I'm developing a MVC5 web project in VS 2013 and I have to use an already existing database and its Stored Procedures so I'm looking forward to using Entity Framework database first approach to help me model the classes.
My question is, should I create the classes (the model) directly from the tables using EF? i mean should my classes represent a table in the database exactly the way they are? - given that some stored procedures return a combination of different attributes from different tables, I'm confused as what the classes on the code should represent exactly.
Also i want to have my own form to let users upload and read their info, so scaffolding the views to create the read/update/delete won't come handy for this task, will it?
Thanks!
If it is code first then you can use the EF tools to scaffold your database for you from your existing database. If it's database first, all of the database models are generate for you anyway and whenever you update your database the models can be updated to reflect the changes for you.
If you are using stored procedures for code first, you'll need to create objects for each stored procedure so that the return values can be mapped back to an object. These should really match precisely the data that is being returned back in both type and naming:
this.Database.SqlQuery<YourEntityType>("storedProcedureName",params);
As for having your views scaffolded for you, I think you should take one step at a time and see what works for your use case or not.
We have an Asp.net Web application with a normal Ado.NET Oracle connectivity.
The back end works completely on stored procedures.
Now, our management has asked us to upgrade the entire application to MVC.
The management has come up with some standards where they say we must use EntityFramework model and go with the same set of stored procedures without any change.
Here is my question. Each action in my application is running from stored procedures written in the Oracle DB. Is it possible for me to call the exact same stored procedures from Entity framework in Oracle. How can I achieve this?
The stored procedures does plenty of things in the back end like insert,update, select or all in most of the cases.
I know entity framework needs an Entity model. If stored-procedures
will work, what will be the entity model. Is there any workaround for
this?
Try out this example for using stored procedure with Entity Framework, maybe you need to understand the things in your project, whether to use entities or complex types as part of your stored procedures.
I have been looking at the Microsoft examples and most use Entity Framework. However it seems overkill for when I need to make a quick connection. Gets some data from a report and the return it to my web page. It also seems like it is difficult to compose such things as complex SQL that might require a multi-table join and some input parameters.
So what other options are available?
If you want a quick way to connect to your datasrouce you can look into some micro ORM-frameworks:
https://code.google.com/p/dapper-dot-net/
https://github.com/robconery/massive
http://www.toptensoftware.com/petapoco/
These are all tiny libraries that allow you to write your own SQL but still help you (a bit) with mapping the results to classes.
Here is what we do for our reports app that fetches data using Web Api and SQL.
1. Stored Procedures with EF
Since you mention you need to fetch data that might require a multi-table join, best approach here would be to use Stored Procedures to get that data. Import that Stored Procedure to the EntityFramework.
2. Service Layer Class & AutoMapper
Now create a similar object that will map to the Data Layer's object that will be returned from the Stored Procedure you just added. To map these two classes we use a mapper called as AutoMapper. We create two classes because we dont want our web app to directly access the Data Layer classes.
3. Expose your Web API
Now this data can be sent from the api to whoever wants to use it.
Hope this helps.
I have an application with an Entity Data Model. I do not completely understand the value of an Entity Data Model. Either way, I know that it connects to my database and interacts with it. I have some C# code from which I want to execute a stored procedure. My problem is, I'm not sure how to do this using the "Entity Data Model" approach.
I'm familiar with the System.Data.SqlClient namespace, but that approach does not seem applicable here. Can someone tell me how to interact with an Entity Data Model sproc via code?
Thanks!
Here's a great video tutorial on "Practical Entity Framework for C#"
You can call a stored procedure on SQL Server like this:
myEntities.CalculateCustomerInvoices();
Here's an article on "Using Stored Procedures for Insert, Update & Delete in an Entity Data Model"
This MSDN page explains how to work with stored procedures with the entity framework: http://msdn.microsoft.com/en-us/library/bb896279.aspx.