Use complex type in stored procedure when inserting data to the database, using entity framework - entity-framework

I am using database first in an MVC application, and in certain situations I need to use stored procedures to map data from the database to complex types (and vise versa).
It was easy to setup the mapping to complex types, when reading data from database, but I can't seem to find out, how to map complex types to stored procedures when writing data to the database. It looks like it is only possible to use properties from the underlying entity itself, when choosing the mapping parameters.
I was wondering if it is even possible to use complex types when inserting, updating and deleting data using stored procedures.
Example:
--Database table--
ID int
StartDate varchar(8)
-- Complex type --
ID int
StartDate DateTime
I would like to create an instance of the complex type and insert its data into the database using a stored procedure. The conversion from DateTime -> string would be done in the stored procedure.
Is that possible?

Related

Call Stored Procedure from Entity Framework that returns compound object

I have a database first setup in .net6
I need to call a stored proc (Lets say it is called GetMyData)
The stored proc is going to return 5 columns from various tables and so does not map to a current entity model.
The only things I can find is to use the context model to return a stored proc result but that will not work as the result set does not map to an existing model. Or a convoluted ADO.Net style call that looks horrific and needs lots of code.
Is there a simple way to call a stored proc that returns various columns from many tables?

How to use Entity framework to pass Table Valued parameter to Stored procedure

I have tables with 2 level hierarchy, Parent->Child->GrandChild
I have create stored procedure with three table valued input parameter ParentTable, ChildTable, GrandChild Table.
Now, I want to consume it in .net using entity framework.
Solution all over internet is, create DataTable in .net , store data in it and pass the same as parameter in stored procedure.
But, I want to use entities instead of data table as data is stored in entity objects. Please suggest. Many Thanks.
You need to have look at this question
Create data table from entities and then pass it to the stored procedure. I haven't tried the code, just showing you the path to go. I hope it may help, looking for better solution.

Stored procedure which returns results of multiple entities

Hi I have a stored procedure which returns result of some fields of multiple tables . can i map the result to associated tables (without using complex types)?
thanks.
Out of the box implementation allows you mapping only to flat structures. So either your stored procedures returns single mapped entity type or you need a complex / custom type. It doesn't allow mapping to relations.

Entity framework 4 model first using money value object

I want to use a Money value object in my application. I have found several examples of a Money datatype. But I can't figure out how to use them with EF4. I would like to store each amount as a Decimal/CurrencyCode pair (where currencycode is a string - "USD", "SEK", etc) in the database. I tried creating a complexType but I couldn't get that to work. Is this possible?
It should be definitely possible. Your complex type is just pair of decimal and string property. It is exactly what complex type are used for. Depending on your approach you must do:
Database first:
You will define your database first. Your table will contain money and varchar columns representing your new type. When you update your EDMX model from database it will include it as scalar properties to your entity. You must remove those properties. Then go to model browser and create new complex type. Return back to entity and add complex property of your new complex type. And at the end you must go to entity mapping and map your complex type to those database columns.
Here is basic tutorial from MSDN but from unknown reason they didn't include such elementary details like screenshots. Here is some video from channel9.
Model first:
This is similar to database first but you don't have to deal with database creation and mapping. It will be generated for you.
Code first (EF 4.1):
You must create separate class for your complex type and use it as property in your entity. You should not need to map it by default - mapping should be infered. If it doesn't work you can map complext type either by using ComplextTypeAttribute annotation or by defining mapping in DbModelBuilder.
I can further extend approach you need to use if you provide more details.

Entity Framework 4: Mapped Stored Procedure on Model with Additional Parameters

We've started using Entity Framework 4 for data access and have come across an issue or perhaps lack of understanding.
Our current system is heavily reliant on Stored Procedures, these procedure contain some necessary business logic so we need to continue to use these when doing Select/Insert/Update/Delete.
The issue we are having is the following:
We've mapped a table to an entity, let's say for example this is a User entity and has the following properties - UserId, FirstName, LastName
Now in our sproc to insert a user we accept FirstName, LastName, CreatedById as parameters.
As our User Entity has no CreatedById we get an error indicating that no property of our Entity can be mapped to the "CreatedById" parameter.
Now one thing we've tried is to manually add a CreatedById scalar property to our Entity, but this results in the issue that there is no Field in our User table in the data source that maps to CreatedById. In general the additional property that we'd like to pass in is not something that is stored.
Now there is potential solution to this in that we can just map the procedures to Function Imports and not bother with using the .AddObject, .DeleteObject, .SaveChanges way of manipulating our objects but that doesn't feel like the way to go about it.
that's a good question. There are few options i can tell u.
instead of mapping the entity to the table, map it a view and have the view return CreatedById and then your problem would be solved.
Second option is to create overloaded stored procedure that takes only FirstName, LastName and calls the actual stored procedure with a default value for CreatedById. You can create overloads at the database layer or create it in the model in the ssdl layer which supports inline stored procedure.
exec myproc #firstName,#LastName,null