Stored procedure into Entity Framework model - visual-studio-2010

Can you add a stored procedure into an Entity Framework model (Model.edmx)?
I just did it in Visual Studio 2010, like this (except my stored procedure is now off the Add tab and is in the Refresh tab)
My stored procedure is a simple SELECT statement, taking 1 parameter.
VS2010 seems to have added it into the Model, but I wonder where it is and how I can use it?
I can't see it anywhere in the diagram nor in Model.designer.cs

You part the way there. It's a bit of a hidden process and multi-stepped. You have to include the Stored Procedures in your model by selecting them in the menu you have shown above.
Once you have done that you want to view the 'Model Browser'
Then in the Model Browser, find the Model Store (This will have folders like 'Tables / Views'). Right click on your Stored Procedure and select 'Add Function Import'.
Once you have done this, you can map the Entities you will get back from your SP.
You will then be able to access your entities via your Context in code, in a strongly typed way. Hope that helps.

From the model diagram, right click and select Model Browser (or View/Other Windows/Entity Data Model Browser from the menu). In the Model.Store section, you should see Tables/Views, Stored Procedures/Functions and Constraints. Your stored procedures will be in the Stored Procedures/Functions section.
Check this link for a good explanation of how to use stored procedures with EF: http://msdn.microsoft.com/en-us/data/gg699321.aspx

Complement for previous answer:
If need, not an entity but juste create a complex type (you can create it in complex type folder in model explorer) and associate it to your stored procedure if your returning dataobject doesn't match with a table of your DB, example:
http://img46.imageshack.us/img46/9168/j4ax.jpg

Related

Oracle Apex 19.1 (how to add, edit, delete data through a form)

I'm new to oracle apex so this might be simple.
I have an application that i'm currently building. I have an 'employee' table in the SQL workshop. When i attempt to create a form linked to the table there is no option to edit, delete, or add entries into the table ,once the form is completed?
This is essentially what i need help with. I need to be able to manipulate the 'employee' table through the form created rather than through anything within the sql workshop. Just for context i am not the workspace admin, however i am a contributor.
I would appreciate if anyone could provide me with a quick step by step guide into creating this desired form accurately.
I'd suggest you to create (using the Wizard, of course) Report with Form on Table. It will create
interactive report you'd use to view data stored in that table
form which will be used to insert/update/delete data
the same form will be called when you push
the "Create" button in order to create new rows, or
icon at the beginning of every line in the report in order to update/delete rows
This combination (report + form) works nicely for ages, so ... try it. I hope you'll find it useful.

Core data, bindings, NSArrayController and table views - how to generate a view of a core data context

I have a working system that lets me build a database containing instances of various entities , all linked together nicely.
Before I knew I would care, I came across a tutorial on using Core Data and bindings, and it went through a complete case where you get a table showing all the entities of some type with a column for each property. It showed both the UI side and the Data model side - not that I need the data model part at this point. Now, darned if I can find it. This is one of those things that is supposed to be easy, and requires virtually no code, but getting exactly the right connections in UIBuilder is not going to happen if I can't find instructions.
Also, I thought I came across an example of something like a query editor where the user could select which properties to sort on, which to match on, etc. Did I imagine that?
Anyone out there know where I can find such?
Sure, you can do this without code:
Add an array controller to your nib.
Bind or connect an outlet for its managed object context
Set the array controller to Entity mode, fill in the entity name, and select Prepares Content.
Bind your table view columns to array controller's arranged objects, and fill in the key name for the model key.
Regarding the query editor, open up the model, and on the Editor menu click Add Fetch Request.
I found at least a partial answer to the query editor question, in this apple tutorial. Not sure how far it will get me, as I prefer to write code where possible, since then I can leave a trail of comments.

Adding a Stored Proc to complex type not showing up in .edmx (thus not mapped)?

