Stored procedure in entity framework 4.3 + mvc 4 - asp.net-mvc

My application is using ASP.NET MVC 4 and using stored procedure in EF 4.3
I know, in EF if I am passing parent entity only then also I can add child entity with just in one line of code.
But I am confused how can I insert data in parent-child tables using single stored procedure?
What I want to is my parent's table is having primary key and which I want to insert in child table as foreign key. So how can I do this in one stored procedure using EF 4.3

You don't say what your database is or why you want to use EF4.3 in an mvc4 app or why you want to add entities using a stored procedure rather than just let EF do it for you taking care of the foreign key relationship as it does. You haven't said whether you're using a code first approach or using the entity model design tools
The reason why I bring this up is because ordinarily EF can handle creating parent child entities and will return the entities with populated key values by default and it's easier to code than using stored procedures. If you have to use stored procedures than EF6 has better support for stored procedures then EF4 did so if you must use stored procedures then can you upgrade to EF6?
If you are using code first with EF4 then your basic workflow would be to create a wrapper method for your parent entity and child entity each wrapper method accepts an entity as a parameter the wrapper for the parent entity then builds a database command from the entity with parameters populated with values from the passed in entity it then executes the command and the stored procedure command should return the key value of the new parent entity.
You can then update each of the child entities with the foreign key value and then pass each child entity to the respective wrapper method - which in turn needs to execute the relevant stored procedure which will also return the new primary key value for the child
You can then either just update your passed in entity with the new key values or use the key to get EF to fetch the new entity with the key - the latter is preferable if your stored procedure is doing more than just populating db fields ie calculating values etc.
I don't know what you would do or even if you can do this in the designer - I know that the designer in ef4 has better support for stored procedures than code first. But I would expect that if you're hoping to just pass the object graph to a stored procedures imported into the designer then this isn't going to work ( I don't know that you can't I would be surprised if you could) because you need to be able to pass in a collection of parameters for the child entities which in turn could have child entities.

Related

Can we map database view in Entity Framework?

I have 5 tables to use in one mvc view. So I created a database view and want to map this view into EF for using it as a model. How can I do?
I have successfully used the following in a code-first application where I used Sql Server Functions as 'views' to run complex queries against the database to fetch data (I did not need inserts or updates).
List<MyClass> myClassItems =
dbContext.Database.SqlQuery<MyClass>("Select * from dbo.MyClassFunction(#param)", param).ToList();
I returned myClassItems as the result of a Repository function, with MyClass a class with properties that match the columns that the MyClassFunction returned.

How to call stored procedure with input parameters in ADO.NET Entity Framework 4 in ASP.NET 4.0

I am using Database first modal and EF4.0.
I have created a stored procedure which required two input parameters and return dynamically column in result-set i.e. columns are not fixed in result.It may be 5 or 7 or 10 and so on.
I am using Grid-view and passing the result-set in it's data-source.
How can i call stored procedure in this case.
thanks.
You must use ADO.NET and data reader / data adapter directly. EF can work only with stored procedures producing strongly typed results known at design time (you must create class for them).

Entity Framework: how do I add a field to my entity to match the store procedure result

I have an entity (Org) in my Entity Framework that has a foreign key with a table that is in another database (BusinessUnit). We need the foreign key to get the description of the BusinessUnit linked with the Org. In the past (old project without Entity Framework) we were using a stored procedure to return all the information about this entity, including the BusinessUnit description, using a join. So now my problem is how to display the same information than before using Entity Framework.
I've tried, once I load my Org entity from database, to make a loop accessing to BusinessUnit to get the description for each Org, but this is too slow. My other idea was use a store procedure, but I need an extra field on my entity and Entity Frameworks gives me an 3004 error: No mapping specified for my property. I was thinking to use a complex type, but I'm not sure if it's what I need keeping in mind I have to add just a field to my entity. In that case, could I use the complex type just for "select" operations and keep my original entity for the rest of CRUD operations?
How should I proceed?
Thanks.
EF is not able to execute queries across multiple databases. If you need to perform such query you must either use database view and map the view as a new entity (it will be readonly - making it updatable requires mapping insert, update and delete stored procedures) or divide your data querying into two separate parts to load data from both databases. Divided querying can either use two contexts or you can get data from the second database using stored procedure.
The reason why you got the error is that you added the property in EDMX. EDMX can contain only properties mapped to your first database. EDMX generates entity code as partial classes If you need property manually populated from the second database you have to create your partial part (partial class) for entity and add the property in code.

Property in Entity partial class

I have an entity/table that uses sqlgeography.
Since EF 4.X doesn't support spatial types I'm instead sending the bytes of the field back and forth.
I have stored procs on the database side that handles the converstion and properties on the code side to do that job.
To add the properties in the code I used a partial class.
One of those properties is for the SqlGeography which simply wraps around the byte[] property to handle getting and setting.
This property is hidden from EF using the NotMappedAttribute.
The other is the property exposing the byte[] itself and is decorated with the EdmScalarPropertyAttribute and DataMemberAttribute.
I then go to the EF model designer (*.edmx) to point the entity model at the Insert/Update/Delete stored procs.
It finds the stored procs alright and realises that they (when appropriate) take a VARBINARY parameter.
It also has a drop down allowing you to select a property on the entity class which maps to that parameter.
However this drop down doesn't list either of my properties. I don't care about the SqlGeography property since that is meant to be hidden from EF, however it is vital for me to be able to point it at the byte[] property, as that is where the data comes from.
I would very much like to avoid database triggers or wrapper classes and addiitonal fields to fudge this in to working.
I tried manually editing the .edmx file to include the byte[] property, but then it just complains it's unmapped.
Can anyone give me some insight in to how to get this to work? Or an alternative method of achiving the end result?
We could use a view to create the binary field for us, but this then involves manually creating a lot of the xml for the relationships within the data.
This pretty much voids the point of using EF which is to make life simple and easy.
For this project We'll just add a binary field to the table then have sprocs to handle the converstion on the server and a property in a partial entity class for exposing the geography type in the model.
Next project I doubt we'll be using EF. Dapper is so much more painless, even if theres a touch more code writing involved.
Here's the links for using views if anyone thinks it would be applicable to them:
http://thedatafarm.com/blog/data-access/yes-you-can-read-and-probably-write-spatial-data-with-entity-framework/
http://smehrozalam.wordpress.com/2009/08/12/entity-framework-creating-a-model-using-views-instead-of-tables/
In the end we created a computed column for each table that exposes the spatial data as bytes.
We then use stored procs for inserting and updating the spatial data.

UpdateModel() cannot assign new value to navigation property (entity reference)

This happens in ASP.NET MVC 2, .NET 4 (EF 4). My Address entity has a reference to the Post reference. Zip is the primary key of the Post entity. Another property in Post entity is CityName. In my views I allow users to change the CityName for the address which automatically (via jquery) loads up the corresponding Zip and stores it inside a hidden field.
When posted, both values are posted fine and binded to the Address's Post reference. But UpdateModel() fails to update them. It says that the Zip is part of the entity's Entity Key and cannot be changed.
I would gladly load up the Post entity by the new Zip and manually assign it to the existing Address but for all other properties I stall want to rely on UpdateModel().
How can I achieve that? One would think that in EF4 stuff like this has been resolved..
By default the entity framework generated classes put restrictions on changing primary key values. This is good. You shouldn't change a PK for any reason at all. Changing PKs outside of add scenarios has pretty huge ramifications for state tracking and the general health of your system.
To solve this problem you want to tell UpdateModel not to update your primary keys using the exclude parameter.

Resources