1 (mvc3) I have added a stored proc to my model as mapped to a ComplextType.
Two Issues:
1) ComplextType.cs exists in class under Model1.tt BUT doesn't show up in .edmx?
2) When I try to create a controller with strongly typed views with that ComplexType.cs it errors stating that it can't be created because ComplextType.cs is not part of the DbContextEntities class?
How can I get this complex type added to the .edmx and mapped to my dbcontext (I have done this with tables but not sure what I'm missing for the stored procedures?
Thanks!
For those who just came here from googling,, :)I will give the what is basically has to be done to map a stored procedure into a ADO.Net entity.
When mapping the Database to the EDMX file(Entity Model).. the Entity Model automatically map the tables and complex types and ect.. But the stored procedures that are created in the database is not mapped with return complex types. We have to map it in the Function imports by creating our own complex type. This complex type can be accessed in the code.
This is done as below:
Right click on the function Import and add new Function import.
Give your name to the function and specify the stored procedure and
then select complex type(If stored procedure returns complex type)
or you can select scalar. IF you are selecting the complex type and
you can view the columns that are returning and you can create a complex there by create new complex type.
So the return data from the stored procedure will be a set of those complex type.
One thing that should be one should access the design or the model view to update the Entity Model. You can not update the Entity Model by just right clicking on the Entity Model. The option of updating the model is provided only on the Model Browser and the Database design diagram. This model browser can be taken from the Other windows of Views in VS2010/VS2012. These information will seems boring. But trust me if you are new to this these seems big at the beginning.
Most probably you are going to create this Entity Model from mapping an existing database.
Keep in mind that even you map the Entity model form the database you can customize the Entity model by deleting the unnecessary entities(tables) and creating complex types

How can one easily set up sproc parameters in a strongly-named DataSet in Visual Studio 2005?

I don't usually work with this type of data layer so I'm not familiar with all the terminology and it's difficult to search for answers.
I have an XSD file in Visual Studio 2005, and I'm looking at the GUI editor. I created a table adapter by dragging a stored procedure onto the form. That stored procedure is basically a SELECT statement with a few parameters. It seems that this has automatically populated the SelectCommand properties for the table adapter.
Now I want to bring in a stored procedure to populate the UpdateCommand properties. The only way I know of to do this is to open the properties pane for the table adapter and expand the UpdateCommand properties. From there I select StoredProcedure as the CommandType and I select the stored procedure from the CommandText dropdown.
At this point one would imagine it might import all of the parameters by analyzing the stored procedure, but when I go to the parameters collection, it's empty, and I seem to have to set up all 30 or so parameters by hand. Surely there must be a faster and less error-prone way to do this.
I imagine I'm missing something very fundamental. What is it?
You could try to right-click the design surface and choose Add TableAdapter. Once you specify the connection to use, it will prompt you for how to fill in the commands. Specify to use existing stored procedures, and it will help you configure the parameters.

Linq-to-SQL How to prevent the use of Delete methods?

By convention our DB only alows the use of stored procedures for INSERT, UPDATE and DELETE. For some tables / types there is no DELETE stored procedure, because it is not allowed to delete rows. (You can only update the status of such a type to "deleted"). e.g. a customer may be marked as deleted but is never really removed from the DB.
How do I prevent the use of Delete() for certain types in the Data Access Layer = in the DMBL?
The "Default Methods" for Insert and Update are mapped to the corresponding stored procedure. But for Delete it says "use runtime". I would like to set it to "not allowed".
Is there a way to achieve this on the DB model layer?
Many thanks
Implement a partial class for each such entity and implement the OnValidate partial method. It takes a ChangeAction as a parameter. When the ChangeAction is ChangeAction.Delete, throw an exception that indicates that the operation is disallowed (IllegalOperationException, maybe).
I had the same constraint while developing one of my application. You can always condifure the delete action to use a certain stored procedure instead of the framework generating a sql command for the same. In my case when I say delete we just wanted to mark a particular row as deleted but not physically deleting it. So our update stored procedure was reused in the delete command to simply mark the isDeleted col value as true. Besides that you may want to build some kind of wrapper around the classes built by DBML and suppress the delete methods. Right now I do not see any speciall setting to have the framework only generate create and update methods. Partial classes may be one more alternative.
How about to configure rights for the user you're using to connect to DB? You can set Deny for Delete operation for this particular user, so it wouldn't be possible to use DELETE statement on DB level.
The deletion API is not generated, by which I mean an option on a generated table cannot remove the ability to mark an item as deleted. DeleteOnSubmit is part of the Table<TEntity> class.
If it is always an error to delete an entity, OnValidate should throw an InvalidOperationException as tvanfosson suggests.
I tried mapping Linq to Sql deletes to stored procedures which simply set the field to true. It gets weird because the DataContext removes instances after "deleting", but they are legal domain entities and should still be in the DataContext after submitting.

Resources